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);
494 spin_lock(&cipso_v4_doi_list_lock);
495 if (cipso_v4_doi_search(doi_def->doi) != NULL)
496 goto doi_add_failure;
497 list_add_tail_rcu(&doi_def->list, &cipso_v4_doi_list);
498 spin_unlock(&cipso_v4_doi_list_lock);
503 spin_unlock(&cipso_v4_doi_list_lock);
508 * cipso_v4_doi_free - Frees a DOI definition
509 * @entry: the entry's RCU field
512 * This function frees all of the memory associated with a DOI definition.
515 void cipso_v4_doi_free(struct cipso_v4_doi *doi_def)
520 switch (doi_def->type) {
521 case CIPSO_V4_MAP_TRANS:
522 kfree(doi_def->map.std->lvl.cipso);
523 kfree(doi_def->map.std->lvl.local);
524 kfree(doi_def->map.std->cat.cipso);
525 kfree(doi_def->map.std->cat.local);
532 * cipso_v4_doi_free_rcu - Frees a DOI definition via the RCU pointer
533 * @entry: the entry's RCU field
536 * This function is designed to be used as a callback to the call_rcu()
537 * function so that the memory allocated to the DOI definition can be released
541 static void cipso_v4_doi_free_rcu(struct rcu_head *entry)
543 struct cipso_v4_doi *doi_def;
545 doi_def = container_of(entry, struct cipso_v4_doi, rcu);
546 cipso_v4_doi_free(doi_def);
550 * cipso_v4_doi_remove - Remove an existing DOI from the CIPSO protocol engine
551 * @doi: the DOI value
552 * @audit_secid: the LSM secid to use in the audit message
555 * Removes a DOI definition from the CIPSO engine. The NetLabel routines will
556 * be called to release their own LSM domain mappings as well as our own
557 * domain list. Returns zero on success and negative values on failure.
560 int cipso_v4_doi_remove(u32 doi, struct netlbl_audit *audit_info)
562 struct cipso_v4_doi *doi_def;
564 spin_lock(&cipso_v4_doi_list_lock);
565 doi_def = cipso_v4_doi_search(doi);
566 if (doi_def == NULL) {
567 spin_unlock(&cipso_v4_doi_list_lock);
570 if (!atomic_dec_and_test(&doi_def->refcount)) {
571 spin_unlock(&cipso_v4_doi_list_lock);
574 list_del_rcu(&doi_def->list);
575 spin_unlock(&cipso_v4_doi_list_lock);
577 cipso_v4_cache_invalidate();
578 call_rcu(&doi_def->rcu, cipso_v4_doi_free_rcu);
584 * cipso_v4_doi_getdef - Returns a reference to a valid DOI definition
585 * @doi: the DOI value
588 * Searches for a valid DOI definition and if one is found it is returned to
589 * the caller. Otherwise NULL is returned. The caller must ensure that
590 * rcu_read_lock() is held while accessing the returned definition and the DOI
591 * definition reference count is decremented when the caller is done.
594 struct cipso_v4_doi *cipso_v4_doi_getdef(u32 doi)
596 struct cipso_v4_doi *doi_def;
599 doi_def = cipso_v4_doi_search(doi);
601 goto doi_getdef_return;
602 if (!atomic_inc_not_zero(&doi_def->refcount))
611 * cipso_v4_doi_putdef - Releases a reference for the given DOI definition
612 * @doi_def: the DOI definition
615 * Releases a DOI definition reference obtained from cipso_v4_doi_getdef().
618 void cipso_v4_doi_putdef(struct cipso_v4_doi *doi_def)
623 if (!atomic_dec_and_test(&doi_def->refcount))
625 spin_lock(&cipso_v4_doi_list_lock);
626 list_del_rcu(&doi_def->list);
627 spin_unlock(&cipso_v4_doi_list_lock);
629 cipso_v4_cache_invalidate();
630 call_rcu(&doi_def->rcu, cipso_v4_doi_free_rcu);
634 * cipso_v4_doi_walk - Iterate through the DOI definitions
635 * @skip_cnt: skip past this number of DOI definitions, updated
636 * @callback: callback for each DOI definition
637 * @cb_arg: argument for the callback function
640 * Iterate over the DOI definition list, skipping the first @skip_cnt entries.
641 * For each entry call @callback, if @callback returns a negative value stop
642 * 'walking' through the list and return. Updates the value in @skip_cnt upon
643 * return. Returns zero on success, negative values on failure.
646 int cipso_v4_doi_walk(u32 *skip_cnt,
647 int (*callback) (struct cipso_v4_doi *doi_def, void *arg),
650 int ret_val = -ENOENT;
652 struct cipso_v4_doi *iter_doi;
655 list_for_each_entry_rcu(iter_doi, &cipso_v4_doi_list, list)
656 if (atomic_read(&iter_doi->refcount) > 0) {
657 if (doi_cnt++ < *skip_cnt)
659 ret_val = callback(iter_doi, cb_arg);
662 goto doi_walk_return;
673 * Label Mapping Functions
677 * cipso_v4_map_lvl_valid - Checks to see if the given level is understood
678 * @doi_def: the DOI definition
679 * @level: the level to check
682 * Checks the given level against the given DOI definition and returns a
683 * negative value if the level does not have a valid mapping and a zero value
684 * if the level is defined by the DOI.
687 static int cipso_v4_map_lvl_valid(const struct cipso_v4_doi *doi_def, u8 level)
689 switch (doi_def->type) {
690 case CIPSO_V4_MAP_PASS:
692 case CIPSO_V4_MAP_TRANS:
693 if (doi_def->map.std->lvl.cipso[level] < CIPSO_V4_INV_LVL)
702 * cipso_v4_map_lvl_hton - Perform a level mapping from the host to the network
703 * @doi_def: the DOI definition
704 * @host_lvl: the host MLS level
705 * @net_lvl: the network/CIPSO MLS level
708 * Perform a label mapping to translate a local MLS level to the correct
709 * CIPSO level using the given DOI definition. Returns zero on success,
710 * negative values otherwise.
713 static int cipso_v4_map_lvl_hton(const struct cipso_v4_doi *doi_def,
717 switch (doi_def->type) {
718 case CIPSO_V4_MAP_PASS:
721 case CIPSO_V4_MAP_TRANS:
722 if (host_lvl < doi_def->map.std->lvl.local_size &&
723 doi_def->map.std->lvl.local[host_lvl] < CIPSO_V4_INV_LVL) {
724 *net_lvl = doi_def->map.std->lvl.local[host_lvl];
734 * cipso_v4_map_lvl_ntoh - Perform a level mapping from the network to the host
735 * @doi_def: the DOI definition
736 * @net_lvl: the network/CIPSO MLS level
737 * @host_lvl: the host MLS level
740 * Perform a label mapping to translate a CIPSO level to the correct local MLS
741 * level using the given DOI definition. Returns zero on success, negative
745 static int cipso_v4_map_lvl_ntoh(const struct cipso_v4_doi *doi_def,
749 struct cipso_v4_std_map_tbl *map_tbl;
751 switch (doi_def->type) {
752 case CIPSO_V4_MAP_PASS:
755 case CIPSO_V4_MAP_TRANS:
756 map_tbl = doi_def->map.std;
757 if (net_lvl < map_tbl->lvl.cipso_size &&
758 map_tbl->lvl.cipso[net_lvl] < CIPSO_V4_INV_LVL) {
759 *host_lvl = doi_def->map.std->lvl.cipso[net_lvl];
769 * cipso_v4_map_cat_rbm_valid - Checks to see if the category bitmap is valid
770 * @doi_def: the DOI definition
771 * @bitmap: category bitmap
772 * @bitmap_len: bitmap length in bytes
775 * Checks the given category bitmap against the given DOI definition and
776 * returns a negative value if any of the categories in the bitmap do not have
777 * a valid mapping and a zero value if all of the categories are valid.
780 static int cipso_v4_map_cat_rbm_valid(const struct cipso_v4_doi *doi_def,
781 const unsigned char *bitmap,
785 u32 bitmap_len_bits = bitmap_len * 8;
789 switch (doi_def->type) {
790 case CIPSO_V4_MAP_PASS:
792 case CIPSO_V4_MAP_TRANS:
793 cipso_cat_size = doi_def->map.std->cat.cipso_size;
794 cipso_array = doi_def->map.std->cat.cipso;
796 cat = cipso_v4_bitmap_walk(bitmap,
802 if (cat >= cipso_cat_size ||
803 cipso_array[cat] >= CIPSO_V4_INV_CAT)
816 * cipso_v4_map_cat_rbm_hton - Perform a category mapping from host to network
817 * @doi_def: the DOI definition
818 * @secattr: the security attributes
819 * @net_cat: the zero'd out category bitmap in network/CIPSO format
820 * @net_cat_len: the length of the CIPSO bitmap in bytes
823 * Perform a label mapping to translate a local MLS category bitmap to the
824 * correct CIPSO bitmap using the given DOI definition. Returns the minimum
825 * size in bytes of the network bitmap on success, negative values otherwise.
828 static int cipso_v4_map_cat_rbm_hton(const struct cipso_v4_doi *doi_def,
829 const struct netlbl_lsm_secattr *secattr,
830 unsigned char *net_cat,
834 u32 net_spot = CIPSO_V4_INV_CAT;
835 u32 net_spot_max = 0;
836 u32 net_clen_bits = net_cat_len * 8;
837 u32 host_cat_size = 0;
838 u32 *host_cat_array = NULL;
840 if (doi_def->type == CIPSO_V4_MAP_TRANS) {
841 host_cat_size = doi_def->map.std->cat.local_size;
842 host_cat_array = doi_def->map.std->cat.local;
846 host_spot = netlbl_secattr_catmap_walk(secattr->attr.mls.cat,
851 switch (doi_def->type) {
852 case CIPSO_V4_MAP_PASS:
853 net_spot = host_spot;
855 case CIPSO_V4_MAP_TRANS:
856 if (host_spot >= host_cat_size)
858 net_spot = host_cat_array[host_spot];
859 if (net_spot >= CIPSO_V4_INV_CAT)
863 if (net_spot >= net_clen_bits)
865 cipso_v4_bitmap_setbit(net_cat, net_spot, 1);
867 if (net_spot > net_spot_max)
868 net_spot_max = net_spot;
871 if (++net_spot_max % 8)
872 return net_spot_max / 8 + 1;
873 return net_spot_max / 8;
877 * cipso_v4_map_cat_rbm_ntoh - Perform a category mapping from network to host
878 * @doi_def: the DOI definition
879 * @net_cat: the category bitmap in network/CIPSO format
880 * @net_cat_len: the length of the CIPSO bitmap in bytes
881 * @secattr: the security attributes
884 * Perform a label mapping to translate a CIPSO bitmap to the correct local
885 * MLS category bitmap using the given DOI definition. Returns zero on
886 * success, negative values on failure.
889 static int cipso_v4_map_cat_rbm_ntoh(const struct cipso_v4_doi *doi_def,
890 const unsigned char *net_cat,
892 struct netlbl_lsm_secattr *secattr)
896 u32 host_spot = CIPSO_V4_INV_CAT;
897 u32 net_clen_bits = net_cat_len * 8;
898 u32 net_cat_size = 0;
899 u32 *net_cat_array = NULL;
901 if (doi_def->type == CIPSO_V4_MAP_TRANS) {
902 net_cat_size = doi_def->map.std->cat.cipso_size;
903 net_cat_array = doi_def->map.std->cat.cipso;
907 net_spot = cipso_v4_bitmap_walk(net_cat,
917 switch (doi_def->type) {
918 case CIPSO_V4_MAP_PASS:
919 host_spot = net_spot;
921 case CIPSO_V4_MAP_TRANS:
922 if (net_spot >= net_cat_size)
924 host_spot = net_cat_array[net_spot];
925 if (host_spot >= CIPSO_V4_INV_CAT)
929 ret_val = netlbl_secattr_catmap_setbit(secattr->attr.mls.cat,
940 * cipso_v4_map_cat_enum_valid - Checks to see if the categories are valid
941 * @doi_def: the DOI definition
942 * @enumcat: category list
943 * @enumcat_len: length of the category list in bytes
946 * Checks the given categories against the given DOI definition and returns a
947 * negative value if any of the categories do not have a valid mapping and a
948 * zero value if all of the categories are valid.
951 static int cipso_v4_map_cat_enum_valid(const struct cipso_v4_doi *doi_def,
952 const unsigned char *enumcat,
959 if (doi_def->type != CIPSO_V4_MAP_PASS || enumcat_len & 0x01)
962 for (iter = 0; iter < enumcat_len; iter += 2) {
963 cat = get_unaligned_be16(&enumcat[iter]);
973 * cipso_v4_map_cat_enum_hton - Perform a category mapping from host to network
974 * @doi_def: the DOI definition
975 * @secattr: the security attributes
976 * @net_cat: the zero'd out category list in network/CIPSO format
977 * @net_cat_len: the length of the CIPSO category list in bytes
980 * Perform a label mapping to translate a local MLS category bitmap to the
981 * correct CIPSO category list using the given DOI definition. Returns the
982 * size in bytes of the network category bitmap on success, negative values
986 static int cipso_v4_map_cat_enum_hton(const struct cipso_v4_doi *doi_def,
987 const struct netlbl_lsm_secattr *secattr,
988 unsigned char *net_cat,
995 cat = netlbl_secattr_catmap_walk(secattr->attr.mls.cat,
999 if ((cat_iter + 2) > net_cat_len)
1002 *((__be16 *)&net_cat[cat_iter]) = htons(cat);
1010 * cipso_v4_map_cat_enum_ntoh - Perform a category mapping from network to host
1011 * @doi_def: the DOI definition
1012 * @net_cat: the category list in network/CIPSO format
1013 * @net_cat_len: the length of the CIPSO bitmap in bytes
1014 * @secattr: the security attributes
1017 * Perform a label mapping to translate a CIPSO category list to the correct
1018 * local MLS category bitmap using the given DOI definition. Returns zero on
1019 * success, negative values on failure.
1022 static int cipso_v4_map_cat_enum_ntoh(const struct cipso_v4_doi *doi_def,
1023 const unsigned char *net_cat,
1025 struct netlbl_lsm_secattr *secattr)
1030 for (iter = 0; iter < net_cat_len; iter += 2) {
1031 ret_val = netlbl_secattr_catmap_setbit(secattr->attr.mls.cat,
1032 get_unaligned_be16(&net_cat[iter]),
1042 * cipso_v4_map_cat_rng_valid - Checks to see if the categories are valid
1043 * @doi_def: the DOI definition
1044 * @rngcat: category list
1045 * @rngcat_len: length of the category list in bytes
1048 * Checks the given categories against the given DOI definition and returns a
1049 * negative value if any of the categories do not have a valid mapping and a
1050 * zero value if all of the categories are valid.
1053 static int cipso_v4_map_cat_rng_valid(const struct cipso_v4_doi *doi_def,
1054 const unsigned char *rngcat,
1059 u32 cat_prev = CIPSO_V4_MAX_REM_CATS + 1;
1062 if (doi_def->type != CIPSO_V4_MAP_PASS || rngcat_len & 0x01)
1065 for (iter = 0; iter < rngcat_len; iter += 4) {
1066 cat_high = get_unaligned_be16(&rngcat[iter]);
1067 if ((iter + 4) <= rngcat_len)
1068 cat_low = get_unaligned_be16(&rngcat[iter + 2]);
1072 if (cat_high > cat_prev)
1082 * cipso_v4_map_cat_rng_hton - Perform a category mapping from host to network
1083 * @doi_def: the DOI definition
1084 * @secattr: the security attributes
1085 * @net_cat: the zero'd out category list in network/CIPSO format
1086 * @net_cat_len: the length of the CIPSO category list in bytes
1089 * Perform a label mapping to translate a local MLS category bitmap to the
1090 * correct CIPSO category list using the given DOI definition. Returns the
1091 * size in bytes of the network category bitmap on success, negative values
1095 static int cipso_v4_map_cat_rng_hton(const struct cipso_v4_doi *doi_def,
1096 const struct netlbl_lsm_secattr *secattr,
1097 unsigned char *net_cat,
1101 u16 array[CIPSO_V4_TAG_RNG_CAT_MAX * 2];
1105 /* make sure we don't overflow the 'array[]' variable */
1107 (CIPSO_V4_OPT_LEN_MAX - CIPSO_V4_HDR_LEN - CIPSO_V4_TAG_RNG_BLEN))
1111 iter = netlbl_secattr_catmap_walk(secattr->attr.mls.cat,
1115 cat_size += (iter == 0 ? 0 : sizeof(u16));
1116 if (cat_size > net_cat_len)
1118 array[array_cnt++] = iter;
1120 iter = netlbl_secattr_catmap_walk_rng(secattr->attr.mls.cat,
1124 cat_size += sizeof(u16);
1125 if (cat_size > net_cat_len)
1127 array[array_cnt++] = iter;
1130 for (iter = 0; array_cnt > 0;) {
1131 *((__be16 *)&net_cat[iter]) = htons(array[--array_cnt]);
1134 if (array[array_cnt] != 0) {
1135 *((__be16 *)&net_cat[iter]) = htons(array[array_cnt]);
1144 * cipso_v4_map_cat_rng_ntoh - Perform a category mapping from network to host
1145 * @doi_def: the DOI definition
1146 * @net_cat: the category list in network/CIPSO format
1147 * @net_cat_len: the length of the CIPSO bitmap in bytes
1148 * @secattr: the security attributes
1151 * Perform a label mapping to translate a CIPSO category list to the correct
1152 * local MLS category bitmap using the given DOI definition. Returns zero on
1153 * success, negative values on failure.
1156 static int cipso_v4_map_cat_rng_ntoh(const struct cipso_v4_doi *doi_def,
1157 const unsigned char *net_cat,
1159 struct netlbl_lsm_secattr *secattr)
1166 for (net_iter = 0; net_iter < net_cat_len; net_iter += 4) {
1167 cat_high = get_unaligned_be16(&net_cat[net_iter]);
1168 if ((net_iter + 4) <= net_cat_len)
1169 cat_low = get_unaligned_be16(&net_cat[net_iter + 2]);
1173 ret_val = netlbl_secattr_catmap_setrng(secattr->attr.mls.cat,
1185 * Protocol Handling Functions
1189 * cipso_v4_gentag_hdr - Generate a CIPSO option header
1190 * @doi_def: the DOI definition
1191 * @len: the total tag length in bytes, not including this header
1192 * @buf: the CIPSO option buffer
1195 * Write a CIPSO header into the beginning of @buffer.
1198 static void cipso_v4_gentag_hdr(const struct cipso_v4_doi *doi_def,
1202 buf[0] = IPOPT_CIPSO;
1203 buf[1] = CIPSO_V4_HDR_LEN + len;
1204 *(__be32 *)&buf[2] = htonl(doi_def->doi);
1208 * cipso_v4_gentag_rbm - Generate a CIPSO restricted bitmap tag (type #1)
1209 * @doi_def: the DOI definition
1210 * @secattr: the security attributes
1211 * @buffer: the option buffer
1212 * @buffer_len: length of buffer in bytes
1215 * Generate a CIPSO option using the restricted bitmap tag, tag type #1. The
1216 * actual buffer length may be larger than the indicated size due to
1217 * translation between host and network category bitmaps. Returns the size of
1218 * the tag on success, negative values on failure.
1221 static int cipso_v4_gentag_rbm(const struct cipso_v4_doi *doi_def,
1222 const struct netlbl_lsm_secattr *secattr,
1223 unsigned char *buffer,
1230 if ((secattr->flags & NETLBL_SECATTR_MLS_LVL) == 0)
1233 ret_val = cipso_v4_map_lvl_hton(doi_def,
1234 secattr->attr.mls.lvl,
1239 if (secattr->flags & NETLBL_SECATTR_MLS_CAT) {
1240 ret_val = cipso_v4_map_cat_rbm_hton(doi_def,
1247 /* This will send packets using the "optimized" format when
1248 * possibile as specified in section 3.4.2.6 of the
1250 if (cipso_v4_rbm_optfmt && ret_val > 0 && ret_val <= 10)
1253 tag_len = 4 + ret_val;
1257 buffer[0] = CIPSO_V4_TAG_RBITMAP;
1258 buffer[1] = tag_len;
1265 * cipso_v4_parsetag_rbm - Parse a CIPSO restricted bitmap tag
1266 * @doi_def: the DOI definition
1267 * @tag: the CIPSO tag
1268 * @secattr: the security attributes
1271 * Parse a CIPSO restricted bitmap tag (tag type #1) and return the security
1272 * attributes in @secattr. Return zero on success, negatives values on
1276 static int cipso_v4_parsetag_rbm(const struct cipso_v4_doi *doi_def,
1277 const unsigned char *tag,
1278 struct netlbl_lsm_secattr *secattr)
1281 u8 tag_len = tag[1];
1284 ret_val = cipso_v4_map_lvl_ntoh(doi_def, tag[3], &level);
1287 secattr->attr.mls.lvl = level;
1288 secattr->flags |= NETLBL_SECATTR_MLS_LVL;
1291 secattr->attr.mls.cat =
1292 netlbl_secattr_catmap_alloc(GFP_ATOMIC);
1293 if (secattr->attr.mls.cat == NULL)
1296 ret_val = cipso_v4_map_cat_rbm_ntoh(doi_def,
1301 netlbl_secattr_catmap_free(secattr->attr.mls.cat);
1305 secattr->flags |= NETLBL_SECATTR_MLS_CAT;
1312 * cipso_v4_gentag_enum - Generate a CIPSO enumerated tag (type #2)
1313 * @doi_def: the DOI definition
1314 * @secattr: the security attributes
1315 * @buffer: the option buffer
1316 * @buffer_len: length of buffer in bytes
1319 * Generate a CIPSO option using the enumerated tag, tag type #2. Returns the
1320 * size of the tag on success, negative values on failure.
1323 static int cipso_v4_gentag_enum(const struct cipso_v4_doi *doi_def,
1324 const struct netlbl_lsm_secattr *secattr,
1325 unsigned char *buffer,
1332 if (!(secattr->flags & NETLBL_SECATTR_MLS_LVL))
1335 ret_val = cipso_v4_map_lvl_hton(doi_def,
1336 secattr->attr.mls.lvl,
1341 if (secattr->flags & NETLBL_SECATTR_MLS_CAT) {
1342 ret_val = cipso_v4_map_cat_enum_hton(doi_def,
1349 tag_len = 4 + ret_val;
1353 buffer[0] = CIPSO_V4_TAG_ENUM;
1354 buffer[1] = tag_len;
1361 * cipso_v4_parsetag_enum - Parse a CIPSO enumerated tag
1362 * @doi_def: the DOI definition
1363 * @tag: the CIPSO tag
1364 * @secattr: the security attributes
1367 * Parse a CIPSO enumerated tag (tag type #2) and return the security
1368 * attributes in @secattr. Return zero on success, negatives values on
1372 static int cipso_v4_parsetag_enum(const struct cipso_v4_doi *doi_def,
1373 const unsigned char *tag,
1374 struct netlbl_lsm_secattr *secattr)
1377 u8 tag_len = tag[1];
1380 ret_val = cipso_v4_map_lvl_ntoh(doi_def, tag[3], &level);
1383 secattr->attr.mls.lvl = level;
1384 secattr->flags |= NETLBL_SECATTR_MLS_LVL;
1387 secattr->attr.mls.cat =
1388 netlbl_secattr_catmap_alloc(GFP_ATOMIC);
1389 if (secattr->attr.mls.cat == NULL)
1392 ret_val = cipso_v4_map_cat_enum_ntoh(doi_def,
1397 netlbl_secattr_catmap_free(secattr->attr.mls.cat);
1401 secattr->flags |= NETLBL_SECATTR_MLS_CAT;
1408 * cipso_v4_gentag_rng - Generate a CIPSO ranged tag (type #5)
1409 * @doi_def: the DOI definition
1410 * @secattr: the security attributes
1411 * @buffer: the option buffer
1412 * @buffer_len: length of buffer in bytes
1415 * Generate a CIPSO option using the ranged tag, tag type #5. Returns the
1416 * size of the tag on success, negative values on failure.
1419 static int cipso_v4_gentag_rng(const struct cipso_v4_doi *doi_def,
1420 const struct netlbl_lsm_secattr *secattr,
1421 unsigned char *buffer,
1428 if (!(secattr->flags & NETLBL_SECATTR_MLS_LVL))
1431 ret_val = cipso_v4_map_lvl_hton(doi_def,
1432 secattr->attr.mls.lvl,
1437 if (secattr->flags & NETLBL_SECATTR_MLS_CAT) {
1438 ret_val = cipso_v4_map_cat_rng_hton(doi_def,
1445 tag_len = 4 + ret_val;
1449 buffer[0] = CIPSO_V4_TAG_RANGE;
1450 buffer[1] = tag_len;
1457 * cipso_v4_parsetag_rng - Parse a CIPSO ranged tag
1458 * @doi_def: the DOI definition
1459 * @tag: the CIPSO tag
1460 * @secattr: the security attributes
1463 * Parse a CIPSO ranged tag (tag type #5) and return the security attributes
1464 * in @secattr. Return zero on success, negatives values on failure.
1467 static int cipso_v4_parsetag_rng(const struct cipso_v4_doi *doi_def,
1468 const unsigned char *tag,
1469 struct netlbl_lsm_secattr *secattr)
1472 u8 tag_len = tag[1];
1475 ret_val = cipso_v4_map_lvl_ntoh(doi_def, tag[3], &level);
1478 secattr->attr.mls.lvl = level;
1479 secattr->flags |= NETLBL_SECATTR_MLS_LVL;
1482 secattr->attr.mls.cat =
1483 netlbl_secattr_catmap_alloc(GFP_ATOMIC);
1484 if (secattr->attr.mls.cat == NULL)
1487 ret_val = cipso_v4_map_cat_rng_ntoh(doi_def,
1492 netlbl_secattr_catmap_free(secattr->attr.mls.cat);
1496 secattr->flags |= NETLBL_SECATTR_MLS_CAT;
1503 * cipso_v4_gentag_loc - Generate a CIPSO local tag (non-standard)
1504 * @doi_def: the DOI definition
1505 * @secattr: the security attributes
1506 * @buffer: the option buffer
1507 * @buffer_len: length of buffer in bytes
1510 * Generate a CIPSO option using the local tag. Returns the size of the tag
1511 * on success, negative values on failure.
1514 static int cipso_v4_gentag_loc(const struct cipso_v4_doi *doi_def,
1515 const struct netlbl_lsm_secattr *secattr,
1516 unsigned char *buffer,
1519 if (!(secattr->flags & NETLBL_SECATTR_SECID))
1522 buffer[0] = CIPSO_V4_TAG_LOCAL;
1523 buffer[1] = CIPSO_V4_TAG_LOC_BLEN;
1524 *(u32 *)&buffer[2] = secattr->attr.secid;
1526 return CIPSO_V4_TAG_LOC_BLEN;
1530 * cipso_v4_parsetag_loc - Parse a CIPSO local tag
1531 * @doi_def: the DOI definition
1532 * @tag: the CIPSO tag
1533 * @secattr: the security attributes
1536 * Parse a CIPSO local tag and return the security attributes in @secattr.
1537 * Return zero on success, negatives values on failure.
1540 static int cipso_v4_parsetag_loc(const struct cipso_v4_doi *doi_def,
1541 const unsigned char *tag,
1542 struct netlbl_lsm_secattr *secattr)
1544 secattr->attr.secid = *(u32 *)&tag[2];
1545 secattr->flags |= NETLBL_SECATTR_SECID;
1551 * cipso_v4_validate - Validate a CIPSO option
1552 * @option: the start of the option, on error it is set to point to the error
1555 * This routine is called to validate a CIPSO option, it checks all of the
1556 * fields to ensure that they are at least valid, see the draft snippet below
1557 * for details. If the option is valid then a zero value is returned and
1558 * the value of @option is unchanged. If the option is invalid then a
1559 * non-zero value is returned and @option is adjusted to point to the
1560 * offending portion of the option. From the IETF draft ...
1562 * "If any field within the CIPSO options, such as the DOI identifier, is not
1563 * recognized the IP datagram is discarded and an ICMP 'parameter problem'
1564 * (type 12) is generated and returned. The ICMP code field is set to 'bad
1565 * parameter' (code 0) and the pointer is set to the start of the CIPSO field
1566 * that is unrecognized."
1569 int cipso_v4_validate(const struct sk_buff *skb, unsigned char **option)
1571 unsigned char *opt = *option;
1573 unsigned char opt_iter;
1574 unsigned char err_offset = 0;
1577 struct cipso_v4_doi *doi_def = NULL;
1580 /* caller already checks for length values that are too large */
1584 goto validate_return;
1588 doi_def = cipso_v4_doi_search(get_unaligned_be32(&opt[2]));
1589 if (doi_def == NULL) {
1591 goto validate_return_locked;
1594 opt_iter = CIPSO_V4_HDR_LEN;
1595 tag = opt + opt_iter;
1596 while (opt_iter < opt_len) {
1597 for (tag_iter = 0; doi_def->tags[tag_iter] != tag[0];)
1598 if (doi_def->tags[tag_iter] == CIPSO_V4_TAG_INVALID ||
1599 ++tag_iter == CIPSO_V4_TAG_MAXCNT) {
1600 err_offset = opt_iter;
1601 goto validate_return_locked;
1605 if (tag_len > (opt_len - opt_iter)) {
1606 err_offset = opt_iter + 1;
1607 goto validate_return_locked;
1611 case CIPSO_V4_TAG_RBITMAP:
1612 if (tag_len < CIPSO_V4_TAG_RBM_BLEN) {
1613 err_offset = opt_iter + 1;
1614 goto validate_return_locked;
1617 /* We are already going to do all the verification
1618 * necessary at the socket layer so from our point of
1619 * view it is safe to turn these checks off (and less
1620 * work), however, the CIPSO draft says we should do
1621 * all the CIPSO validations here but it doesn't
1622 * really specify _exactly_ what we need to validate
1623 * ... so, just make it a sysctl tunable. */
1624 if (cipso_v4_rbm_strictvalid) {
1625 if (cipso_v4_map_lvl_valid(doi_def,
1627 err_offset = opt_iter + 3;
1628 goto validate_return_locked;
1630 if (tag_len > CIPSO_V4_TAG_RBM_BLEN &&
1631 cipso_v4_map_cat_rbm_valid(doi_def,
1634 err_offset = opt_iter + 4;
1635 goto validate_return_locked;
1639 case CIPSO_V4_TAG_ENUM:
1640 if (tag_len < CIPSO_V4_TAG_ENUM_BLEN) {
1641 err_offset = opt_iter + 1;
1642 goto validate_return_locked;
1645 if (cipso_v4_map_lvl_valid(doi_def,
1647 err_offset = opt_iter + 3;
1648 goto validate_return_locked;
1650 if (tag_len > CIPSO_V4_TAG_ENUM_BLEN &&
1651 cipso_v4_map_cat_enum_valid(doi_def,
1654 err_offset = opt_iter + 4;
1655 goto validate_return_locked;
1658 case CIPSO_V4_TAG_RANGE:
1659 if (tag_len < CIPSO_V4_TAG_RNG_BLEN) {
1660 err_offset = opt_iter + 1;
1661 goto validate_return_locked;
1664 if (cipso_v4_map_lvl_valid(doi_def,
1666 err_offset = opt_iter + 3;
1667 goto validate_return_locked;
1669 if (tag_len > CIPSO_V4_TAG_RNG_BLEN &&
1670 cipso_v4_map_cat_rng_valid(doi_def,
1673 err_offset = opt_iter + 4;
1674 goto validate_return_locked;
1677 case CIPSO_V4_TAG_LOCAL:
1678 /* This is a non-standard tag that we only allow for
1679 * local connections, so if the incoming interface is
1680 * not the loopback device drop the packet. */
1681 if (!(skb->dev->flags & IFF_LOOPBACK)) {
1682 err_offset = opt_iter;
1683 goto validate_return_locked;
1685 if (tag_len != CIPSO_V4_TAG_LOC_BLEN) {
1686 err_offset = opt_iter + 1;
1687 goto validate_return_locked;
1691 err_offset = opt_iter;
1692 goto validate_return_locked;
1696 opt_iter += tag_len;
1699 validate_return_locked:
1702 *option = opt + err_offset;
1707 * cipso_v4_error - Send the correct reponse for a bad packet
1709 * @error: the error code
1710 * @gateway: CIPSO gateway flag
1713 * Based on the error code given in @error, send an ICMP error message back to
1714 * the originating host. From the IETF draft ...
1716 * "If the contents of the CIPSO [option] are valid but the security label is
1717 * outside of the configured host or port label range, the datagram is
1718 * discarded and an ICMP 'destination unreachable' (type 3) is generated and
1719 * returned. The code field of the ICMP is set to 'communication with
1720 * destination network administratively prohibited' (code 9) or to
1721 * 'communication with destination host administratively prohibited'
1722 * (code 10). The value of the code is dependent on whether the originator
1723 * of the ICMP message is acting as a CIPSO host or a CIPSO gateway. The
1724 * recipient of the ICMP message MUST be able to handle either value. The
1725 * same procedure is performed if a CIPSO [option] can not be added to an
1726 * IP packet because it is too large to fit in the IP options area."
1728 * "If the error is triggered by receipt of an ICMP message, the message is
1729 * discarded and no response is permitted (consistent with general ICMP
1730 * processing rules)."
1733 void cipso_v4_error(struct sk_buff *skb, int error, u32 gateway)
1735 if (ip_hdr(skb)->protocol == IPPROTO_ICMP || error != -EACCES)
1739 icmp_send(skb, ICMP_DEST_UNREACH, ICMP_NET_ANO, 0);
1741 icmp_send(skb, ICMP_DEST_UNREACH, ICMP_HOST_ANO, 0);
1745 * cipso_v4_genopt - Generate a CIPSO option
1746 * @buf: the option buffer
1747 * @buf_len: the size of opt_buf
1748 * @doi_def: the CIPSO DOI to use
1749 * @secattr: the security attributes
1752 * Generate a CIPSO option using the DOI definition and security attributes
1753 * passed to the function. Returns the length of the option on success and
1754 * negative values on failure.
1757 static int cipso_v4_genopt(unsigned char *buf, u32 buf_len,
1758 const struct cipso_v4_doi *doi_def,
1759 const struct netlbl_lsm_secattr *secattr)
1764 if (buf_len <= CIPSO_V4_HDR_LEN)
1767 /* XXX - This code assumes only one tag per CIPSO option which isn't
1768 * really a good assumption to make but since we only support the MAC
1769 * tags right now it is a safe assumption. */
1772 memset(buf, 0, buf_len);
1773 switch (doi_def->tags[iter]) {
1774 case CIPSO_V4_TAG_RBITMAP:
1775 ret_val = cipso_v4_gentag_rbm(doi_def,
1777 &buf[CIPSO_V4_HDR_LEN],
1778 buf_len - CIPSO_V4_HDR_LEN);
1780 case CIPSO_V4_TAG_ENUM:
1781 ret_val = cipso_v4_gentag_enum(doi_def,
1783 &buf[CIPSO_V4_HDR_LEN],
1784 buf_len - CIPSO_V4_HDR_LEN);
1786 case CIPSO_V4_TAG_RANGE:
1787 ret_val = cipso_v4_gentag_rng(doi_def,
1789 &buf[CIPSO_V4_HDR_LEN],
1790 buf_len - CIPSO_V4_HDR_LEN);
1792 case CIPSO_V4_TAG_LOCAL:
1793 ret_val = cipso_v4_gentag_loc(doi_def,
1795 &buf[CIPSO_V4_HDR_LEN],
1796 buf_len - CIPSO_V4_HDR_LEN);
1803 } while (ret_val < 0 &&
1804 iter < CIPSO_V4_TAG_MAXCNT &&
1805 doi_def->tags[iter] != CIPSO_V4_TAG_INVALID);
1808 cipso_v4_gentag_hdr(doi_def, buf, ret_val);
1809 return CIPSO_V4_HDR_LEN + ret_val;
1813 * cipso_v4_sock_setattr - Add a CIPSO option to a socket
1815 * @doi_def: the CIPSO DOI to use
1816 * @secattr: the specific security attributes of the socket
1819 * Set the CIPSO option on the given socket using the DOI definition and
1820 * security attributes passed to the function. This function requires
1821 * exclusive access to @sk, which means it either needs to be in the
1822 * process of being created or locked. Returns zero on success and negative
1823 * values on failure.
1826 int cipso_v4_sock_setattr(struct sock *sk,
1827 const struct cipso_v4_doi *doi_def,
1828 const struct netlbl_lsm_secattr *secattr)
1830 int ret_val = -EPERM;
1831 unsigned char *buf = NULL;
1834 struct ip_options *opt = NULL;
1835 struct inet_sock *sk_inet;
1836 struct inet_connection_sock *sk_conn;
1838 /* In the case of sock_create_lite(), the sock->sk field is not
1839 * defined yet but it is not a problem as the only users of these
1840 * "lite" PF_INET sockets are functions which do an accept() call
1841 * afterwards so we will label the socket as part of the accept(). */
1845 /* We allocate the maximum CIPSO option size here so we are probably
1846 * being a little wasteful, but it makes our life _much_ easier later
1847 * on and after all we are only talking about 40 bytes. */
1848 buf_len = CIPSO_V4_OPT_LEN_MAX;
1849 buf = kmalloc(buf_len, GFP_ATOMIC);
1852 goto socket_setattr_failure;
1855 ret_val = cipso_v4_genopt(buf, buf_len, doi_def, secattr);
1857 goto socket_setattr_failure;
1860 /* We can't use ip_options_get() directly because it makes a call to
1861 * ip_options_get_alloc() which allocates memory with GFP_KERNEL and
1862 * we won't always have CAP_NET_RAW even though we _always_ want to
1863 * set the IPOPT_CIPSO option. */
1864 opt_len = (buf_len + 3) & ~3;
1865 opt = kzalloc(sizeof(*opt) + opt_len, GFP_ATOMIC);
1868 goto socket_setattr_failure;
1870 memcpy(opt->__data, buf, buf_len);
1871 opt->optlen = opt_len;
1872 opt->cipso = sizeof(struct iphdr);
1876 sk_inet = inet_sk(sk);
1877 if (sk_inet->is_icsk) {
1878 sk_conn = inet_csk(sk);
1880 sk_conn->icsk_ext_hdr_len -= sk_inet->opt->optlen;
1881 sk_conn->icsk_ext_hdr_len += opt->optlen;
1882 sk_conn->icsk_sync_mss(sk, sk_conn->icsk_pmtu_cookie);
1884 opt = xchg(&sk_inet->opt, opt);
1889 socket_setattr_failure:
1896 * cipso_v4_sock_delattr - Delete the CIPSO option from a socket
1900 * Removes the CIPSO option from a socket, if present.
1903 void cipso_v4_sock_delattr(struct sock *sk)
1906 struct ip_options *opt;
1907 struct inet_sock *sk_inet;
1909 sk_inet = inet_sk(sk);
1911 if (opt == NULL || opt->cipso == 0)
1914 if (opt->srr || opt->rr || opt->ts || opt->router_alert) {
1917 unsigned char *cipso_ptr;
1921 cipso_off = opt->cipso - sizeof(struct iphdr);
1922 cipso_ptr = &opt->__data[cipso_off];
1923 cipso_len = cipso_ptr[1];
1925 if (opt->srr > opt->cipso)
1926 opt->srr -= cipso_len;
1927 if (opt->rr > opt->cipso)
1928 opt->rr -= cipso_len;
1929 if (opt->ts > opt->cipso)
1930 opt->ts -= cipso_len;
1931 if (opt->router_alert > opt->cipso)
1932 opt->router_alert -= cipso_len;
1935 memmove(cipso_ptr, cipso_ptr + cipso_len,
1936 opt->optlen - cipso_off - cipso_len);
1938 /* determining the new total option length is tricky because of
1939 * the padding necessary, the only thing i can think to do at
1940 * this point is walk the options one-by-one, skipping the
1941 * padding at the end to determine the actual option size and
1942 * from there we can determine the new total option length */
1945 while (iter < opt->optlen)
1946 if (opt->__data[iter] != IPOPT_NOP) {
1947 iter += opt->__data[iter + 1];
1951 hdr_delta = opt->optlen;
1952 opt->optlen = (optlen_new + 3) & ~3;
1953 hdr_delta -= opt->optlen;
1955 /* only the cipso option was present on the socket so we can
1956 * remove the entire option struct */
1957 sk_inet->opt = NULL;
1958 hdr_delta = opt->optlen;
1962 if (sk_inet->is_icsk && hdr_delta > 0) {
1963 struct inet_connection_sock *sk_conn = inet_csk(sk);
1964 sk_conn->icsk_ext_hdr_len -= hdr_delta;
1965 sk_conn->icsk_sync_mss(sk, sk_conn->icsk_pmtu_cookie);
1970 * cipso_v4_getattr - Helper function for the cipso_v4_*_getattr functions
1971 * @cipso: the CIPSO v4 option
1972 * @secattr: the security attributes
1975 * Inspect @cipso and return the security attributes in @secattr. Returns zero
1976 * on success and negative values on failure.
1979 static int cipso_v4_getattr(const unsigned char *cipso,
1980 struct netlbl_lsm_secattr *secattr)
1982 int ret_val = -ENOMSG;
1984 struct cipso_v4_doi *doi_def;
1986 if (cipso_v4_cache_check(cipso, cipso[1], secattr) == 0)
1989 doi = get_unaligned_be32(&cipso[2]);
1991 doi_def = cipso_v4_doi_search(doi);
1992 if (doi_def == NULL)
1993 goto getattr_return;
1994 /* XXX - This code assumes only one tag per CIPSO option which isn't
1995 * really a good assumption to make but since we only support the MAC
1996 * tags right now it is a safe assumption. */
1998 case CIPSO_V4_TAG_RBITMAP:
1999 ret_val = cipso_v4_parsetag_rbm(doi_def, &cipso[6], secattr);
2001 case CIPSO_V4_TAG_ENUM:
2002 ret_val = cipso_v4_parsetag_enum(doi_def, &cipso[6], secattr);
2004 case CIPSO_V4_TAG_RANGE:
2005 ret_val = cipso_v4_parsetag_rng(doi_def, &cipso[6], secattr);
2007 case CIPSO_V4_TAG_LOCAL:
2008 ret_val = cipso_v4_parsetag_loc(doi_def, &cipso[6], secattr);
2012 secattr->type = NETLBL_NLTYPE_CIPSOV4;
2020 * cipso_v4_sock_getattr - Get the security attributes from a sock
2022 * @secattr: the security attributes
2025 * Query @sk to see if there is a CIPSO option attached to the sock and if
2026 * there is return the CIPSO security attributes in @secattr. This function
2027 * requires that @sk be locked, or privately held, but it does not do any
2028 * locking itself. Returns zero on success and negative values on failure.
2031 int cipso_v4_sock_getattr(struct sock *sk, struct netlbl_lsm_secattr *secattr)
2033 struct ip_options *opt;
2035 opt = inet_sk(sk)->opt;
2036 if (opt == NULL || opt->cipso == 0)
2039 return cipso_v4_getattr(opt->__data + opt->cipso - sizeof(struct iphdr),
2044 * cipso_v4_skbuff_setattr - Set the CIPSO option on a packet
2046 * @secattr: the security attributes
2049 * Set the CIPSO option on the given packet based on the security attributes.
2050 * Returns a pointer to the IP header on success and NULL on failure.
2053 int cipso_v4_skbuff_setattr(struct sk_buff *skb,
2054 const struct cipso_v4_doi *doi_def,
2055 const struct netlbl_lsm_secattr *secattr)
2059 struct ip_options *opt = &IPCB(skb)->opt;
2060 unsigned char buf[CIPSO_V4_OPT_LEN_MAX];
2061 u32 buf_len = CIPSO_V4_OPT_LEN_MAX;
2065 ret_val = 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);