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, 2008
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>
48 #include <asm/unaligned.h>
50 /* List of available DOI definitions */
51 /* XXX - This currently assumes a minimal number of different DOIs in use,
52 * if in practice there are a lot of different DOIs this list should
53 * probably be turned into a hash table or something similar so we
54 * can do quick lookups. */
55 static DEFINE_SPINLOCK(cipso_v4_doi_list_lock);
56 static LIST_HEAD(cipso_v4_doi_list);
58 /* Label mapping cache */
59 int cipso_v4_cache_enabled = 1;
60 int cipso_v4_cache_bucketsize = 10;
61 #define CIPSO_V4_CACHE_BUCKETBITS 7
62 #define CIPSO_V4_CACHE_BUCKETS (1 << CIPSO_V4_CACHE_BUCKETBITS)
63 #define CIPSO_V4_CACHE_REORDERLIMIT 10
64 struct cipso_v4_map_cache_bkt {
67 struct list_head list;
69 struct cipso_v4_map_cache_entry {
74 struct netlbl_lsm_cache *lsm_data;
77 struct list_head list;
79 static struct cipso_v4_map_cache_bkt *cipso_v4_cache = NULL;
81 /* Restricted bitmap (tag #1) flags */
82 int cipso_v4_rbm_optfmt = 0;
83 int cipso_v4_rbm_strictvalid = 1;
89 /* Maximum size of the CIPSO IP option, derived from the fact that the maximum
90 * IPv4 header size is 60 bytes and the base IPv4 header is 20 bytes long. */
91 #define CIPSO_V4_OPT_LEN_MAX 40
93 /* Length of the base CIPSO option, this includes the option type (1 byte), the
94 * option length (1 byte), and the DOI (4 bytes). */
95 #define CIPSO_V4_HDR_LEN 6
97 /* Base length of the restrictive category bitmap tag (tag #1). */
98 #define CIPSO_V4_TAG_RBM_BLEN 4
100 /* Base length of the enumerated category tag (tag #2). */
101 #define CIPSO_V4_TAG_ENUM_BLEN 4
103 /* Base length of the ranged categories bitmap tag (tag #5). */
104 #define CIPSO_V4_TAG_RNG_BLEN 4
105 /* The maximum number of category ranges permitted in the ranged category tag
106 * (tag #5). You may note that the IETF draft states that the maximum number
107 * of category ranges is 7, but if the low end of the last category range is
108 * zero then it is possibile to fit 8 category ranges because the zero should
110 #define CIPSO_V4_TAG_RNG_CAT_MAX 8
112 /* Base length of the local tag (non-standard tag).
113 * Tag definition (may change between kernel versions)
116 * +----------+----------+----------+----------+
117 * | 10000000 | 00000110 | 32-bit secid value |
118 * +----------+----------+----------+----------+
119 * | in (host byte order)|
120 * +----------+----------+
123 #define CIPSO_V4_TAG_LOC_BLEN 6
130 * cipso_v4_bitmap_walk - Walk a bitmap looking for a bit
131 * @bitmap: the bitmap
132 * @bitmap_len: length in bits
133 * @offset: starting offset
134 * @state: if non-zero, look for a set (1) bit else look for a cleared (0) bit
137 * Starting at @offset, walk the bitmap from left to right until either the
138 * desired bit is found or we reach the end. Return the bit offset, -1 if
139 * not found, or -2 if error.
141 static int cipso_v4_bitmap_walk(const unsigned char *bitmap,
148 unsigned char bitmask;
151 /* gcc always rounds to zero when doing integer division */
152 byte_offset = offset / 8;
153 byte = bitmap[byte_offset];
155 bitmask = 0x80 >> (offset % 8);
157 while (bit_spot < bitmap_len) {
158 if ((state && (byte & bitmask) == bitmask) ||
159 (state == 0 && (byte & bitmask) == 0))
165 byte = bitmap[++byte_offset];
174 * cipso_v4_bitmap_setbit - Sets a single bit in a bitmap
175 * @bitmap: the bitmap
177 * @state: if non-zero, set the bit (1) else clear the bit (0)
180 * Set a single bit in the bitmask. Returns zero on success, negative values
183 static void cipso_v4_bitmap_setbit(unsigned char *bitmap,
190 /* gcc always rounds to zero when doing integer division */
192 bitmask = 0x80 >> (bit % 8);
194 bitmap[byte_spot] |= bitmask;
196 bitmap[byte_spot] &= ~bitmask;
200 * cipso_v4_cache_entry_free - Frees a cache entry
201 * @entry: the entry to free
204 * This function frees the memory associated with a cache entry including the
205 * LSM cache data if there are no longer any users, i.e. reference count == 0.
208 static void cipso_v4_cache_entry_free(struct cipso_v4_map_cache_entry *entry)
211 netlbl_secattr_cache_free(entry->lsm_data);
217 * cipso_v4_map_cache_hash - Hashing function for the CIPSO cache
219 * @key_len: the length of the key in bytes
222 * The CIPSO tag hashing function. Returns a 32-bit hash value.
225 static u32 cipso_v4_map_cache_hash(const unsigned char *key, u32 key_len)
227 return jhash(key, key_len, 0);
231 * Label Mapping Cache Functions
235 * cipso_v4_cache_init - Initialize the CIPSO cache
238 * Initializes the CIPSO label mapping cache, this function should be called
239 * before any of the other functions defined in this file. Returns zero on
240 * success, negative values on error.
243 static int cipso_v4_cache_init(void)
247 cipso_v4_cache = kcalloc(CIPSO_V4_CACHE_BUCKETS,
248 sizeof(struct cipso_v4_map_cache_bkt),
250 if (cipso_v4_cache == NULL)
253 for (iter = 0; iter < CIPSO_V4_CACHE_BUCKETS; iter++) {
254 spin_lock_init(&cipso_v4_cache[iter].lock);
255 cipso_v4_cache[iter].size = 0;
256 INIT_LIST_HEAD(&cipso_v4_cache[iter].list);
263 * cipso_v4_cache_invalidate - Invalidates the current CIPSO cache
266 * Invalidates and frees any entries in the CIPSO cache. Returns zero on
267 * success and negative values on failure.
270 void cipso_v4_cache_invalidate(void)
272 struct cipso_v4_map_cache_entry *entry, *tmp_entry;
275 for (iter = 0; iter < CIPSO_V4_CACHE_BUCKETS; iter++) {
276 spin_lock_bh(&cipso_v4_cache[iter].lock);
277 list_for_each_entry_safe(entry,
279 &cipso_v4_cache[iter].list, list) {
280 list_del(&entry->list);
281 cipso_v4_cache_entry_free(entry);
283 cipso_v4_cache[iter].size = 0;
284 spin_unlock_bh(&cipso_v4_cache[iter].lock);
291 * cipso_v4_cache_check - Check the CIPSO cache for a label mapping
292 * @key: the buffer to check
293 * @key_len: buffer length in bytes
294 * @secattr: the security attribute struct to use
297 * This function checks the cache to see if a label mapping already exists for
298 * the given key. If there is a match then the cache is adjusted and the
299 * @secattr struct is populated with the correct LSM security attributes. The
300 * cache is adjusted in the following manner if the entry is not already the
301 * first in the cache bucket:
303 * 1. The cache entry's activity counter is incremented
304 * 2. The previous (higher ranking) entry's activity counter is decremented
305 * 3. If the difference between the two activity counters is geater than
306 * CIPSO_V4_CACHE_REORDERLIMIT the two entries are swapped
308 * Returns zero on success, -ENOENT for a cache miss, and other negative values
312 static int cipso_v4_cache_check(const unsigned char *key,
314 struct netlbl_lsm_secattr *secattr)
317 struct cipso_v4_map_cache_entry *entry;
318 struct cipso_v4_map_cache_entry *prev_entry = NULL;
321 if (!cipso_v4_cache_enabled)
324 hash = cipso_v4_map_cache_hash(key, key_len);
325 bkt = hash & (CIPSO_V4_CACHE_BUCKETS - 1);
326 spin_lock_bh(&cipso_v4_cache[bkt].lock);
327 list_for_each_entry(entry, &cipso_v4_cache[bkt].list, list) {
328 if (entry->hash == hash &&
329 entry->key_len == key_len &&
330 memcmp(entry->key, key, key_len) == 0) {
331 entry->activity += 1;
332 atomic_inc(&entry->lsm_data->refcount);
333 secattr->cache = entry->lsm_data;
334 secattr->flags |= NETLBL_SECATTR_CACHE;
335 secattr->type = NETLBL_NLTYPE_CIPSOV4;
336 if (prev_entry == NULL) {
337 spin_unlock_bh(&cipso_v4_cache[bkt].lock);
341 if (prev_entry->activity > 0)
342 prev_entry->activity -= 1;
343 if (entry->activity > prev_entry->activity &&
344 entry->activity - prev_entry->activity >
345 CIPSO_V4_CACHE_REORDERLIMIT) {
346 __list_del(entry->list.prev, entry->list.next);
347 __list_add(&entry->list,
348 prev_entry->list.prev,
352 spin_unlock_bh(&cipso_v4_cache[bkt].lock);
357 spin_unlock_bh(&cipso_v4_cache[bkt].lock);
363 * cipso_v4_cache_add - Add an entry to the CIPSO cache
365 * @secattr: the packet's security attributes
368 * Add a new entry into the CIPSO label mapping cache. Add the new entry to
369 * head of the cache bucket's list, if the cache bucket is out of room remove
370 * the last entry in the list first. It is important to note that there is
371 * currently no checking for duplicate keys. Returns zero on success,
372 * negative values on failure.
375 int cipso_v4_cache_add(const struct sk_buff *skb,
376 const struct netlbl_lsm_secattr *secattr)
378 int ret_val = -EPERM;
380 struct cipso_v4_map_cache_entry *entry = NULL;
381 struct cipso_v4_map_cache_entry *old_entry = NULL;
382 unsigned char *cipso_ptr;
385 if (!cipso_v4_cache_enabled || cipso_v4_cache_bucketsize <= 0)
388 cipso_ptr = CIPSO_V4_OPTPTR(skb);
389 cipso_ptr_len = cipso_ptr[1];
391 entry = kzalloc(sizeof(*entry), GFP_ATOMIC);
394 entry->key = kmemdup(cipso_ptr, cipso_ptr_len, GFP_ATOMIC);
395 if (entry->key == NULL) {
397 goto cache_add_failure;
399 entry->key_len = cipso_ptr_len;
400 entry->hash = cipso_v4_map_cache_hash(cipso_ptr, cipso_ptr_len);
401 atomic_inc(&secattr->cache->refcount);
402 entry->lsm_data = secattr->cache;
404 bkt = entry->hash & (CIPSO_V4_CACHE_BUCKETS - 1);
405 spin_lock_bh(&cipso_v4_cache[bkt].lock);
406 if (cipso_v4_cache[bkt].size < cipso_v4_cache_bucketsize) {
407 list_add(&entry->list, &cipso_v4_cache[bkt].list);
408 cipso_v4_cache[bkt].size += 1;
410 old_entry = list_entry(cipso_v4_cache[bkt].list.prev,
411 struct cipso_v4_map_cache_entry, list);
412 list_del(&old_entry->list);
413 list_add(&entry->list, &cipso_v4_cache[bkt].list);
414 cipso_v4_cache_entry_free(old_entry);
416 spin_unlock_bh(&cipso_v4_cache[bkt].lock);
422 cipso_v4_cache_entry_free(entry);
431 * cipso_v4_doi_search - Searches for a DOI definition
432 * @doi: the DOI to search for
435 * Search the DOI definition list for a DOI definition with a DOI value that
436 * matches @doi. The caller is responsibile for calling rcu_read_[un]lock().
437 * Returns a pointer to the DOI definition on success and NULL on failure.
439 static struct cipso_v4_doi *cipso_v4_doi_search(u32 doi)
441 struct cipso_v4_doi *iter;
443 list_for_each_entry_rcu(iter, &cipso_v4_doi_list, list)
444 if (iter->doi == doi && atomic_read(&iter->refcount))
450 * cipso_v4_doi_add - Add a new DOI to the CIPSO protocol engine
451 * @doi_def: the DOI structure
454 * The caller defines a new DOI for use by the CIPSO engine and calls this
455 * function to add it to the list of acceptable domains. The caller must
456 * ensure that the mapping table specified in @doi_def->map meets all of the
457 * requirements of the mapping type (see cipso_ipv4.h for details). Returns
458 * zero on success and non-zero on failure.
461 int cipso_v4_doi_add(struct cipso_v4_doi *doi_def)
465 if (doi_def == NULL || doi_def->doi == CIPSO_V4_DOI_UNKNOWN)
467 for (iter = 0; iter < CIPSO_V4_TAG_MAXCNT; iter++) {
468 switch (doi_def->tags[iter]) {
469 case CIPSO_V4_TAG_RBITMAP:
471 case CIPSO_V4_TAG_RANGE:
472 if (doi_def->type != CIPSO_V4_MAP_PASS)
475 case CIPSO_V4_TAG_INVALID:
479 case CIPSO_V4_TAG_ENUM:
480 if (doi_def->type != CIPSO_V4_MAP_PASS)
483 case CIPSO_V4_TAG_LOCAL:
484 if (doi_def->type != CIPSO_V4_MAP_LOCAL)
492 atomic_set(&doi_def->refcount, 1);
493 INIT_RCU_HEAD(&doi_def->rcu);
495 spin_lock(&cipso_v4_doi_list_lock);
496 if (cipso_v4_doi_search(doi_def->doi) != NULL)
497 goto doi_add_failure;
498 list_add_tail_rcu(&doi_def->list, &cipso_v4_doi_list);
499 spin_unlock(&cipso_v4_doi_list_lock);
504 spin_unlock(&cipso_v4_doi_list_lock);
509 * cipso_v4_doi_free - Frees a DOI definition
510 * @entry: the entry's RCU field
513 * This function frees all of the memory associated with a DOI definition.
516 void cipso_v4_doi_free(struct cipso_v4_doi *doi_def)
521 switch (doi_def->type) {
522 case CIPSO_V4_MAP_TRANS:
523 kfree(doi_def->map.std->lvl.cipso);
524 kfree(doi_def->map.std->lvl.local);
525 kfree(doi_def->map.std->cat.cipso);
526 kfree(doi_def->map.std->cat.local);
533 * cipso_v4_doi_free_rcu - Frees a DOI definition via the RCU pointer
534 * @entry: the entry's RCU field
537 * This function is designed to be used as a callback to the call_rcu()
538 * function so that the memory allocated to the DOI definition can be released
542 static void cipso_v4_doi_free_rcu(struct rcu_head *entry)
544 struct cipso_v4_doi *doi_def;
546 doi_def = container_of(entry, struct cipso_v4_doi, rcu);
547 cipso_v4_doi_free(doi_def);
551 * cipso_v4_doi_remove - Remove an existing DOI from the CIPSO protocol engine
552 * @doi: the DOI value
553 * @audit_secid: the LSM secid to use in the audit message
556 * Removes a DOI definition from the CIPSO engine. The NetLabel routines will
557 * be called to release their own LSM domain mappings as well as our own
558 * domain list. Returns zero on success and negative values on failure.
561 int cipso_v4_doi_remove(u32 doi, struct netlbl_audit *audit_info)
563 struct cipso_v4_doi *doi_def;
565 spin_lock(&cipso_v4_doi_list_lock);
566 doi_def = cipso_v4_doi_search(doi);
567 if (doi_def == NULL) {
568 spin_unlock(&cipso_v4_doi_list_lock);
571 if (!atomic_dec_and_test(&doi_def->refcount)) {
572 spin_unlock(&cipso_v4_doi_list_lock);
575 list_del_rcu(&doi_def->list);
576 spin_unlock(&cipso_v4_doi_list_lock);
578 cipso_v4_cache_invalidate();
579 call_rcu(&doi_def->rcu, cipso_v4_doi_free_rcu);
585 * cipso_v4_doi_getdef - Returns a reference to a valid DOI definition
586 * @doi: the DOI value
589 * Searches for a valid DOI definition and if one is found it is returned to
590 * the caller. Otherwise NULL is returned. The caller must ensure that
591 * rcu_read_lock() is held while accessing the returned definition and the DOI
592 * definition reference count is decremented when the caller is done.
595 struct cipso_v4_doi *cipso_v4_doi_getdef(u32 doi)
597 struct cipso_v4_doi *doi_def;
600 doi_def = cipso_v4_doi_search(doi);
602 goto doi_getdef_return;
603 if (!atomic_inc_not_zero(&doi_def->refcount))
612 * cipso_v4_doi_putdef - Releases a reference for the given DOI definition
613 * @doi_def: the DOI definition
616 * Releases a DOI definition reference obtained from cipso_v4_doi_getdef().
619 void cipso_v4_doi_putdef(struct cipso_v4_doi *doi_def)
624 if (!atomic_dec_and_test(&doi_def->refcount))
626 spin_lock(&cipso_v4_doi_list_lock);
627 list_del_rcu(&doi_def->list);
628 spin_unlock(&cipso_v4_doi_list_lock);
630 cipso_v4_cache_invalidate();
631 call_rcu(&doi_def->rcu, cipso_v4_doi_free_rcu);
635 * cipso_v4_doi_walk - Iterate through the DOI definitions
636 * @skip_cnt: skip past this number of DOI definitions, updated
637 * @callback: callback for each DOI definition
638 * @cb_arg: argument for the callback function
641 * Iterate over the DOI definition list, skipping the first @skip_cnt entries.
642 * For each entry call @callback, if @callback returns a negative value stop
643 * 'walking' through the list and return. Updates the value in @skip_cnt upon
644 * return. Returns zero on success, negative values on failure.
647 int cipso_v4_doi_walk(u32 *skip_cnt,
648 int (*callback) (struct cipso_v4_doi *doi_def, void *arg),
651 int ret_val = -ENOENT;
653 struct cipso_v4_doi *iter_doi;
656 list_for_each_entry_rcu(iter_doi, &cipso_v4_doi_list, list)
657 if (atomic_read(&iter_doi->refcount) > 0) {
658 if (doi_cnt++ < *skip_cnt)
660 ret_val = callback(iter_doi, cb_arg);
663 goto doi_walk_return;
674 * Label Mapping Functions
678 * cipso_v4_map_lvl_valid - Checks to see if the given level is understood
679 * @doi_def: the DOI definition
680 * @level: the level to check
683 * Checks the given level against the given DOI definition and returns a
684 * negative value if the level does not have a valid mapping and a zero value
685 * if the level is defined by the DOI.
688 static int cipso_v4_map_lvl_valid(const struct cipso_v4_doi *doi_def, u8 level)
690 switch (doi_def->type) {
691 case CIPSO_V4_MAP_PASS:
693 case CIPSO_V4_MAP_TRANS:
694 if (doi_def->map.std->lvl.cipso[level] < CIPSO_V4_INV_LVL)
703 * cipso_v4_map_lvl_hton - Perform a level mapping from the host to the network
704 * @doi_def: the DOI definition
705 * @host_lvl: the host MLS level
706 * @net_lvl: the network/CIPSO MLS level
709 * Perform a label mapping to translate a local MLS level to the correct
710 * CIPSO level using the given DOI definition. Returns zero on success,
711 * negative values otherwise.
714 static int cipso_v4_map_lvl_hton(const struct cipso_v4_doi *doi_def,
718 switch (doi_def->type) {
719 case CIPSO_V4_MAP_PASS:
722 case CIPSO_V4_MAP_TRANS:
723 if (host_lvl < doi_def->map.std->lvl.local_size &&
724 doi_def->map.std->lvl.local[host_lvl] < CIPSO_V4_INV_LVL) {
725 *net_lvl = doi_def->map.std->lvl.local[host_lvl];
735 * cipso_v4_map_lvl_ntoh - Perform a level mapping from the network to the host
736 * @doi_def: the DOI definition
737 * @net_lvl: the network/CIPSO MLS level
738 * @host_lvl: the host MLS level
741 * Perform a label mapping to translate a CIPSO level to the correct local MLS
742 * level using the given DOI definition. Returns zero on success, negative
746 static int cipso_v4_map_lvl_ntoh(const struct cipso_v4_doi *doi_def,
750 struct cipso_v4_std_map_tbl *map_tbl;
752 switch (doi_def->type) {
753 case CIPSO_V4_MAP_PASS:
756 case CIPSO_V4_MAP_TRANS:
757 map_tbl = doi_def->map.std;
758 if (net_lvl < map_tbl->lvl.cipso_size &&
759 map_tbl->lvl.cipso[net_lvl] < CIPSO_V4_INV_LVL) {
760 *host_lvl = doi_def->map.std->lvl.cipso[net_lvl];
770 * cipso_v4_map_cat_rbm_valid - Checks to see if the category bitmap is valid
771 * @doi_def: the DOI definition
772 * @bitmap: category bitmap
773 * @bitmap_len: bitmap length in bytes
776 * Checks the given category bitmap against the given DOI definition and
777 * returns a negative value if any of the categories in the bitmap do not have
778 * a valid mapping and a zero value if all of the categories are valid.
781 static int cipso_v4_map_cat_rbm_valid(const struct cipso_v4_doi *doi_def,
782 const unsigned char *bitmap,
786 u32 bitmap_len_bits = bitmap_len * 8;
790 switch (doi_def->type) {
791 case CIPSO_V4_MAP_PASS:
793 case CIPSO_V4_MAP_TRANS:
794 cipso_cat_size = doi_def->map.std->cat.cipso_size;
795 cipso_array = doi_def->map.std->cat.cipso;
797 cat = cipso_v4_bitmap_walk(bitmap,
803 if (cat >= cipso_cat_size ||
804 cipso_array[cat] >= CIPSO_V4_INV_CAT)
817 * cipso_v4_map_cat_rbm_hton - Perform a category mapping from host to network
818 * @doi_def: the DOI definition
819 * @secattr: the security attributes
820 * @net_cat: the zero'd out category bitmap in network/CIPSO format
821 * @net_cat_len: the length of the CIPSO bitmap in bytes
824 * Perform a label mapping to translate a local MLS category bitmap to the
825 * correct CIPSO bitmap using the given DOI definition. Returns the minimum
826 * size in bytes of the network bitmap on success, negative values otherwise.
829 static int cipso_v4_map_cat_rbm_hton(const struct cipso_v4_doi *doi_def,
830 const struct netlbl_lsm_secattr *secattr,
831 unsigned char *net_cat,
835 u32 net_spot = CIPSO_V4_INV_CAT;
836 u32 net_spot_max = 0;
837 u32 net_clen_bits = net_cat_len * 8;
838 u32 host_cat_size = 0;
839 u32 *host_cat_array = NULL;
841 if (doi_def->type == CIPSO_V4_MAP_TRANS) {
842 host_cat_size = doi_def->map.std->cat.local_size;
843 host_cat_array = doi_def->map.std->cat.local;
847 host_spot = netlbl_secattr_catmap_walk(secattr->attr.mls.cat,
852 switch (doi_def->type) {
853 case CIPSO_V4_MAP_PASS:
854 net_spot = host_spot;
856 case CIPSO_V4_MAP_TRANS:
857 if (host_spot >= host_cat_size)
859 net_spot = host_cat_array[host_spot];
860 if (net_spot >= CIPSO_V4_INV_CAT)
864 if (net_spot >= net_clen_bits)
866 cipso_v4_bitmap_setbit(net_cat, net_spot, 1);
868 if (net_spot > net_spot_max)
869 net_spot_max = net_spot;
872 if (++net_spot_max % 8)
873 return net_spot_max / 8 + 1;
874 return net_spot_max / 8;
878 * cipso_v4_map_cat_rbm_ntoh - Perform a category mapping from network to host
879 * @doi_def: the DOI definition
880 * @net_cat: the category bitmap in network/CIPSO format
881 * @net_cat_len: the length of the CIPSO bitmap in bytes
882 * @secattr: the security attributes
885 * Perform a label mapping to translate a CIPSO bitmap to the correct local
886 * MLS category bitmap using the given DOI definition. Returns zero on
887 * success, negative values on failure.
890 static int cipso_v4_map_cat_rbm_ntoh(const struct cipso_v4_doi *doi_def,
891 const unsigned char *net_cat,
893 struct netlbl_lsm_secattr *secattr)
897 u32 host_spot = CIPSO_V4_INV_CAT;
898 u32 net_clen_bits = net_cat_len * 8;
899 u32 net_cat_size = 0;
900 u32 *net_cat_array = NULL;
902 if (doi_def->type == CIPSO_V4_MAP_TRANS) {
903 net_cat_size = doi_def->map.std->cat.cipso_size;
904 net_cat_array = doi_def->map.std->cat.cipso;
908 net_spot = cipso_v4_bitmap_walk(net_cat,
918 switch (doi_def->type) {
919 case CIPSO_V4_MAP_PASS:
920 host_spot = net_spot;
922 case CIPSO_V4_MAP_TRANS:
923 if (net_spot >= net_cat_size)
925 host_spot = net_cat_array[net_spot];
926 if (host_spot >= CIPSO_V4_INV_CAT)
930 ret_val = netlbl_secattr_catmap_setbit(secattr->attr.mls.cat,
941 * cipso_v4_map_cat_enum_valid - Checks to see if the categories are valid
942 * @doi_def: the DOI definition
943 * @enumcat: category list
944 * @enumcat_len: length of the category list in bytes
947 * Checks the given categories against the given DOI definition and returns a
948 * negative value if any of the categories do not have a valid mapping and a
949 * zero value if all of the categories are valid.
952 static int cipso_v4_map_cat_enum_valid(const struct cipso_v4_doi *doi_def,
953 const unsigned char *enumcat,
960 if (doi_def->type != CIPSO_V4_MAP_PASS || enumcat_len & 0x01)
963 for (iter = 0; iter < enumcat_len; iter += 2) {
964 cat = get_unaligned_be16(&enumcat[iter]);
974 * cipso_v4_map_cat_enum_hton - Perform a category mapping from host to network
975 * @doi_def: the DOI definition
976 * @secattr: the security attributes
977 * @net_cat: the zero'd out category list in network/CIPSO format
978 * @net_cat_len: the length of the CIPSO category list in bytes
981 * Perform a label mapping to translate a local MLS category bitmap to the
982 * correct CIPSO category list using the given DOI definition. Returns the
983 * size in bytes of the network category bitmap on success, negative values
987 static int cipso_v4_map_cat_enum_hton(const struct cipso_v4_doi *doi_def,
988 const struct netlbl_lsm_secattr *secattr,
989 unsigned char *net_cat,
996 cat = netlbl_secattr_catmap_walk(secattr->attr.mls.cat,
1000 if ((cat_iter + 2) > net_cat_len)
1003 *((__be16 *)&net_cat[cat_iter]) = htons(cat);
1011 * cipso_v4_map_cat_enum_ntoh - Perform a category mapping from network to host
1012 * @doi_def: the DOI definition
1013 * @net_cat: the category list in network/CIPSO format
1014 * @net_cat_len: the length of the CIPSO bitmap in bytes
1015 * @secattr: the security attributes
1018 * Perform a label mapping to translate a CIPSO category list to the correct
1019 * local MLS category bitmap using the given DOI definition. Returns zero on
1020 * success, negative values on failure.
1023 static int cipso_v4_map_cat_enum_ntoh(const struct cipso_v4_doi *doi_def,
1024 const unsigned char *net_cat,
1026 struct netlbl_lsm_secattr *secattr)
1031 for (iter = 0; iter < net_cat_len; iter += 2) {
1032 ret_val = netlbl_secattr_catmap_setbit(secattr->attr.mls.cat,
1033 get_unaligned_be16(&net_cat[iter]),
1043 * cipso_v4_map_cat_rng_valid - Checks to see if the categories are valid
1044 * @doi_def: the DOI definition
1045 * @rngcat: category list
1046 * @rngcat_len: length of the category list in bytes
1049 * Checks the given categories against the given DOI definition and returns a
1050 * negative value if any of the categories do not have a valid mapping and a
1051 * zero value if all of the categories are valid.
1054 static int cipso_v4_map_cat_rng_valid(const struct cipso_v4_doi *doi_def,
1055 const unsigned char *rngcat,
1060 u32 cat_prev = CIPSO_V4_MAX_REM_CATS + 1;
1063 if (doi_def->type != CIPSO_V4_MAP_PASS || rngcat_len & 0x01)
1066 for (iter = 0; iter < rngcat_len; iter += 4) {
1067 cat_high = get_unaligned_be16(&rngcat[iter]);
1068 if ((iter + 4) <= rngcat_len)
1069 cat_low = get_unaligned_be16(&rngcat[iter + 2]);
1073 if (cat_high > cat_prev)
1083 * cipso_v4_map_cat_rng_hton - Perform a category mapping from host to network
1084 * @doi_def: the DOI definition
1085 * @secattr: the security attributes
1086 * @net_cat: the zero'd out category list in network/CIPSO format
1087 * @net_cat_len: the length of the CIPSO category list in bytes
1090 * Perform a label mapping to translate a local MLS category bitmap to the
1091 * correct CIPSO category list using the given DOI definition. Returns the
1092 * size in bytes of the network category bitmap on success, negative values
1096 static int cipso_v4_map_cat_rng_hton(const struct cipso_v4_doi *doi_def,
1097 const struct netlbl_lsm_secattr *secattr,
1098 unsigned char *net_cat,
1102 u16 array[CIPSO_V4_TAG_RNG_CAT_MAX * 2];
1106 /* make sure we don't overflow the 'array[]' variable */
1108 (CIPSO_V4_OPT_LEN_MAX - CIPSO_V4_HDR_LEN - CIPSO_V4_TAG_RNG_BLEN))
1112 iter = netlbl_secattr_catmap_walk(secattr->attr.mls.cat,
1116 cat_size += (iter == 0 ? 0 : sizeof(u16));
1117 if (cat_size > net_cat_len)
1119 array[array_cnt++] = iter;
1121 iter = netlbl_secattr_catmap_walk_rng(secattr->attr.mls.cat,
1125 cat_size += sizeof(u16);
1126 if (cat_size > net_cat_len)
1128 array[array_cnt++] = iter;
1131 for (iter = 0; array_cnt > 0;) {
1132 *((__be16 *)&net_cat[iter]) = htons(array[--array_cnt]);
1135 if (array[array_cnt] != 0) {
1136 *((__be16 *)&net_cat[iter]) = htons(array[array_cnt]);
1145 * cipso_v4_map_cat_rng_ntoh - Perform a category mapping from network to host
1146 * @doi_def: the DOI definition
1147 * @net_cat: the category list in network/CIPSO format
1148 * @net_cat_len: the length of the CIPSO bitmap in bytes
1149 * @secattr: the security attributes
1152 * Perform a label mapping to translate a CIPSO category list to the correct
1153 * local MLS category bitmap using the given DOI definition. Returns zero on
1154 * success, negative values on failure.
1157 static int cipso_v4_map_cat_rng_ntoh(const struct cipso_v4_doi *doi_def,
1158 const unsigned char *net_cat,
1160 struct netlbl_lsm_secattr *secattr)
1167 for (net_iter = 0; net_iter < net_cat_len; net_iter += 4) {
1168 cat_high = get_unaligned_be16(&net_cat[net_iter]);
1169 if ((net_iter + 4) <= net_cat_len)
1170 cat_low = get_unaligned_be16(&net_cat[net_iter + 2]);
1174 ret_val = netlbl_secattr_catmap_setrng(secattr->attr.mls.cat,
1186 * Protocol Handling Functions
1190 * cipso_v4_gentag_hdr - Generate a CIPSO option header
1191 * @doi_def: the DOI definition
1192 * @len: the total tag length in bytes, not including this header
1193 * @buf: the CIPSO option buffer
1196 * Write a CIPSO header into the beginning of @buffer.
1199 static void cipso_v4_gentag_hdr(const struct cipso_v4_doi *doi_def,
1203 buf[0] = IPOPT_CIPSO;
1204 buf[1] = CIPSO_V4_HDR_LEN + len;
1205 *(__be32 *)&buf[2] = htonl(doi_def->doi);
1209 * cipso_v4_gentag_rbm - Generate a CIPSO restricted bitmap tag (type #1)
1210 * @doi_def: the DOI definition
1211 * @secattr: the security attributes
1212 * @buffer: the option buffer
1213 * @buffer_len: length of buffer in bytes
1216 * Generate a CIPSO option using the restricted bitmap tag, tag type #1. The
1217 * actual buffer length may be larger than the indicated size due to
1218 * translation between host and network category bitmaps. Returns the size of
1219 * the tag on success, negative values on failure.
1222 static int cipso_v4_gentag_rbm(const struct cipso_v4_doi *doi_def,
1223 const struct netlbl_lsm_secattr *secattr,
1224 unsigned char *buffer,
1231 if ((secattr->flags & NETLBL_SECATTR_MLS_LVL) == 0)
1234 ret_val = cipso_v4_map_lvl_hton(doi_def,
1235 secattr->attr.mls.lvl,
1240 if (secattr->flags & NETLBL_SECATTR_MLS_CAT) {
1241 ret_val = cipso_v4_map_cat_rbm_hton(doi_def,
1248 /* This will send packets using the "optimized" format when
1249 * possibile as specified in section 3.4.2.6 of the
1251 if (cipso_v4_rbm_optfmt && ret_val > 0 && ret_val <= 10)
1254 tag_len = 4 + ret_val;
1258 buffer[0] = CIPSO_V4_TAG_RBITMAP;
1259 buffer[1] = tag_len;
1266 * cipso_v4_parsetag_rbm - Parse a CIPSO restricted bitmap tag
1267 * @doi_def: the DOI definition
1268 * @tag: the CIPSO tag
1269 * @secattr: the security attributes
1272 * Parse a CIPSO restricted bitmap tag (tag type #1) and return the security
1273 * attributes in @secattr. Return zero on success, negatives values on
1277 static int cipso_v4_parsetag_rbm(const struct cipso_v4_doi *doi_def,
1278 const unsigned char *tag,
1279 struct netlbl_lsm_secattr *secattr)
1282 u8 tag_len = tag[1];
1285 ret_val = cipso_v4_map_lvl_ntoh(doi_def, tag[3], &level);
1288 secattr->attr.mls.lvl = level;
1289 secattr->flags |= NETLBL_SECATTR_MLS_LVL;
1292 secattr->attr.mls.cat =
1293 netlbl_secattr_catmap_alloc(GFP_ATOMIC);
1294 if (secattr->attr.mls.cat == NULL)
1297 ret_val = cipso_v4_map_cat_rbm_ntoh(doi_def,
1302 netlbl_secattr_catmap_free(secattr->attr.mls.cat);
1306 secattr->flags |= NETLBL_SECATTR_MLS_CAT;
1313 * cipso_v4_gentag_enum - Generate a CIPSO enumerated tag (type #2)
1314 * @doi_def: the DOI definition
1315 * @secattr: the security attributes
1316 * @buffer: the option buffer
1317 * @buffer_len: length of buffer in bytes
1320 * Generate a CIPSO option using the enumerated tag, tag type #2. Returns the
1321 * size of the tag on success, negative values on failure.
1324 static int cipso_v4_gentag_enum(const struct cipso_v4_doi *doi_def,
1325 const struct netlbl_lsm_secattr *secattr,
1326 unsigned char *buffer,
1333 if (!(secattr->flags & NETLBL_SECATTR_MLS_LVL))
1336 ret_val = cipso_v4_map_lvl_hton(doi_def,
1337 secattr->attr.mls.lvl,
1342 if (secattr->flags & NETLBL_SECATTR_MLS_CAT) {
1343 ret_val = cipso_v4_map_cat_enum_hton(doi_def,
1350 tag_len = 4 + ret_val;
1354 buffer[0] = CIPSO_V4_TAG_ENUM;
1355 buffer[1] = tag_len;
1362 * cipso_v4_parsetag_enum - Parse a CIPSO enumerated tag
1363 * @doi_def: the DOI definition
1364 * @tag: the CIPSO tag
1365 * @secattr: the security attributes
1368 * Parse a CIPSO enumerated tag (tag type #2) and return the security
1369 * attributes in @secattr. Return zero on success, negatives values on
1373 static int cipso_v4_parsetag_enum(const struct cipso_v4_doi *doi_def,
1374 const unsigned char *tag,
1375 struct netlbl_lsm_secattr *secattr)
1378 u8 tag_len = tag[1];
1381 ret_val = cipso_v4_map_lvl_ntoh(doi_def, tag[3], &level);
1384 secattr->attr.mls.lvl = level;
1385 secattr->flags |= NETLBL_SECATTR_MLS_LVL;
1388 secattr->attr.mls.cat =
1389 netlbl_secattr_catmap_alloc(GFP_ATOMIC);
1390 if (secattr->attr.mls.cat == NULL)
1393 ret_val = cipso_v4_map_cat_enum_ntoh(doi_def,
1398 netlbl_secattr_catmap_free(secattr->attr.mls.cat);
1402 secattr->flags |= NETLBL_SECATTR_MLS_CAT;
1409 * cipso_v4_gentag_rng - Generate a CIPSO ranged tag (type #5)
1410 * @doi_def: the DOI definition
1411 * @secattr: the security attributes
1412 * @buffer: the option buffer
1413 * @buffer_len: length of buffer in bytes
1416 * Generate a CIPSO option using the ranged tag, tag type #5. Returns the
1417 * size of the tag on success, negative values on failure.
1420 static int cipso_v4_gentag_rng(const struct cipso_v4_doi *doi_def,
1421 const struct netlbl_lsm_secattr *secattr,
1422 unsigned char *buffer,
1429 if (!(secattr->flags & NETLBL_SECATTR_MLS_LVL))
1432 ret_val = cipso_v4_map_lvl_hton(doi_def,
1433 secattr->attr.mls.lvl,
1438 if (secattr->flags & NETLBL_SECATTR_MLS_CAT) {
1439 ret_val = cipso_v4_map_cat_rng_hton(doi_def,
1446 tag_len = 4 + ret_val;
1450 buffer[0] = CIPSO_V4_TAG_RANGE;
1451 buffer[1] = tag_len;
1458 * cipso_v4_parsetag_rng - Parse a CIPSO ranged tag
1459 * @doi_def: the DOI definition
1460 * @tag: the CIPSO tag
1461 * @secattr: the security attributes
1464 * Parse a CIPSO ranged tag (tag type #5) and return the security attributes
1465 * in @secattr. Return zero on success, negatives values on failure.
1468 static int cipso_v4_parsetag_rng(const struct cipso_v4_doi *doi_def,
1469 const unsigned char *tag,
1470 struct netlbl_lsm_secattr *secattr)
1473 u8 tag_len = tag[1];
1476 ret_val = cipso_v4_map_lvl_ntoh(doi_def, tag[3], &level);
1479 secattr->attr.mls.lvl = level;
1480 secattr->flags |= NETLBL_SECATTR_MLS_LVL;
1483 secattr->attr.mls.cat =
1484 netlbl_secattr_catmap_alloc(GFP_ATOMIC);
1485 if (secattr->attr.mls.cat == NULL)
1488 ret_val = cipso_v4_map_cat_rng_ntoh(doi_def,
1493 netlbl_secattr_catmap_free(secattr->attr.mls.cat);
1497 secattr->flags |= NETLBL_SECATTR_MLS_CAT;
1504 * cipso_v4_gentag_loc - Generate a CIPSO local tag (non-standard)
1505 * @doi_def: the DOI definition
1506 * @secattr: the security attributes
1507 * @buffer: the option buffer
1508 * @buffer_len: length of buffer in bytes
1511 * Generate a CIPSO option using the local tag. Returns the size of the tag
1512 * on success, negative values on failure.
1515 static int cipso_v4_gentag_loc(const struct cipso_v4_doi *doi_def,
1516 const struct netlbl_lsm_secattr *secattr,
1517 unsigned char *buffer,
1520 if (!(secattr->flags & NETLBL_SECATTR_SECID))
1523 buffer[0] = CIPSO_V4_TAG_LOCAL;
1524 buffer[1] = CIPSO_V4_TAG_LOC_BLEN;
1525 *(u32 *)&buffer[2] = secattr->attr.secid;
1527 return CIPSO_V4_TAG_LOC_BLEN;
1531 * cipso_v4_parsetag_loc - Parse a CIPSO local tag
1532 * @doi_def: the DOI definition
1533 * @tag: the CIPSO tag
1534 * @secattr: the security attributes
1537 * Parse a CIPSO local tag and return the security attributes in @secattr.
1538 * Return zero on success, negatives values on failure.
1541 static int cipso_v4_parsetag_loc(const struct cipso_v4_doi *doi_def,
1542 const unsigned char *tag,
1543 struct netlbl_lsm_secattr *secattr)
1545 secattr->attr.secid = *(u32 *)&tag[2];
1546 secattr->flags |= NETLBL_SECATTR_SECID;
1552 * cipso_v4_validate - Validate a CIPSO option
1553 * @option: the start of the option, on error it is set to point to the error
1556 * This routine is called to validate a CIPSO option, it checks all of the
1557 * fields to ensure that they are at least valid, see the draft snippet below
1558 * for details. If the option is valid then a zero value is returned and
1559 * the value of @option is unchanged. If the option is invalid then a
1560 * non-zero value is returned and @option is adjusted to point to the
1561 * offending portion of the option. From the IETF draft ...
1563 * "If any field within the CIPSO options, such as the DOI identifier, is not
1564 * recognized the IP datagram is discarded and an ICMP 'parameter problem'
1565 * (type 12) is generated and returned. The ICMP code field is set to 'bad
1566 * parameter' (code 0) and the pointer is set to the start of the CIPSO field
1567 * that is unrecognized."
1570 int cipso_v4_validate(const struct sk_buff *skb, unsigned char **option)
1572 unsigned char *opt = *option;
1574 unsigned char opt_iter;
1575 unsigned char err_offset = 0;
1578 struct cipso_v4_doi *doi_def = NULL;
1581 /* caller already checks for length values that are too large */
1585 goto validate_return;
1589 doi_def = cipso_v4_doi_search(get_unaligned_be32(&opt[2]));
1590 if (doi_def == NULL) {
1592 goto validate_return_locked;
1595 opt_iter = CIPSO_V4_HDR_LEN;
1596 tag = opt + opt_iter;
1597 while (opt_iter < opt_len) {
1598 for (tag_iter = 0; doi_def->tags[tag_iter] != tag[0];)
1599 if (doi_def->tags[tag_iter] == CIPSO_V4_TAG_INVALID ||
1600 ++tag_iter == CIPSO_V4_TAG_MAXCNT) {
1601 err_offset = opt_iter;
1602 goto validate_return_locked;
1606 if (tag_len > (opt_len - opt_iter)) {
1607 err_offset = opt_iter + 1;
1608 goto validate_return_locked;
1612 case CIPSO_V4_TAG_RBITMAP:
1613 if (tag_len < CIPSO_V4_TAG_RBM_BLEN) {
1614 err_offset = opt_iter + 1;
1615 goto validate_return_locked;
1618 /* We are already going to do all the verification
1619 * necessary at the socket layer so from our point of
1620 * view it is safe to turn these checks off (and less
1621 * work), however, the CIPSO draft says we should do
1622 * all the CIPSO validations here but it doesn't
1623 * really specify _exactly_ what we need to validate
1624 * ... so, just make it a sysctl tunable. */
1625 if (cipso_v4_rbm_strictvalid) {
1626 if (cipso_v4_map_lvl_valid(doi_def,
1628 err_offset = opt_iter + 3;
1629 goto validate_return_locked;
1631 if (tag_len > CIPSO_V4_TAG_RBM_BLEN &&
1632 cipso_v4_map_cat_rbm_valid(doi_def,
1635 err_offset = opt_iter + 4;
1636 goto validate_return_locked;
1640 case CIPSO_V4_TAG_ENUM:
1641 if (tag_len < CIPSO_V4_TAG_ENUM_BLEN) {
1642 err_offset = opt_iter + 1;
1643 goto validate_return_locked;
1646 if (cipso_v4_map_lvl_valid(doi_def,
1648 err_offset = opt_iter + 3;
1649 goto validate_return_locked;
1651 if (tag_len > CIPSO_V4_TAG_ENUM_BLEN &&
1652 cipso_v4_map_cat_enum_valid(doi_def,
1655 err_offset = opt_iter + 4;
1656 goto validate_return_locked;
1659 case CIPSO_V4_TAG_RANGE:
1660 if (tag_len < CIPSO_V4_TAG_RNG_BLEN) {
1661 err_offset = opt_iter + 1;
1662 goto validate_return_locked;
1665 if (cipso_v4_map_lvl_valid(doi_def,
1667 err_offset = opt_iter + 3;
1668 goto validate_return_locked;
1670 if (tag_len > CIPSO_V4_TAG_RNG_BLEN &&
1671 cipso_v4_map_cat_rng_valid(doi_def,
1674 err_offset = opt_iter + 4;
1675 goto validate_return_locked;
1678 case CIPSO_V4_TAG_LOCAL:
1679 /* This is a non-standard tag that we only allow for
1680 * local connections, so if the incoming interface is
1681 * not the loopback device drop the packet. */
1682 if (!(skb->dev->flags & IFF_LOOPBACK)) {
1683 err_offset = opt_iter;
1684 goto validate_return_locked;
1686 if (tag_len != CIPSO_V4_TAG_LOC_BLEN) {
1687 err_offset = opt_iter + 1;
1688 goto validate_return_locked;
1692 err_offset = opt_iter;
1693 goto validate_return_locked;
1697 opt_iter += tag_len;
1700 validate_return_locked:
1703 *option = opt + err_offset;
1708 * cipso_v4_error - Send the correct reponse for a bad packet
1710 * @error: the error code
1711 * @gateway: CIPSO gateway flag
1714 * Based on the error code given in @error, send an ICMP error message back to
1715 * the originating host. From the IETF draft ...
1717 * "If the contents of the CIPSO [option] are valid but the security label is
1718 * outside of the configured host or port label range, the datagram is
1719 * discarded and an ICMP 'destination unreachable' (type 3) is generated and
1720 * returned. The code field of the ICMP is set to 'communication with
1721 * destination network administratively prohibited' (code 9) or to
1722 * 'communication with destination host administratively prohibited'
1723 * (code 10). The value of the code is dependent on whether the originator
1724 * of the ICMP message is acting as a CIPSO host or a CIPSO gateway. The
1725 * recipient of the ICMP message MUST be able to handle either value. The
1726 * same procedure is performed if a CIPSO [option] can not be added to an
1727 * IP packet because it is too large to fit in the IP options area."
1729 * "If the error is triggered by receipt of an ICMP message, the message is
1730 * discarded and no response is permitted (consistent with general ICMP
1731 * processing rules)."
1734 void cipso_v4_error(struct sk_buff *skb, int error, u32 gateway)
1736 if (ip_hdr(skb)->protocol == IPPROTO_ICMP || error != -EACCES)
1740 icmp_send(skb, ICMP_DEST_UNREACH, ICMP_NET_ANO, 0);
1742 icmp_send(skb, ICMP_DEST_UNREACH, ICMP_HOST_ANO, 0);
1746 * cipso_v4_genopt - Generate a CIPSO option
1747 * @buf: the option buffer
1748 * @buf_len: the size of opt_buf
1749 * @doi_def: the CIPSO DOI to use
1750 * @secattr: the security attributes
1753 * Generate a CIPSO option using the DOI definition and security attributes
1754 * passed to the function. Returns the length of the option on success and
1755 * negative values on failure.
1758 static int cipso_v4_genopt(unsigned char *buf, u32 buf_len,
1759 const struct cipso_v4_doi *doi_def,
1760 const struct netlbl_lsm_secattr *secattr)
1765 if (buf_len <= CIPSO_V4_HDR_LEN)
1768 /* XXX - This code assumes only one tag per CIPSO option which isn't
1769 * really a good assumption to make but since we only support the MAC
1770 * tags right now it is a safe assumption. */
1773 memset(buf, 0, buf_len);
1774 switch (doi_def->tags[iter]) {
1775 case CIPSO_V4_TAG_RBITMAP:
1776 ret_val = cipso_v4_gentag_rbm(doi_def,
1778 &buf[CIPSO_V4_HDR_LEN],
1779 buf_len - CIPSO_V4_HDR_LEN);
1781 case CIPSO_V4_TAG_ENUM:
1782 ret_val = cipso_v4_gentag_enum(doi_def,
1784 &buf[CIPSO_V4_HDR_LEN],
1785 buf_len - CIPSO_V4_HDR_LEN);
1787 case CIPSO_V4_TAG_RANGE:
1788 ret_val = cipso_v4_gentag_rng(doi_def,
1790 &buf[CIPSO_V4_HDR_LEN],
1791 buf_len - CIPSO_V4_HDR_LEN);
1793 case CIPSO_V4_TAG_LOCAL:
1794 ret_val = cipso_v4_gentag_loc(doi_def,
1796 &buf[CIPSO_V4_HDR_LEN],
1797 buf_len - CIPSO_V4_HDR_LEN);
1804 } while (ret_val < 0 &&
1805 iter < CIPSO_V4_TAG_MAXCNT &&
1806 doi_def->tags[iter] != CIPSO_V4_TAG_INVALID);
1809 cipso_v4_gentag_hdr(doi_def, buf, ret_val);
1810 return CIPSO_V4_HDR_LEN + ret_val;
1814 * cipso_v4_sock_setattr - Add a CIPSO option to a socket
1816 * @doi_def: the CIPSO DOI to use
1817 * @secattr: the specific security attributes of the socket
1820 * Set the CIPSO option on the given socket using the DOI definition and
1821 * security attributes passed to the function. This function requires
1822 * exclusive access to @sk, which means it either needs to be in the
1823 * process of being created or locked. Returns zero on success and negative
1824 * values on failure.
1827 int cipso_v4_sock_setattr(struct sock *sk,
1828 const struct cipso_v4_doi *doi_def,
1829 const struct netlbl_lsm_secattr *secattr)
1831 int ret_val = -EPERM;
1832 unsigned char *buf = NULL;
1835 struct ip_options *opt = NULL;
1836 struct inet_sock *sk_inet;
1837 struct inet_connection_sock *sk_conn;
1839 /* In the case of sock_create_lite(), the sock->sk field is not
1840 * defined yet but it is not a problem as the only users of these
1841 * "lite" PF_INET sockets are functions which do an accept() call
1842 * afterwards so we will label the socket as part of the accept(). */
1846 /* We allocate the maximum CIPSO option size here so we are probably
1847 * being a little wasteful, but it makes our life _much_ easier later
1848 * on and after all we are only talking about 40 bytes. */
1849 buf_len = CIPSO_V4_OPT_LEN_MAX;
1850 buf = kmalloc(buf_len, GFP_ATOMIC);
1853 goto socket_setattr_failure;
1856 ret_val = cipso_v4_genopt(buf, buf_len, doi_def, secattr);
1858 goto socket_setattr_failure;
1861 /* We can't use ip_options_get() directly because it makes a call to
1862 * ip_options_get_alloc() which allocates memory with GFP_KERNEL and
1863 * we won't always have CAP_NET_RAW even though we _always_ want to
1864 * set the IPOPT_CIPSO option. */
1865 opt_len = (buf_len + 3) & ~3;
1866 opt = kzalloc(sizeof(*opt) + opt_len, GFP_ATOMIC);
1869 goto socket_setattr_failure;
1871 memcpy(opt->__data, buf, buf_len);
1872 opt->optlen = opt_len;
1873 opt->cipso = sizeof(struct iphdr);
1877 sk_inet = inet_sk(sk);
1878 if (sk_inet->is_icsk) {
1879 sk_conn = inet_csk(sk);
1881 sk_conn->icsk_ext_hdr_len -= sk_inet->opt->optlen;
1882 sk_conn->icsk_ext_hdr_len += opt->optlen;
1883 sk_conn->icsk_sync_mss(sk, sk_conn->icsk_pmtu_cookie);
1885 opt = xchg(&sk_inet->opt, opt);
1890 socket_setattr_failure:
1897 * cipso_v4_sock_delattr - Delete the CIPSO option from a socket
1901 * Removes the CIPSO option from a socket, if present.
1904 void cipso_v4_sock_delattr(struct sock *sk)
1907 struct ip_options *opt;
1908 struct inet_sock *sk_inet;
1910 sk_inet = inet_sk(sk);
1912 if (opt == NULL || opt->cipso == 0)
1915 if (opt->srr || opt->rr || opt->ts || opt->router_alert) {
1918 unsigned char *cipso_ptr;
1922 cipso_off = opt->cipso - sizeof(struct iphdr);
1923 cipso_ptr = &opt->__data[cipso_off];
1924 cipso_len = cipso_ptr[1];
1926 if (opt->srr > opt->cipso)
1927 opt->srr -= cipso_len;
1928 if (opt->rr > opt->cipso)
1929 opt->rr -= cipso_len;
1930 if (opt->ts > opt->cipso)
1931 opt->ts -= cipso_len;
1932 if (opt->router_alert > opt->cipso)
1933 opt->router_alert -= cipso_len;
1936 memmove(cipso_ptr, cipso_ptr + cipso_len,
1937 opt->optlen - cipso_off - cipso_len);
1939 /* determining the new total option length is tricky because of
1940 * the padding necessary, the only thing i can think to do at
1941 * this point is walk the options one-by-one, skipping the
1942 * padding at the end to determine the actual option size and
1943 * from there we can determine the new total option length */
1946 while (iter < opt->optlen)
1947 if (opt->__data[iter] != IPOPT_NOP) {
1948 iter += opt->__data[iter + 1];
1952 hdr_delta = opt->optlen;
1953 opt->optlen = (optlen_new + 3) & ~3;
1954 hdr_delta -= opt->optlen;
1956 /* only the cipso option was present on the socket so we can
1957 * remove the entire option struct */
1958 sk_inet->opt = NULL;
1959 hdr_delta = opt->optlen;
1963 if (sk_inet->is_icsk && hdr_delta > 0) {
1964 struct inet_connection_sock *sk_conn = inet_csk(sk);
1965 sk_conn->icsk_ext_hdr_len -= hdr_delta;
1966 sk_conn->icsk_sync_mss(sk, sk_conn->icsk_pmtu_cookie);
1971 * cipso_v4_getattr - Helper function for the cipso_v4_*_getattr functions
1972 * @cipso: the CIPSO v4 option
1973 * @secattr: the security attributes
1976 * Inspect @cipso and return the security attributes in @secattr. Returns zero
1977 * on success and negative values on failure.
1980 static int cipso_v4_getattr(const unsigned char *cipso,
1981 struct netlbl_lsm_secattr *secattr)
1983 int ret_val = -ENOMSG;
1985 struct cipso_v4_doi *doi_def;
1987 if (cipso_v4_cache_check(cipso, cipso[1], secattr) == 0)
1990 doi = get_unaligned_be32(&cipso[2]);
1992 doi_def = cipso_v4_doi_search(doi);
1993 if (doi_def == NULL)
1994 goto getattr_return;
1995 /* XXX - This code assumes only one tag per CIPSO option which isn't
1996 * really a good assumption to make but since we only support the MAC
1997 * tags right now it is a safe assumption. */
1999 case CIPSO_V4_TAG_RBITMAP:
2000 ret_val = cipso_v4_parsetag_rbm(doi_def, &cipso[6], secattr);
2002 case CIPSO_V4_TAG_ENUM:
2003 ret_val = cipso_v4_parsetag_enum(doi_def, &cipso[6], secattr);
2005 case CIPSO_V4_TAG_RANGE:
2006 ret_val = cipso_v4_parsetag_rng(doi_def, &cipso[6], secattr);
2008 case CIPSO_V4_TAG_LOCAL:
2009 ret_val = cipso_v4_parsetag_loc(doi_def, &cipso[6], secattr);
2013 secattr->type = NETLBL_NLTYPE_CIPSOV4;
2021 * cipso_v4_sock_getattr - Get the security attributes from a sock
2023 * @secattr: the security attributes
2026 * Query @sk to see if there is a CIPSO option attached to the sock and if
2027 * there is return the CIPSO security attributes in @secattr. This function
2028 * requires that @sk be locked, or privately held, but it does not do any
2029 * locking itself. Returns zero on success and negative values on failure.
2032 int cipso_v4_sock_getattr(struct sock *sk, struct netlbl_lsm_secattr *secattr)
2034 struct ip_options *opt;
2036 opt = inet_sk(sk)->opt;
2037 if (opt == NULL || opt->cipso == 0)
2040 return cipso_v4_getattr(opt->__data + opt->cipso - sizeof(struct iphdr),
2045 * cipso_v4_skbuff_setattr - Set the CIPSO option on a packet
2047 * @secattr: the security attributes
2050 * Set the CIPSO option on the given packet based on the security attributes.
2051 * Returns a pointer to the IP header on success and NULL on failure.
2054 int cipso_v4_skbuff_setattr(struct sk_buff *skb,
2055 const struct cipso_v4_doi *doi_def,
2056 const struct netlbl_lsm_secattr *secattr)
2060 struct ip_options *opt = &IPCB(skb)->opt;
2061 unsigned char buf[CIPSO_V4_OPT_LEN_MAX];
2062 u32 buf_len = CIPSO_V4_OPT_LEN_MAX;
2066 buf_len = cipso_v4_genopt(buf, buf_len, doi_def, secattr);
2069 opt_len = (buf_len + 3) & ~3;
2071 /* we overwrite any existing options to ensure that we have enough
2072 * room for the CIPSO option, the reason is that we _need_ to guarantee
2073 * that the security label is applied to the packet - we do the same
2074 * thing when using the socket options and it hasn't caused a problem,
2075 * if we need to we can always revisit this choice later */
2077 len_delta = opt_len - opt->optlen;
2078 /* if we don't ensure enough headroom we could panic on the skb_push()
2079 * call below so make sure we have enough, we are also "mangling" the
2080 * packet so we should probably do a copy-on-write call anyway */
2081 ret_val = skb_cow(skb, skb_headroom(skb) + len_delta);
2085 if (len_delta > 0) {
2086 /* we assume that the header + opt->optlen have already been
2087 * "pushed" in ip_options_build() or similar */
2089 skb_push(skb, len_delta);
2090 memmove((char *)iph - len_delta, iph, iph->ihl << 2);
2091 skb_reset_network_header(skb);
2093 } else if (len_delta < 0) {
2095 memset(iph + 1, IPOPT_NOP, opt->optlen);
2099 if (opt->optlen > 0)
2100 memset(opt, 0, sizeof(*opt));
2101 opt->optlen = opt_len;
2102 opt->cipso = sizeof(struct iphdr);
2103 opt->is_changed = 1;
2105 /* we have to do the following because we are being called from a
2106 * netfilter hook which means the packet already has had the header
2107 * fields populated and the checksum calculated - yes this means we
2108 * are doing more work than needed but we do it to keep the core
2109 * stack clean and tidy */
2110 memcpy(iph + 1, buf, buf_len);
2111 if (opt_len > buf_len)
2112 memset((char *)(iph + 1) + buf_len, 0, opt_len - buf_len);
2113 if (len_delta != 0) {
2114 iph->ihl = 5 + (opt_len >> 2);
2115 iph->tot_len = htons(skb->len);
2123 * cipso_v4_skbuff_delattr - Delete any CIPSO options from a packet
2127 * Removes any and all CIPSO options from the given packet. Returns zero on
2128 * success, negative values on failure.
2131 int cipso_v4_skbuff_delattr(struct sk_buff *skb)
2135 struct ip_options *opt = &IPCB(skb)->opt;
2136 unsigned char *cipso_ptr;
2138 if (opt->cipso == 0)
2141 /* since we are changing the packet we should make a copy */
2142 ret_val = skb_cow(skb, skb_headroom(skb));
2146 /* the easiest thing to do is just replace the cipso option with noop
2147 * options since we don't change the size of the packet, although we
2148 * still need to recalculate the checksum */
2151 cipso_ptr = (unsigned char *)iph + opt->cipso;
2152 memset(cipso_ptr, IPOPT_NOOP, cipso_ptr[1]);
2154 opt->is_changed = 1;
2162 * cipso_v4_skbuff_getattr - Get the security attributes from the CIPSO option
2164 * @secattr: the security attributes
2167 * Parse the given packet's CIPSO option and return the security attributes.
2168 * Returns zero on success and negative values on failure.
2171 int cipso_v4_skbuff_getattr(const struct sk_buff *skb,
2172 struct netlbl_lsm_secattr *secattr)
2174 return cipso_v4_getattr(CIPSO_V4_OPTPTR(skb), secattr);
2182 * cipso_v4_init - Initialize the CIPSO module
2185 * Initialize the CIPSO module and prepare it for use. Returns zero on success
2186 * and negative values on failure.
2189 static int __init cipso_v4_init(void)
2193 ret_val = cipso_v4_cache_init();
2195 panic("Failed to initialize the CIPSO/IPv4 cache (%d)\n",
2201 subsys_initcall(cipso_v4_init);