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;
98 /* Maximum size of the CIPSO IP option, derived from the fact that the maximum
99 * IPv4 header size is 60 bytes and the base IPv4 header is 20 bytes long. */
100 #define CIPSO_V4_OPT_LEN_MAX 40
102 /* Length of the base CIPSO option, this includes the option type (1 byte), the
103 * option length (1 byte), and the DOI (4 bytes). */
104 #define CIPSO_V4_HDR_LEN 6
106 /* Base length of the restrictive category bitmap tag (tag #1). */
107 #define CIPSO_V4_TAG_RBM_BLEN 4
109 /* Base length of the enumerated category tag (tag #2). */
110 #define CIPSO_V4_TAG_ENUM_BLEN 4
112 /* Base length of the ranged categories bitmap tag (tag #5). */
113 #define CIPSO_V4_TAG_RNG_BLEN 4
114 /* The maximum number of category ranges permitted in the ranged category tag
115 * (tag #5). You may note that the IETF draft states that the maximum number
116 * of category ranges is 7, but if the low end of the last category range is
117 * zero then it is possibile to fit 8 category ranges because the zero should
119 #define CIPSO_V4_TAG_RNG_CAT_MAX 8
126 * cipso_v4_bitmap_walk - Walk a bitmap looking for a bit
127 * @bitmap: the bitmap
128 * @bitmap_len: length in bits
129 * @offset: starting offset
130 * @state: if non-zero, look for a set (1) bit else look for a cleared (0) bit
133 * Starting at @offset, walk the bitmap from left to right until either the
134 * desired bit is found or we reach the end. Return the bit offset, -1 if
135 * not found, or -2 if error.
137 static int cipso_v4_bitmap_walk(const unsigned char *bitmap
,
144 unsigned char bitmask
;
147 /* gcc always rounds to zero when doing integer division */
148 byte_offset
= offset
/ 8;
149 byte
= bitmap
[byte_offset
];
151 bitmask
= 0x80 >> (offset
% 8);
153 while (bit_spot
< bitmap_len
) {
154 if ((state
&& (byte
& bitmask
) == bitmask
) ||
155 (state
== 0 && (byte
& bitmask
) == 0))
161 byte
= bitmap
[++byte_offset
];
170 * cipso_v4_bitmap_setbit - Sets a single bit in a bitmap
171 * @bitmap: the bitmap
173 * @state: if non-zero, set the bit (1) else clear the bit (0)
176 * Set a single bit in the bitmask. Returns zero on success, negative values
179 static void cipso_v4_bitmap_setbit(unsigned char *bitmap
,
186 /* gcc always rounds to zero when doing integer division */
188 bitmask
= 0x80 >> (bit
% 8);
190 bitmap
[byte_spot
] |= bitmask
;
192 bitmap
[byte_spot
] &= ~bitmask
;
196 * cipso_v4_doi_domhsh_free - Frees a domain list entry
197 * @entry: the entry's RCU field
200 * This function is designed to be used as a callback to the call_rcu()
201 * function so that the memory allocated to a domain list entry can be released
205 static void cipso_v4_doi_domhsh_free(struct rcu_head
*entry
)
207 struct cipso_v4_domhsh_entry
*ptr
;
209 ptr
= container_of(entry
, struct cipso_v4_domhsh_entry
, rcu
);
215 * cipso_v4_cache_entry_free - Frees a cache entry
216 * @entry: the entry to free
219 * This function frees the memory associated with a cache entry including the
220 * LSM cache data if there are no longer any users, i.e. reference count == 0.
223 static void cipso_v4_cache_entry_free(struct cipso_v4_map_cache_entry
*entry
)
226 netlbl_secattr_cache_free(entry
->lsm_data
);
232 * cipso_v4_map_cache_hash - Hashing function for the CIPSO cache
234 * @key_len: the length of the key in bytes
237 * The CIPSO tag hashing function. Returns a 32-bit hash value.
240 static u32
cipso_v4_map_cache_hash(const unsigned char *key
, u32 key_len
)
242 return jhash(key
, key_len
, 0);
246 * Label Mapping Cache Functions
250 * cipso_v4_cache_init - Initialize the CIPSO cache
253 * Initializes the CIPSO label mapping cache, this function should be called
254 * before any of the other functions defined in this file. Returns zero on
255 * success, negative values on error.
258 static int cipso_v4_cache_init(void)
262 cipso_v4_cache
= kcalloc(CIPSO_V4_CACHE_BUCKETS
,
263 sizeof(struct cipso_v4_map_cache_bkt
),
265 if (cipso_v4_cache
== NULL
)
268 for (iter
= 0; iter
< CIPSO_V4_CACHE_BUCKETS
; iter
++) {
269 spin_lock_init(&cipso_v4_cache
[iter
].lock
);
270 cipso_v4_cache
[iter
].size
= 0;
271 INIT_LIST_HEAD(&cipso_v4_cache
[iter
].list
);
278 * cipso_v4_cache_invalidate - Invalidates the current CIPSO cache
281 * Invalidates and frees any entries in the CIPSO cache. Returns zero on
282 * success and negative values on failure.
285 void cipso_v4_cache_invalidate(void)
287 struct cipso_v4_map_cache_entry
*entry
, *tmp_entry
;
290 for (iter
= 0; iter
< CIPSO_V4_CACHE_BUCKETS
; iter
++) {
291 spin_lock_bh(&cipso_v4_cache
[iter
].lock
);
292 list_for_each_entry_safe(entry
,
294 &cipso_v4_cache
[iter
].list
, list
) {
295 list_del(&entry
->list
);
296 cipso_v4_cache_entry_free(entry
);
298 cipso_v4_cache
[iter
].size
= 0;
299 spin_unlock_bh(&cipso_v4_cache
[iter
].lock
);
306 * cipso_v4_cache_check - Check the CIPSO cache for a label mapping
307 * @key: the buffer to check
308 * @key_len: buffer length in bytes
309 * @secattr: the security attribute struct to use
312 * This function checks the cache to see if a label mapping already exists for
313 * the given key. If there is a match then the cache is adjusted and the
314 * @secattr struct is populated with the correct LSM security attributes. The
315 * cache is adjusted in the following manner if the entry is not already the
316 * first in the cache bucket:
318 * 1. The cache entry's activity counter is incremented
319 * 2. The previous (higher ranking) entry's activity counter is decremented
320 * 3. If the difference between the two activity counters is geater than
321 * CIPSO_V4_CACHE_REORDERLIMIT the two entries are swapped
323 * Returns zero on success, -ENOENT for a cache miss, and other negative values
327 static int cipso_v4_cache_check(const unsigned char *key
,
329 struct netlbl_lsm_secattr
*secattr
)
332 struct cipso_v4_map_cache_entry
*entry
;
333 struct cipso_v4_map_cache_entry
*prev_entry
= NULL
;
336 if (!cipso_v4_cache_enabled
)
339 hash
= cipso_v4_map_cache_hash(key
, key_len
);
340 bkt
= hash
& (CIPSO_V4_CACHE_BUCKETBITS
- 1);
341 spin_lock_bh(&cipso_v4_cache
[bkt
].lock
);
342 list_for_each_entry(entry
, &cipso_v4_cache
[bkt
].list
, list
) {
343 if (entry
->hash
== hash
&&
344 entry
->key_len
== key_len
&&
345 memcmp(entry
->key
, key
, key_len
) == 0) {
346 entry
->activity
+= 1;
347 atomic_inc(&entry
->lsm_data
->refcount
);
348 secattr
->cache
= entry
->lsm_data
;
349 secattr
->flags
|= NETLBL_SECATTR_CACHE
;
350 if (prev_entry
== NULL
) {
351 spin_unlock_bh(&cipso_v4_cache
[bkt
].lock
);
355 if (prev_entry
->activity
> 0)
356 prev_entry
->activity
-= 1;
357 if (entry
->activity
> prev_entry
->activity
&&
358 entry
->activity
- prev_entry
->activity
>
359 CIPSO_V4_CACHE_REORDERLIMIT
) {
360 __list_del(entry
->list
.prev
, entry
->list
.next
);
361 __list_add(&entry
->list
,
362 prev_entry
->list
.prev
,
366 spin_unlock_bh(&cipso_v4_cache
[bkt
].lock
);
371 spin_unlock_bh(&cipso_v4_cache
[bkt
].lock
);
377 * cipso_v4_cache_add - Add an entry to the CIPSO cache
379 * @secattr: the packet's security attributes
382 * Add a new entry into the CIPSO label mapping cache. Add the new entry to
383 * head of the cache bucket's list, if the cache bucket is out of room remove
384 * the last entry in the list first. It is important to note that there is
385 * currently no checking for duplicate keys. Returns zero on success,
386 * negative values on failure.
389 int cipso_v4_cache_add(const struct sk_buff
*skb
,
390 const struct netlbl_lsm_secattr
*secattr
)
392 int ret_val
= -EPERM
;
394 struct cipso_v4_map_cache_entry
*entry
= NULL
;
395 struct cipso_v4_map_cache_entry
*old_entry
= NULL
;
396 unsigned char *cipso_ptr
;
399 if (!cipso_v4_cache_enabled
|| cipso_v4_cache_bucketsize
<= 0)
402 cipso_ptr
= CIPSO_V4_OPTPTR(skb
);
403 cipso_ptr_len
= cipso_ptr
[1];
405 entry
= kzalloc(sizeof(*entry
), GFP_ATOMIC
);
408 entry
->key
= kmemdup(cipso_ptr
, cipso_ptr_len
, GFP_ATOMIC
);
409 if (entry
->key
== NULL
) {
411 goto cache_add_failure
;
413 entry
->key_len
= cipso_ptr_len
;
414 entry
->hash
= cipso_v4_map_cache_hash(cipso_ptr
, cipso_ptr_len
);
415 atomic_inc(&secattr
->cache
->refcount
);
416 entry
->lsm_data
= secattr
->cache
;
418 bkt
= entry
->hash
& (CIPSO_V4_CACHE_BUCKETBITS
- 1);
419 spin_lock_bh(&cipso_v4_cache
[bkt
].lock
);
420 if (cipso_v4_cache
[bkt
].size
< cipso_v4_cache_bucketsize
) {
421 list_add(&entry
->list
, &cipso_v4_cache
[bkt
].list
);
422 cipso_v4_cache
[bkt
].size
+= 1;
424 old_entry
= list_entry(cipso_v4_cache
[bkt
].list
.prev
,
425 struct cipso_v4_map_cache_entry
, list
);
426 list_del(&old_entry
->list
);
427 list_add(&entry
->list
, &cipso_v4_cache
[bkt
].list
);
428 cipso_v4_cache_entry_free(old_entry
);
430 spin_unlock_bh(&cipso_v4_cache
[bkt
].lock
);
436 cipso_v4_cache_entry_free(entry
);
445 * cipso_v4_doi_search - Searches for a DOI definition
446 * @doi: the DOI to search for
449 * Search the DOI definition list for a DOI definition with a DOI value that
450 * matches @doi. The caller is responsibile for calling rcu_read_[un]lock().
451 * Returns a pointer to the DOI definition on success and NULL on failure.
453 static struct cipso_v4_doi
*cipso_v4_doi_search(u32 doi
)
455 struct cipso_v4_doi
*iter
;
457 list_for_each_entry_rcu(iter
, &cipso_v4_doi_list
, list
)
458 if (iter
->doi
== doi
&& iter
->valid
)
464 * cipso_v4_doi_add - Add a new DOI to the CIPSO protocol engine
465 * @doi_def: the DOI structure
468 * The caller defines a new DOI for use by the CIPSO engine and calls this
469 * function to add it to the list of acceptable domains. The caller must
470 * ensure that the mapping table specified in @doi_def->map meets all of the
471 * requirements of the mapping type (see cipso_ipv4.h for details). Returns
472 * zero on success and non-zero on failure.
475 int cipso_v4_doi_add(struct cipso_v4_doi
*doi_def
)
479 if (doi_def
== NULL
|| doi_def
->doi
== CIPSO_V4_DOI_UNKNOWN
)
481 for (iter
= 0; iter
< CIPSO_V4_TAG_MAXCNT
; iter
++) {
482 switch (doi_def
->tags
[iter
]) {
483 case CIPSO_V4_TAG_RBITMAP
:
485 case CIPSO_V4_TAG_RANGE
:
486 if (doi_def
->type
!= CIPSO_V4_MAP_PASS
)
489 case CIPSO_V4_TAG_INVALID
:
493 case CIPSO_V4_TAG_ENUM
:
494 if (doi_def
->type
!= CIPSO_V4_MAP_PASS
)
503 INIT_RCU_HEAD(&doi_def
->rcu
);
504 INIT_LIST_HEAD(&doi_def
->dom_list
);
507 if (cipso_v4_doi_search(doi_def
->doi
) != NULL
)
508 goto doi_add_failure_rlock
;
509 spin_lock(&cipso_v4_doi_list_lock
);
510 if (cipso_v4_doi_search(doi_def
->doi
) != NULL
)
511 goto doi_add_failure_slock
;
512 list_add_tail_rcu(&doi_def
->list
, &cipso_v4_doi_list
);
513 spin_unlock(&cipso_v4_doi_list_lock
);
518 doi_add_failure_slock
:
519 spin_unlock(&cipso_v4_doi_list_lock
);
520 doi_add_failure_rlock
:
526 * cipso_v4_doi_remove - Remove an existing DOI from the CIPSO protocol engine
527 * @doi: the DOI value
528 * @audit_secid: the LSM secid to use in the audit message
529 * @callback: the DOI cleanup/free callback
532 * Removes a DOI definition from the CIPSO engine, @callback is called to
533 * free any memory. The NetLabel routines will be called to release their own
534 * LSM domain mappings as well as our own domain list. Returns zero on
535 * success and negative values on failure.
538 int cipso_v4_doi_remove(u32 doi
,
539 struct netlbl_audit
*audit_info
,
540 void (*callback
) (struct rcu_head
* head
))
542 struct cipso_v4_doi
*doi_def
;
543 struct cipso_v4_domhsh_entry
*dom_iter
;
546 if (cipso_v4_doi_search(doi
) != NULL
) {
547 spin_lock(&cipso_v4_doi_list_lock
);
548 doi_def
= cipso_v4_doi_search(doi
);
549 if (doi_def
== NULL
) {
550 spin_unlock(&cipso_v4_doi_list_lock
);
555 list_del_rcu(&doi_def
->list
);
556 spin_unlock(&cipso_v4_doi_list_lock
);
557 list_for_each_entry_rcu(dom_iter
, &doi_def
->dom_list
, list
)
559 netlbl_domhsh_remove(dom_iter
->domain
,
561 cipso_v4_cache_invalidate();
564 call_rcu(&doi_def
->rcu
, callback
);
573 * cipso_v4_doi_getdef - Returns a pointer to a valid DOI definition
574 * @doi: the DOI value
577 * Searches for a valid DOI definition and if one is found it is returned to
578 * the caller. Otherwise NULL is returned. The caller must ensure that
579 * rcu_read_lock() is held while accessing the returned definition.
582 struct cipso_v4_doi
*cipso_v4_doi_getdef(u32 doi
)
584 return cipso_v4_doi_search(doi
);
588 * cipso_v4_doi_walk - Iterate through the DOI definitions
589 * @skip_cnt: skip past this number of DOI definitions, updated
590 * @callback: callback for each DOI definition
591 * @cb_arg: argument for the callback function
594 * Iterate over the DOI definition list, skipping the first @skip_cnt entries.
595 * For each entry call @callback, if @callback returns a negative value stop
596 * 'walking' through the list and return. Updates the value in @skip_cnt upon
597 * return. Returns zero on success, negative values on failure.
600 int cipso_v4_doi_walk(u32
*skip_cnt
,
601 int (*callback
) (struct cipso_v4_doi
*doi_def
, void *arg
),
604 int ret_val
= -ENOENT
;
606 struct cipso_v4_doi
*iter_doi
;
609 list_for_each_entry_rcu(iter_doi
, &cipso_v4_doi_list
, list
)
610 if (iter_doi
->valid
) {
611 if (doi_cnt
++ < *skip_cnt
)
613 ret_val
= callback(iter_doi
, cb_arg
);
616 goto doi_walk_return
;
627 * cipso_v4_doi_domhsh_add - Adds a domain entry to a DOI definition
628 * @doi_def: the DOI definition
629 * @domain: the domain to add
632 * Adds the @domain to the DOI specified by @doi_def, this function
633 * should only be called by external functions (i.e. NetLabel). This function
634 * does allocate memory. Returns zero on success, negative values on failure.
637 int cipso_v4_doi_domhsh_add(struct cipso_v4_doi
*doi_def
, const char *domain
)
639 struct cipso_v4_domhsh_entry
*iter
;
640 struct cipso_v4_domhsh_entry
*new_dom
;
642 new_dom
= kzalloc(sizeof(*new_dom
), GFP_KERNEL
);
646 new_dom
->domain
= kstrdup(domain
, GFP_KERNEL
);
647 if (new_dom
->domain
== NULL
) {
653 INIT_RCU_HEAD(&new_dom
->rcu
);
656 spin_lock(&cipso_v4_doi_list_lock
);
657 list_for_each_entry_rcu(iter
, &doi_def
->dom_list
, list
)
659 ((domain
!= NULL
&& iter
->domain
!= NULL
&&
660 strcmp(iter
->domain
, domain
) == 0) ||
661 (domain
== NULL
&& iter
->domain
== NULL
))) {
662 spin_unlock(&cipso_v4_doi_list_lock
);
664 kfree(new_dom
->domain
);
668 list_add_tail_rcu(&new_dom
->list
, &doi_def
->dom_list
);
669 spin_unlock(&cipso_v4_doi_list_lock
);
676 * cipso_v4_doi_domhsh_remove - Removes a domain entry from a DOI definition
677 * @doi_def: the DOI definition
678 * @domain: the domain to remove
681 * Removes the @domain from the DOI specified by @doi_def, this function
682 * should only be called by external functions (i.e. NetLabel). Returns zero
683 * on success and negative values on error.
686 int cipso_v4_doi_domhsh_remove(struct cipso_v4_doi
*doi_def
,
689 struct cipso_v4_domhsh_entry
*iter
;
692 spin_lock(&cipso_v4_doi_list_lock
);
693 list_for_each_entry_rcu(iter
, &doi_def
->dom_list
, list
)
695 ((domain
!= NULL
&& iter
->domain
!= NULL
&&
696 strcmp(iter
->domain
, domain
) == 0) ||
697 (domain
== NULL
&& iter
->domain
== NULL
))) {
699 list_del_rcu(&iter
->list
);
700 spin_unlock(&cipso_v4_doi_list_lock
);
702 call_rcu(&iter
->rcu
, cipso_v4_doi_domhsh_free
);
706 spin_unlock(&cipso_v4_doi_list_lock
);
713 * Label Mapping Functions
717 * cipso_v4_map_lvl_valid - Checks to see if the given level is understood
718 * @doi_def: the DOI definition
719 * @level: the level to check
722 * Checks the given level against the given DOI definition and returns a
723 * negative value if the level does not have a valid mapping and a zero value
724 * if the level is defined by the DOI.
727 static int cipso_v4_map_lvl_valid(const struct cipso_v4_doi
*doi_def
, u8 level
)
729 switch (doi_def
->type
) {
730 case CIPSO_V4_MAP_PASS
:
732 case CIPSO_V4_MAP_STD
:
733 if (doi_def
->map
.std
->lvl
.cipso
[level
] < CIPSO_V4_INV_LVL
)
742 * cipso_v4_map_lvl_hton - Perform a level mapping from the host to the network
743 * @doi_def: the DOI definition
744 * @host_lvl: the host MLS level
745 * @net_lvl: the network/CIPSO MLS level
748 * Perform a label mapping to translate a local MLS level to the correct
749 * CIPSO level using the given DOI definition. Returns zero on success,
750 * negative values otherwise.
753 static int cipso_v4_map_lvl_hton(const struct cipso_v4_doi
*doi_def
,
757 switch (doi_def
->type
) {
758 case CIPSO_V4_MAP_PASS
:
761 case CIPSO_V4_MAP_STD
:
762 if (host_lvl
< doi_def
->map
.std
->lvl
.local_size
&&
763 doi_def
->map
.std
->lvl
.local
[host_lvl
] < CIPSO_V4_INV_LVL
) {
764 *net_lvl
= doi_def
->map
.std
->lvl
.local
[host_lvl
];
774 * cipso_v4_map_lvl_ntoh - Perform a level mapping from the network to the host
775 * @doi_def: the DOI definition
776 * @net_lvl: the network/CIPSO MLS level
777 * @host_lvl: the host MLS level
780 * Perform a label mapping to translate a CIPSO level to the correct local MLS
781 * level using the given DOI definition. Returns zero on success, negative
785 static int cipso_v4_map_lvl_ntoh(const struct cipso_v4_doi
*doi_def
,
789 struct cipso_v4_std_map_tbl
*map_tbl
;
791 switch (doi_def
->type
) {
792 case CIPSO_V4_MAP_PASS
:
795 case CIPSO_V4_MAP_STD
:
796 map_tbl
= doi_def
->map
.std
;
797 if (net_lvl
< map_tbl
->lvl
.cipso_size
&&
798 map_tbl
->lvl
.cipso
[net_lvl
] < CIPSO_V4_INV_LVL
) {
799 *host_lvl
= doi_def
->map
.std
->lvl
.cipso
[net_lvl
];
809 * cipso_v4_map_cat_rbm_valid - Checks to see if the category bitmap is valid
810 * @doi_def: the DOI definition
811 * @bitmap: category bitmap
812 * @bitmap_len: bitmap length in bytes
815 * Checks the given category bitmap against the given DOI definition and
816 * returns a negative value if any of the categories in the bitmap do not have
817 * a valid mapping and a zero value if all of the categories are valid.
820 static int cipso_v4_map_cat_rbm_valid(const struct cipso_v4_doi
*doi_def
,
821 const unsigned char *bitmap
,
825 u32 bitmap_len_bits
= bitmap_len
* 8;
829 switch (doi_def
->type
) {
830 case CIPSO_V4_MAP_PASS
:
832 case CIPSO_V4_MAP_STD
:
833 cipso_cat_size
= doi_def
->map
.std
->cat
.cipso_size
;
834 cipso_array
= doi_def
->map
.std
->cat
.cipso
;
836 cat
= cipso_v4_bitmap_walk(bitmap
,
842 if (cat
>= cipso_cat_size
||
843 cipso_array
[cat
] >= CIPSO_V4_INV_CAT
)
856 * cipso_v4_map_cat_rbm_hton - Perform a category mapping from host to network
857 * @doi_def: the DOI definition
858 * @secattr: the security attributes
859 * @net_cat: the zero'd out category bitmap in network/CIPSO format
860 * @net_cat_len: the length of the CIPSO bitmap in bytes
863 * Perform a label mapping to translate a local MLS category bitmap to the
864 * correct CIPSO bitmap using the given DOI definition. Returns the minimum
865 * size in bytes of the network bitmap on success, negative values otherwise.
868 static int cipso_v4_map_cat_rbm_hton(const struct cipso_v4_doi
*doi_def
,
869 const struct netlbl_lsm_secattr
*secattr
,
870 unsigned char *net_cat
,
874 u32 net_spot
= CIPSO_V4_INV_CAT
;
875 u32 net_spot_max
= 0;
876 u32 net_clen_bits
= net_cat_len
* 8;
877 u32 host_cat_size
= 0;
878 u32
*host_cat_array
= NULL
;
880 if (doi_def
->type
== CIPSO_V4_MAP_STD
) {
881 host_cat_size
= doi_def
->map
.std
->cat
.local_size
;
882 host_cat_array
= doi_def
->map
.std
->cat
.local
;
886 host_spot
= netlbl_secattr_catmap_walk(secattr
->mls_cat
,
891 switch (doi_def
->type
) {
892 case CIPSO_V4_MAP_PASS
:
893 net_spot
= host_spot
;
895 case CIPSO_V4_MAP_STD
:
896 if (host_spot
>= host_cat_size
)
898 net_spot
= host_cat_array
[host_spot
];
899 if (net_spot
>= CIPSO_V4_INV_CAT
)
903 if (net_spot
>= net_clen_bits
)
905 cipso_v4_bitmap_setbit(net_cat
, net_spot
, 1);
907 if (net_spot
> net_spot_max
)
908 net_spot_max
= net_spot
;
911 if (++net_spot_max
% 8)
912 return net_spot_max
/ 8 + 1;
913 return net_spot_max
/ 8;
917 * cipso_v4_map_cat_rbm_ntoh - Perform a category mapping from network to host
918 * @doi_def: the DOI definition
919 * @net_cat: the category bitmap in network/CIPSO format
920 * @net_cat_len: the length of the CIPSO bitmap in bytes
921 * @secattr: the security attributes
924 * Perform a label mapping to translate a CIPSO bitmap to the correct local
925 * MLS category bitmap using the given DOI definition. Returns zero on
926 * success, negative values on failure.
929 static int cipso_v4_map_cat_rbm_ntoh(const struct cipso_v4_doi
*doi_def
,
930 const unsigned char *net_cat
,
932 struct netlbl_lsm_secattr
*secattr
)
936 u32 host_spot
= CIPSO_V4_INV_CAT
;
937 u32 net_clen_bits
= net_cat_len
* 8;
938 u32 net_cat_size
= 0;
939 u32
*net_cat_array
= NULL
;
941 if (doi_def
->type
== CIPSO_V4_MAP_STD
) {
942 net_cat_size
= doi_def
->map
.std
->cat
.cipso_size
;
943 net_cat_array
= doi_def
->map
.std
->cat
.cipso
;
947 net_spot
= cipso_v4_bitmap_walk(net_cat
,
957 switch (doi_def
->type
) {
958 case CIPSO_V4_MAP_PASS
:
959 host_spot
= net_spot
;
961 case CIPSO_V4_MAP_STD
:
962 if (net_spot
>= net_cat_size
)
964 host_spot
= net_cat_array
[net_spot
];
965 if (host_spot
>= CIPSO_V4_INV_CAT
)
969 ret_val
= netlbl_secattr_catmap_setbit(secattr
->mls_cat
,
980 * cipso_v4_map_cat_enum_valid - Checks to see if the categories are valid
981 * @doi_def: the DOI definition
982 * @enumcat: category list
983 * @enumcat_len: length of the category list in bytes
986 * Checks the given categories against the given DOI definition and returns a
987 * negative value if any of the categories do not have a valid mapping and a
988 * zero value if all of the categories are valid.
991 static int cipso_v4_map_cat_enum_valid(const struct cipso_v4_doi
*doi_def
,
992 const unsigned char *enumcat
,
999 if (doi_def
->type
!= CIPSO_V4_MAP_PASS
|| enumcat_len
& 0x01)
1002 for (iter
= 0; iter
< enumcat_len
; iter
+= 2) {
1003 cat
= ntohs(*((__be16
*)&enumcat
[iter
]));
1004 if (cat
<= cat_prev
)
1013 * cipso_v4_map_cat_enum_hton - Perform a category mapping from host to network
1014 * @doi_def: the DOI definition
1015 * @secattr: the security attributes
1016 * @net_cat: the zero'd out category list in network/CIPSO format
1017 * @net_cat_len: the length of the CIPSO category list in bytes
1020 * Perform a label mapping to translate a local MLS category bitmap to the
1021 * correct CIPSO category list using the given DOI definition. Returns the
1022 * size in bytes of the network category bitmap on success, negative values
1026 static int cipso_v4_map_cat_enum_hton(const struct cipso_v4_doi
*doi_def
,
1027 const struct netlbl_lsm_secattr
*secattr
,
1028 unsigned char *net_cat
,
1035 cat
= netlbl_secattr_catmap_walk(secattr
->mls_cat
, cat
+ 1);
1038 if ((cat_iter
+ 2) > net_cat_len
)
1041 *((__be16
*)&net_cat
[cat_iter
]) = htons(cat
);
1049 * cipso_v4_map_cat_enum_ntoh - Perform a category mapping from network to host
1050 * @doi_def: the DOI definition
1051 * @net_cat: the category list in network/CIPSO format
1052 * @net_cat_len: the length of the CIPSO bitmap in bytes
1053 * @secattr: the security attributes
1056 * Perform a label mapping to translate a CIPSO category list to the correct
1057 * local MLS category bitmap using the given DOI definition. Returns zero on
1058 * success, negative values on failure.
1061 static int cipso_v4_map_cat_enum_ntoh(const struct cipso_v4_doi
*doi_def
,
1062 const unsigned char *net_cat
,
1064 struct netlbl_lsm_secattr
*secattr
)
1069 for (iter
= 0; iter
< net_cat_len
; iter
+= 2) {
1070 ret_val
= netlbl_secattr_catmap_setbit(secattr
->mls_cat
,
1071 ntohs(*((__be16
*)&net_cat
[iter
])),
1081 * cipso_v4_map_cat_rng_valid - Checks to see if the categories are valid
1082 * @doi_def: the DOI definition
1083 * @rngcat: category list
1084 * @rngcat_len: length of the category list in bytes
1087 * Checks the given categories against the given DOI definition and returns a
1088 * negative value if any of the categories do not have a valid mapping and a
1089 * zero value if all of the categories are valid.
1092 static int cipso_v4_map_cat_rng_valid(const struct cipso_v4_doi
*doi_def
,
1093 const unsigned char *rngcat
,
1098 u32 cat_prev
= CIPSO_V4_MAX_REM_CATS
+ 1;
1101 if (doi_def
->type
!= CIPSO_V4_MAP_PASS
|| rngcat_len
& 0x01)
1104 for (iter
= 0; iter
< rngcat_len
; iter
+= 4) {
1105 cat_high
= ntohs(*((__be16
*)&rngcat
[iter
]));
1106 if ((iter
+ 4) <= rngcat_len
)
1107 cat_low
= ntohs(*((__be16
*)&rngcat
[iter
+ 2]));
1111 if (cat_high
> cat_prev
)
1121 * cipso_v4_map_cat_rng_hton - Perform a category mapping from host to network
1122 * @doi_def: the DOI definition
1123 * @secattr: the security attributes
1124 * @net_cat: the zero'd out category list in network/CIPSO format
1125 * @net_cat_len: the length of the CIPSO category list in bytes
1128 * Perform a label mapping to translate a local MLS category bitmap to the
1129 * correct CIPSO category list using the given DOI definition. Returns the
1130 * size in bytes of the network category bitmap on success, negative values
1134 static int cipso_v4_map_cat_rng_hton(const struct cipso_v4_doi
*doi_def
,
1135 const struct netlbl_lsm_secattr
*secattr
,
1136 unsigned char *net_cat
,
1140 u16 array
[CIPSO_V4_TAG_RNG_CAT_MAX
* 2];
1144 /* make sure we don't overflow the 'array[]' variable */
1146 (CIPSO_V4_OPT_LEN_MAX
- CIPSO_V4_HDR_LEN
- CIPSO_V4_TAG_RNG_BLEN
))
1150 iter
= netlbl_secattr_catmap_walk(secattr
->mls_cat
, iter
+ 1);
1153 cat_size
+= (iter
== 0 ? 0 : sizeof(u16
));
1154 if (cat_size
> net_cat_len
)
1156 array
[array_cnt
++] = iter
;
1158 iter
= netlbl_secattr_catmap_walk_rng(secattr
->mls_cat
, iter
);
1161 cat_size
+= sizeof(u16
);
1162 if (cat_size
> net_cat_len
)
1164 array
[array_cnt
++] = iter
;
1167 for (iter
= 0; array_cnt
> 0;) {
1168 *((__be16
*)&net_cat
[iter
]) = htons(array
[--array_cnt
]);
1171 if (array
[array_cnt
] != 0) {
1172 *((__be16
*)&net_cat
[iter
]) = htons(array
[array_cnt
]);
1181 * cipso_v4_map_cat_rng_ntoh - Perform a category mapping from network to host
1182 * @doi_def: the DOI definition
1183 * @net_cat: the category list in network/CIPSO format
1184 * @net_cat_len: the length of the CIPSO bitmap in bytes
1185 * @secattr: the security attributes
1188 * Perform a label mapping to translate a CIPSO category list to the correct
1189 * local MLS category bitmap using the given DOI definition. Returns zero on
1190 * success, negative values on failure.
1193 static int cipso_v4_map_cat_rng_ntoh(const struct cipso_v4_doi
*doi_def
,
1194 const unsigned char *net_cat
,
1196 struct netlbl_lsm_secattr
*secattr
)
1203 for (net_iter
= 0; net_iter
< net_cat_len
; net_iter
+= 4) {
1204 cat_high
= ntohs(*((__be16
*)&net_cat
[net_iter
]));
1205 if ((net_iter
+ 4) <= net_cat_len
)
1206 cat_low
= ntohs(*((__be16
*)&net_cat
[net_iter
+ 2]));
1210 ret_val
= netlbl_secattr_catmap_setrng(secattr
->mls_cat
,
1222 * Protocol Handling Functions
1226 * cipso_v4_gentag_hdr - Generate a CIPSO option header
1227 * @doi_def: the DOI definition
1228 * @len: the total tag length in bytes, not including this header
1229 * @buf: the CIPSO option buffer
1232 * Write a CIPSO header into the beginning of @buffer.
1235 static void cipso_v4_gentag_hdr(const struct cipso_v4_doi
*doi_def
,
1239 buf
[0] = IPOPT_CIPSO
;
1240 buf
[1] = CIPSO_V4_HDR_LEN
+ len
;
1241 *(__be32
*)&buf
[2] = htonl(doi_def
->doi
);
1245 * cipso_v4_gentag_rbm - Generate a CIPSO restricted bitmap tag (type #1)
1246 * @doi_def: the DOI definition
1247 * @secattr: the security attributes
1248 * @buffer: the option buffer
1249 * @buffer_len: length of buffer in bytes
1252 * Generate a CIPSO option using the restricted bitmap tag, tag type #1. The
1253 * actual buffer length may be larger than the indicated size due to
1254 * translation between host and network category bitmaps. Returns the size of
1255 * the tag on success, negative values on failure.
1258 static int cipso_v4_gentag_rbm(const struct cipso_v4_doi
*doi_def
,
1259 const struct netlbl_lsm_secattr
*secattr
,
1260 unsigned char *buffer
,
1267 if ((secattr
->flags
& NETLBL_SECATTR_MLS_LVL
) == 0)
1270 ret_val
= cipso_v4_map_lvl_hton(doi_def
, secattr
->mls_lvl
, &level
);
1274 if (secattr
->flags
& NETLBL_SECATTR_MLS_CAT
) {
1275 ret_val
= cipso_v4_map_cat_rbm_hton(doi_def
,
1282 /* This will send packets using the "optimized" format when
1283 * possibile as specified in section 3.4.2.6 of the
1285 if (cipso_v4_rbm_optfmt
&& ret_val
> 0 && ret_val
<= 10)
1288 tag_len
= 4 + ret_val
;
1293 buffer
[1] = tag_len
;
1300 * cipso_v4_parsetag_rbm - Parse a CIPSO restricted bitmap tag
1301 * @doi_def: the DOI definition
1302 * @tag: the CIPSO tag
1303 * @secattr: the security attributes
1306 * Parse a CIPSO restricted bitmap tag (tag type #1) and return the security
1307 * attributes in @secattr. Return zero on success, negatives values on
1311 static int cipso_v4_parsetag_rbm(const struct cipso_v4_doi
*doi_def
,
1312 const unsigned char *tag
,
1313 struct netlbl_lsm_secattr
*secattr
)
1316 u8 tag_len
= tag
[1];
1319 ret_val
= cipso_v4_map_lvl_ntoh(doi_def
, tag
[3], &level
);
1322 secattr
->mls_lvl
= level
;
1323 secattr
->flags
|= NETLBL_SECATTR_MLS_LVL
;
1326 secattr
->mls_cat
= netlbl_secattr_catmap_alloc(GFP_ATOMIC
);
1327 if (secattr
->mls_cat
== NULL
)
1330 ret_val
= cipso_v4_map_cat_rbm_ntoh(doi_def
,
1335 netlbl_secattr_catmap_free(secattr
->mls_cat
);
1339 secattr
->flags
|= NETLBL_SECATTR_MLS_CAT
;
1346 * cipso_v4_gentag_enum - Generate a CIPSO enumerated tag (type #2)
1347 * @doi_def: the DOI definition
1348 * @secattr: the security attributes
1349 * @buffer: the option buffer
1350 * @buffer_len: length of buffer in bytes
1353 * Generate a CIPSO option using the enumerated tag, tag type #2. Returns the
1354 * size of the tag on success, negative values on failure.
1357 static int cipso_v4_gentag_enum(const struct cipso_v4_doi
*doi_def
,
1358 const struct netlbl_lsm_secattr
*secattr
,
1359 unsigned char *buffer
,
1366 if (!(secattr
->flags
& NETLBL_SECATTR_MLS_LVL
))
1369 ret_val
= cipso_v4_map_lvl_hton(doi_def
, secattr
->mls_lvl
, &level
);
1373 if (secattr
->flags
& NETLBL_SECATTR_MLS_CAT
) {
1374 ret_val
= cipso_v4_map_cat_enum_hton(doi_def
,
1381 tag_len
= 4 + ret_val
;
1386 buffer
[1] = tag_len
;
1393 * cipso_v4_parsetag_enum - Parse a CIPSO enumerated tag
1394 * @doi_def: the DOI definition
1395 * @tag: the CIPSO tag
1396 * @secattr: the security attributes
1399 * Parse a CIPSO enumerated tag (tag type #2) and return the security
1400 * attributes in @secattr. Return zero on success, negatives values on
1404 static int cipso_v4_parsetag_enum(const struct cipso_v4_doi
*doi_def
,
1405 const unsigned char *tag
,
1406 struct netlbl_lsm_secattr
*secattr
)
1409 u8 tag_len
= tag
[1];
1412 ret_val
= cipso_v4_map_lvl_ntoh(doi_def
, tag
[3], &level
);
1415 secattr
->mls_lvl
= level
;
1416 secattr
->flags
|= NETLBL_SECATTR_MLS_LVL
;
1419 secattr
->mls_cat
= netlbl_secattr_catmap_alloc(GFP_ATOMIC
);
1420 if (secattr
->mls_cat
== NULL
)
1423 ret_val
= cipso_v4_map_cat_enum_ntoh(doi_def
,
1428 netlbl_secattr_catmap_free(secattr
->mls_cat
);
1432 secattr
->flags
|= NETLBL_SECATTR_MLS_CAT
;
1439 * cipso_v4_gentag_rng - Generate a CIPSO ranged tag (type #5)
1440 * @doi_def: the DOI definition
1441 * @secattr: the security attributes
1442 * @buffer: the option buffer
1443 * @buffer_len: length of buffer in bytes
1446 * Generate a CIPSO option using the ranged tag, tag type #5. Returns the
1447 * size of the tag on success, negative values on failure.
1450 static int cipso_v4_gentag_rng(const struct cipso_v4_doi
*doi_def
,
1451 const struct netlbl_lsm_secattr
*secattr
,
1452 unsigned char *buffer
,
1459 if (!(secattr
->flags
& NETLBL_SECATTR_MLS_LVL
))
1462 ret_val
= cipso_v4_map_lvl_hton(doi_def
, secattr
->mls_lvl
, &level
);
1466 if (secattr
->flags
& NETLBL_SECATTR_MLS_CAT
) {
1467 ret_val
= cipso_v4_map_cat_rng_hton(doi_def
,
1474 tag_len
= 4 + ret_val
;
1479 buffer
[1] = tag_len
;
1486 * cipso_v4_parsetag_rng - Parse a CIPSO ranged tag
1487 * @doi_def: the DOI definition
1488 * @tag: the CIPSO tag
1489 * @secattr: the security attributes
1492 * Parse a CIPSO ranged tag (tag type #5) and return the security attributes
1493 * in @secattr. Return zero on success, negatives values on failure.
1496 static int cipso_v4_parsetag_rng(const struct cipso_v4_doi
*doi_def
,
1497 const unsigned char *tag
,
1498 struct netlbl_lsm_secattr
*secattr
)
1501 u8 tag_len
= tag
[1];
1504 ret_val
= cipso_v4_map_lvl_ntoh(doi_def
, tag
[3], &level
);
1507 secattr
->mls_lvl
= level
;
1508 secattr
->flags
|= NETLBL_SECATTR_MLS_LVL
;
1511 secattr
->mls_cat
= netlbl_secattr_catmap_alloc(GFP_ATOMIC
);
1512 if (secattr
->mls_cat
== NULL
)
1515 ret_val
= cipso_v4_map_cat_rng_ntoh(doi_def
,
1520 netlbl_secattr_catmap_free(secattr
->mls_cat
);
1524 secattr
->flags
|= NETLBL_SECATTR_MLS_CAT
;
1531 * cipso_v4_validate - Validate a CIPSO option
1532 * @option: the start of the option, on error it is set to point to the error
1535 * This routine is called to validate a CIPSO option, it checks all of the
1536 * fields to ensure that they are at least valid, see the draft snippet below
1537 * for details. If the option is valid then a zero value is returned and
1538 * the value of @option is unchanged. If the option is invalid then a
1539 * non-zero value is returned and @option is adjusted to point to the
1540 * offending portion of the option. From the IETF draft ...
1542 * "If any field within the CIPSO options, such as the DOI identifier, is not
1543 * recognized the IP datagram is discarded and an ICMP 'parameter problem'
1544 * (type 12) is generated and returned. The ICMP code field is set to 'bad
1545 * parameter' (code 0) and the pointer is set to the start of the CIPSO field
1546 * that is unrecognized."
1549 int cipso_v4_validate(unsigned char **option
)
1551 unsigned char *opt
= *option
;
1553 unsigned char opt_iter
;
1554 unsigned char err_offset
= 0;
1557 struct cipso_v4_doi
*doi_def
= NULL
;
1560 /* caller already checks for length values that are too large */
1564 goto validate_return
;
1568 doi_def
= cipso_v4_doi_search(ntohl(*((__be32
*)&opt
[2])));
1569 if (doi_def
== NULL
) {
1571 goto validate_return_locked
;
1575 tag
= opt
+ opt_iter
;
1576 while (opt_iter
< opt_len
) {
1577 for (tag_iter
= 0; doi_def
->tags
[tag_iter
] != tag
[0];)
1578 if (doi_def
->tags
[tag_iter
] == CIPSO_V4_TAG_INVALID
||
1579 ++tag_iter
== CIPSO_V4_TAG_MAXCNT
) {
1580 err_offset
= opt_iter
;
1581 goto validate_return_locked
;
1585 if (tag_len
> (opt_len
- opt_iter
)) {
1586 err_offset
= opt_iter
+ 1;
1587 goto validate_return_locked
;
1591 case CIPSO_V4_TAG_RBITMAP
:
1593 err_offset
= opt_iter
+ 1;
1594 goto validate_return_locked
;
1597 /* We are already going to do all the verification
1598 * necessary at the socket layer so from our point of
1599 * view it is safe to turn these checks off (and less
1600 * work), however, the CIPSO draft says we should do
1601 * all the CIPSO validations here but it doesn't
1602 * really specify _exactly_ what we need to validate
1603 * ... so, just make it a sysctl tunable. */
1604 if (cipso_v4_rbm_strictvalid
) {
1605 if (cipso_v4_map_lvl_valid(doi_def
,
1607 err_offset
= opt_iter
+ 3;
1608 goto validate_return_locked
;
1611 cipso_v4_map_cat_rbm_valid(doi_def
,
1614 err_offset
= opt_iter
+ 4;
1615 goto validate_return_locked
;
1619 case CIPSO_V4_TAG_ENUM
:
1621 err_offset
= opt_iter
+ 1;
1622 goto validate_return_locked
;
1625 if (cipso_v4_map_lvl_valid(doi_def
,
1627 err_offset
= opt_iter
+ 3;
1628 goto validate_return_locked
;
1631 cipso_v4_map_cat_enum_valid(doi_def
,
1634 err_offset
= opt_iter
+ 4;
1635 goto validate_return_locked
;
1638 case CIPSO_V4_TAG_RANGE
:
1640 err_offset
= opt_iter
+ 1;
1641 goto validate_return_locked
;
1644 if (cipso_v4_map_lvl_valid(doi_def
,
1646 err_offset
= opt_iter
+ 3;
1647 goto validate_return_locked
;
1650 cipso_v4_map_cat_rng_valid(doi_def
,
1653 err_offset
= opt_iter
+ 4;
1654 goto validate_return_locked
;
1658 err_offset
= opt_iter
;
1659 goto validate_return_locked
;
1663 opt_iter
+= tag_len
;
1666 validate_return_locked
:
1669 *option
= opt
+ err_offset
;
1674 * cipso_v4_error - Send the correct reponse for a bad packet
1676 * @error: the error code
1677 * @gateway: CIPSO gateway flag
1680 * Based on the error code given in @error, send an ICMP error message back to
1681 * the originating host. From the IETF draft ...
1683 * "If the contents of the CIPSO [option] are valid but the security label is
1684 * outside of the configured host or port label range, the datagram is
1685 * discarded and an ICMP 'destination unreachable' (type 3) is generated and
1686 * returned. The code field of the ICMP is set to 'communication with
1687 * destination network administratively prohibited' (code 9) or to
1688 * 'communication with destination host administratively prohibited'
1689 * (code 10). The value of the code is dependent on whether the originator
1690 * of the ICMP message is acting as a CIPSO host or a CIPSO gateway. The
1691 * recipient of the ICMP message MUST be able to handle either value. The
1692 * same procedure is performed if a CIPSO [option] can not be added to an
1693 * IP packet because it is too large to fit in the IP options area."
1695 * "If the error is triggered by receipt of an ICMP message, the message is
1696 * discarded and no response is permitted (consistent with general ICMP
1697 * processing rules)."
1700 void cipso_v4_error(struct sk_buff
*skb
, int error
, u32 gateway
)
1702 if (ip_hdr(skb
)->protocol
== IPPROTO_ICMP
|| error
!= -EACCES
)
1706 icmp_send(skb
, ICMP_DEST_UNREACH
, ICMP_NET_ANO
, 0);
1708 icmp_send(skb
, ICMP_DEST_UNREACH
, ICMP_HOST_ANO
, 0);
1712 * cipso_v4_socket_setattr - Add a CIPSO option to a socket
1714 * @doi_def: the CIPSO DOI to use
1715 * @secattr: the specific security attributes of the socket
1718 * Set the CIPSO option on the given socket using the DOI definition and
1719 * security attributes passed to the function. This function requires
1720 * exclusive access to @sock->sk, which means it either needs to be in the
1721 * process of being created or locked via lock_sock(sock->sk). Returns zero on
1722 * success and negative values on failure.
1725 int cipso_v4_socket_setattr(const struct socket
*sock
,
1726 const struct cipso_v4_doi
*doi_def
,
1727 const struct netlbl_lsm_secattr
*secattr
)
1729 int ret_val
= -EPERM
;
1734 struct ip_options
*opt
= NULL
;
1736 struct inet_sock
*sk_inet
;
1737 struct inet_connection_sock
*sk_conn
;
1739 /* In the case of sock_create_lite(), the sock->sk field is not
1740 * defined yet but it is not a problem as the only users of these
1741 * "lite" PF_INET sockets are functions which do an accept() call
1742 * afterwards so we will label the socket as part of the accept(). */
1747 /* We allocate the maximum CIPSO option size here so we are probably
1748 * being a little wasteful, but it makes our life _much_ easier later
1749 * on and after all we are only talking about 40 bytes. */
1750 buf_len
= CIPSO_V4_OPT_LEN_MAX
;
1751 buf
= kmalloc(buf_len
, GFP_ATOMIC
);
1754 goto socket_setattr_failure
;
1757 /* XXX - This code assumes only one tag per CIPSO option which isn't
1758 * really a good assumption to make but since we only support the MAC
1759 * tags right now it is a safe assumption. */
1762 memset(buf
, 0, buf_len
);
1763 switch (doi_def
->tags
[iter
]) {
1764 case CIPSO_V4_TAG_RBITMAP
:
1765 ret_val
= cipso_v4_gentag_rbm(doi_def
,
1767 &buf
[CIPSO_V4_HDR_LEN
],
1768 buf_len
- CIPSO_V4_HDR_LEN
);
1770 case CIPSO_V4_TAG_ENUM
:
1771 ret_val
= cipso_v4_gentag_enum(doi_def
,
1773 &buf
[CIPSO_V4_HDR_LEN
],
1774 buf_len
- CIPSO_V4_HDR_LEN
);
1776 case CIPSO_V4_TAG_RANGE
:
1777 ret_val
= cipso_v4_gentag_rng(doi_def
,
1779 &buf
[CIPSO_V4_HDR_LEN
],
1780 buf_len
- CIPSO_V4_HDR_LEN
);
1784 goto socket_setattr_failure
;
1788 } while (ret_val
< 0 &&
1789 iter
< CIPSO_V4_TAG_MAXCNT
&&
1790 doi_def
->tags
[iter
] != CIPSO_V4_TAG_INVALID
);
1792 goto socket_setattr_failure
;
1793 cipso_v4_gentag_hdr(doi_def
, buf
, ret_val
);
1794 buf_len
= CIPSO_V4_HDR_LEN
+ ret_val
;
1796 /* We can't use ip_options_get() directly because it makes a call to
1797 * ip_options_get_alloc() which allocates memory with GFP_KERNEL and
1798 * we won't always have CAP_NET_RAW even though we _always_ want to
1799 * set the IPOPT_CIPSO option. */
1800 opt_len
= (buf_len
+ 3) & ~3;
1801 opt
= kzalloc(sizeof(*opt
) + opt_len
, GFP_ATOMIC
);
1804 goto socket_setattr_failure
;
1806 memcpy(opt
->__data
, buf
, buf_len
);
1807 opt
->optlen
= opt_len
;
1809 opt
->cipso
= sizeof(struct iphdr
);
1813 sk_inet
= inet_sk(sk
);
1814 if (sk_inet
->is_icsk
) {
1815 sk_conn
= inet_csk(sk
);
1817 sk_conn
->icsk_ext_hdr_len
-= sk_inet
->opt
->optlen
;
1818 sk_conn
->icsk_ext_hdr_len
+= opt
->optlen
;
1819 sk_conn
->icsk_sync_mss(sk
, sk_conn
->icsk_pmtu_cookie
);
1821 opt
= xchg(&sk_inet
->opt
, opt
);
1826 socket_setattr_failure
:
1833 * cipso_v4_sock_getattr - Get the security attributes from a sock
1835 * @secattr: the security attributes
1838 * Query @sk to see if there is a CIPSO option attached to the sock and if
1839 * there is return the CIPSO security attributes in @secattr. This function
1840 * requires that @sk be locked, or privately held, but it does not do any
1841 * locking itself. Returns zero on success and negative values on failure.
1844 int cipso_v4_sock_getattr(struct sock
*sk
, struct netlbl_lsm_secattr
*secattr
)
1846 int ret_val
= -ENOMSG
;
1847 struct inet_sock
*sk_inet
;
1848 unsigned char *cipso_ptr
;
1850 struct cipso_v4_doi
*doi_def
;
1852 sk_inet
= inet_sk(sk
);
1853 if (sk_inet
->opt
== NULL
|| sk_inet
->opt
->cipso
== 0)
1855 cipso_ptr
= sk_inet
->opt
->__data
+ sk_inet
->opt
->cipso
-
1856 sizeof(struct iphdr
);
1857 ret_val
= cipso_v4_cache_check(cipso_ptr
, cipso_ptr
[1], secattr
);
1861 doi
= ntohl(*(__be32
*)&cipso_ptr
[2]);
1863 doi_def
= cipso_v4_doi_search(doi
);
1864 if (doi_def
== NULL
) {
1869 /* XXX - This code assumes only one tag per CIPSO option which isn't
1870 * really a good assumption to make but since we only support the MAC
1871 * tags right now it is a safe assumption. */
1872 switch (cipso_ptr
[6]) {
1873 case CIPSO_V4_TAG_RBITMAP
:
1874 ret_val
= cipso_v4_parsetag_rbm(doi_def
,
1878 case CIPSO_V4_TAG_ENUM
:
1879 ret_val
= cipso_v4_parsetag_enum(doi_def
,
1883 case CIPSO_V4_TAG_RANGE
:
1884 ret_val
= cipso_v4_parsetag_rng(doi_def
,
1895 * cipso_v4_socket_getattr - Get the security attributes from a socket
1897 * @secattr: the security attributes
1900 * Query @sock to see if there is a CIPSO option attached to the socket and if
1901 * there is return the CIPSO security attributes in @secattr. Returns zero on
1902 * success and negative values on failure.
1905 int cipso_v4_socket_getattr(const struct socket
*sock
,
1906 struct netlbl_lsm_secattr
*secattr
)
1910 lock_sock(sock
->sk
);
1911 ret_val
= cipso_v4_sock_getattr(sock
->sk
, secattr
);
1912 release_sock(sock
->sk
);
1918 * cipso_v4_skbuff_getattr - Get the security attributes from the CIPSO option
1920 * @secattr: the security attributes
1923 * Parse the given packet's CIPSO option and return the security attributes.
1924 * Returns zero on success and negative values on failure.
1927 int cipso_v4_skbuff_getattr(const struct sk_buff
*skb
,
1928 struct netlbl_lsm_secattr
*secattr
)
1930 int ret_val
= -ENOMSG
;
1931 unsigned char *cipso_ptr
;
1933 struct cipso_v4_doi
*doi_def
;
1935 cipso_ptr
= CIPSO_V4_OPTPTR(skb
);
1936 if (cipso_v4_cache_check(cipso_ptr
, cipso_ptr
[1], secattr
) == 0)
1939 doi
= ntohl(*(__be32
*)&cipso_ptr
[2]);
1941 doi_def
= cipso_v4_doi_search(doi
);
1942 if (doi_def
== NULL
)
1943 goto skbuff_getattr_return
;
1945 /* XXX - This code assumes only one tag per CIPSO option which isn't
1946 * really a good assumption to make but since we only support the MAC
1947 * tags right now it is a safe assumption. */
1948 switch (cipso_ptr
[6]) {
1949 case CIPSO_V4_TAG_RBITMAP
:
1950 ret_val
= cipso_v4_parsetag_rbm(doi_def
,
1954 case CIPSO_V4_TAG_ENUM
:
1955 ret_val
= cipso_v4_parsetag_enum(doi_def
,
1959 case CIPSO_V4_TAG_RANGE
:
1960 ret_val
= cipso_v4_parsetag_rng(doi_def
,
1966 skbuff_getattr_return
:
1976 * cipso_v4_init - Initialize the CIPSO module
1979 * Initialize the CIPSO module and prepare it for use. Returns zero on success
1980 * and negative values on failure.
1983 static int __init
cipso_v4_init(void)
1987 ret_val
= cipso_v4_cache_init();
1989 panic("Failed to initialize the CIPSO/IPv4 cache (%d)\n",
1995 subsys_initcall(cipso_v4_init
);