RT-AC56 3.0.0.4.374.37 core
[tomato.git] / release / src-rt-6.x.4708 / linux / linux-2.6.36 / net / ipv4 / cipso_ipv4.c
blobd31a4271ee95a8d630eac7f35756419c44eb2c93
1 /*
2 * CIPSO - Commercial IP Security Option
4 * This is an implementation of the CIPSO 2.2 protocol as specified in
5 * draft-ietf-cipso-ipsecurity-01.txt with additional tag types as found in
6 * FIPS-188. While CIPSO never became a full IETF RFC standard many vendors
7 * have chosen to adopt the protocol and over the years it has become a
8 * de-facto standard for labeled networking.
10 * The CIPSO draft specification can be found in the kernel's Documentation
11 * directory as well as the following URL:
12 * http://netlabel.sourceforge.net/files/draft-ietf-cipso-ipsecurity-01.txt
13 * The FIPS-188 specification can be found at the following URL:
14 * http://www.itl.nist.gov/fipspubs/fip188.htm
16 * Author: Paul Moore <paul.moore@hp.com>
21 * (c) Copyright Hewlett-Packard Development Company, L.P., 2006, 2008
23 * This program is free software; you can redistribute it and/or modify
24 * it under the terms of the GNU General Public License as published by
25 * the Free Software Foundation; either version 2 of the License, or
26 * (at your option) any later version.
28 * This program is distributed in the hope that it will be useful,
29 * but WITHOUT ANY WARRANTY; without even the implied warranty of
30 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See
31 * the GNU General Public License for more details.
33 * You should have received a copy of the GNU General Public License
34 * along with this program; if not, write to the Free Software
35 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
39 #include <linux/init.h>
40 #include <linux/types.h>
41 #include <linux/rcupdate.h>
42 #include <linux/list.h>
43 #include <linux/spinlock.h>
44 #include <linux/string.h>
45 #include <linux/jhash.h>
46 #include <linux/audit.h>
47 #include <linux/slab.h>
48 #include <net/ip.h>
49 #include <net/icmp.h>
50 #include <net/tcp.h>
51 #include <net/netlabel.h>
52 #include <net/cipso_ipv4.h>
53 #include <asm/atomic.h>
54 #include <asm/bug.h>
55 #include <asm/unaligned.h>
57 /* List of available DOI definitions */
58 static DEFINE_SPINLOCK(cipso_v4_doi_list_lock);
59 static LIST_HEAD(cipso_v4_doi_list);
61 /* Label mapping cache */
62 int cipso_v4_cache_enabled = 1;
63 int cipso_v4_cache_bucketsize = 10;
64 #define CIPSO_V4_CACHE_BUCKETBITS 7
65 #define CIPSO_V4_CACHE_BUCKETS (1 << CIPSO_V4_CACHE_BUCKETBITS)
66 #define CIPSO_V4_CACHE_REORDERLIMIT 10
67 struct cipso_v4_map_cache_bkt {
68 spinlock_t lock;
69 u32 size;
70 struct list_head list;
72 struct cipso_v4_map_cache_entry {
73 u32 hash;
74 unsigned char *key;
75 size_t key_len;
77 struct netlbl_lsm_cache *lsm_data;
79 u32 activity;
80 struct list_head list;
82 static struct cipso_v4_map_cache_bkt *cipso_v4_cache = NULL;
84 /* Restricted bitmap (tag #1) flags */
85 int cipso_v4_rbm_optfmt = 0;
86 int cipso_v4_rbm_strictvalid = 1;
89 * Protocol Constants
92 /* Maximum size of the CIPSO IP option, derived from the fact that the maximum
93 * IPv4 header size is 60 bytes and the base IPv4 header is 20 bytes long. */
94 #define CIPSO_V4_OPT_LEN_MAX 40
96 /* Length of the base CIPSO option, this includes the option type (1 byte), the
97 * option length (1 byte), and the DOI (4 bytes). */
98 #define CIPSO_V4_HDR_LEN 6
100 /* Base length of the restrictive category bitmap tag (tag #1). */
101 #define CIPSO_V4_TAG_RBM_BLEN 4
103 /* Base length of the enumerated category tag (tag #2). */
104 #define CIPSO_V4_TAG_ENUM_BLEN 4
106 /* Base length of the ranged categories bitmap tag (tag #5). */
107 #define CIPSO_V4_TAG_RNG_BLEN 4
108 /* The maximum number of category ranges permitted in the ranged category tag
109 * (tag #5). You may note that the IETF draft states that the maximum number
110 * of category ranges is 7, but if the low end of the last category range is
111 * zero then it is possibile to fit 8 category ranges because the zero should
112 * be omitted. */
113 #define CIPSO_V4_TAG_RNG_CAT_MAX 8
115 /* Base length of the local tag (non-standard tag).
116 * Tag definition (may change between kernel versions)
118 * 0 8 16 24 32
119 * +----------+----------+----------+----------+
120 * | 10000000 | 00000110 | 32-bit secid value |
121 * +----------+----------+----------+----------+
122 * | in (host byte order)|
123 * +----------+----------+
126 #define CIPSO_V4_TAG_LOC_BLEN 6
129 * Helper Functions
133 * cipso_v4_bitmap_walk - Walk a bitmap looking for a bit
134 * @bitmap: the bitmap
135 * @bitmap_len: length in bits
136 * @offset: starting offset
137 * @state: if non-zero, look for a set (1) bit else look for a cleared (0) bit
139 * Description:
140 * Starting at @offset, walk the bitmap from left to right until either the
141 * desired bit is found or we reach the end. Return the bit offset, -1 if
142 * not found, or -2 if error.
144 static int cipso_v4_bitmap_walk(const unsigned char *bitmap,
145 u32 bitmap_len,
146 u32 offset,
147 u8 state)
149 u32 bit_spot;
150 u32 byte_offset;
151 unsigned char bitmask;
152 unsigned char byte;
154 /* gcc always rounds to zero when doing integer division */
155 byte_offset = offset / 8;
156 byte = bitmap[byte_offset];
157 bit_spot = offset;
158 bitmask = 0x80 >> (offset % 8);
160 while (bit_spot < bitmap_len) {
161 if ((state && (byte & bitmask) == bitmask) ||
162 (state == 0 && (byte & bitmask) == 0))
163 return bit_spot;
165 bit_spot++;
166 bitmask >>= 1;
167 if (bitmask == 0) {
168 byte = bitmap[++byte_offset];
169 bitmask = 0x80;
173 return -1;
177 * cipso_v4_bitmap_setbit - Sets a single bit in a bitmap
178 * @bitmap: the bitmap
179 * @bit: the bit
180 * @state: if non-zero, set the bit (1) else clear the bit (0)
182 * Description:
183 * Set a single bit in the bitmask. Returns zero on success, negative values
184 * on error.
186 static void cipso_v4_bitmap_setbit(unsigned char *bitmap,
187 u32 bit,
188 u8 state)
190 u32 byte_spot;
191 u8 bitmask;
193 /* gcc always rounds to zero when doing integer division */
194 byte_spot = bit / 8;
195 bitmask = 0x80 >> (bit % 8);
196 if (state)
197 bitmap[byte_spot] |= bitmask;
198 else
199 bitmap[byte_spot] &= ~bitmask;
203 * cipso_v4_cache_entry_free - Frees a cache entry
204 * @entry: the entry to free
206 * Description:
207 * This function frees the memory associated with a cache entry including the
208 * LSM cache data if there are no longer any users, i.e. reference count == 0.
211 static void cipso_v4_cache_entry_free(struct cipso_v4_map_cache_entry *entry)
213 if (entry->lsm_data)
214 netlbl_secattr_cache_free(entry->lsm_data);
215 kfree(entry->key);
216 kfree(entry);
220 * cipso_v4_map_cache_hash - Hashing function for the CIPSO cache
221 * @key: the hash key
222 * @key_len: the length of the key in bytes
224 * Description:
225 * The CIPSO tag hashing function. Returns a 32-bit hash value.
228 static u32 cipso_v4_map_cache_hash(const unsigned char *key, u32 key_len)
230 return jhash(key, key_len, 0);
234 * Label Mapping Cache Functions
238 * cipso_v4_cache_init - Initialize the CIPSO cache
240 * Description:
241 * Initializes the CIPSO label mapping cache, this function should be called
242 * before any of the other functions defined in this file. Returns zero on
243 * success, negative values on error.
246 static int cipso_v4_cache_init(void)
248 u32 iter;
250 cipso_v4_cache = kcalloc(CIPSO_V4_CACHE_BUCKETS,
251 sizeof(struct cipso_v4_map_cache_bkt),
252 GFP_KERNEL);
253 if (cipso_v4_cache == NULL)
254 return -ENOMEM;
256 for (iter = 0; iter < CIPSO_V4_CACHE_BUCKETS; iter++) {
257 spin_lock_init(&cipso_v4_cache[iter].lock);
258 cipso_v4_cache[iter].size = 0;
259 INIT_LIST_HEAD(&cipso_v4_cache[iter].list);
262 return 0;
266 * cipso_v4_cache_invalidate - Invalidates the current CIPSO cache
268 * Description:
269 * Invalidates and frees any entries in the CIPSO cache. Returns zero on
270 * success and negative values on failure.
273 void cipso_v4_cache_invalidate(void)
275 struct cipso_v4_map_cache_entry *entry, *tmp_entry;
276 u32 iter;
278 for (iter = 0; iter < CIPSO_V4_CACHE_BUCKETS; iter++) {
279 spin_lock_bh(&cipso_v4_cache[iter].lock);
280 list_for_each_entry_safe(entry,
281 tmp_entry,
282 &cipso_v4_cache[iter].list, list) {
283 list_del(&entry->list);
284 cipso_v4_cache_entry_free(entry);
286 cipso_v4_cache[iter].size = 0;
287 spin_unlock_bh(&cipso_v4_cache[iter].lock);
292 * cipso_v4_cache_check - Check the CIPSO cache for a label mapping
293 * @key: the buffer to check
294 * @key_len: buffer length in bytes
295 * @secattr: the security attribute struct to use
297 * Description:
298 * This function checks the cache to see if a label mapping already exists for
299 * the given key. If there is a match then the cache is adjusted and the
300 * @secattr struct is populated with the correct LSM security attributes. The
301 * cache is adjusted in the following manner if the entry is not already the
302 * first in the cache bucket:
304 * 1. The cache entry's activity counter is incremented
305 * 2. The previous (higher ranking) entry's activity counter is decremented
306 * 3. If the difference between the two activity counters is geater than
307 * CIPSO_V4_CACHE_REORDERLIMIT the two entries are swapped
309 * Returns zero on success, -ENOENT for a cache miss, and other negative values
310 * on error.
313 static int cipso_v4_cache_check(const unsigned char *key,
314 u32 key_len,
315 struct netlbl_lsm_secattr *secattr)
317 u32 bkt;
318 struct cipso_v4_map_cache_entry *entry;
319 struct cipso_v4_map_cache_entry *prev_entry = NULL;
320 u32 hash;
322 if (!cipso_v4_cache_enabled)
323 return -ENOENT;
325 hash = cipso_v4_map_cache_hash(key, key_len);
326 bkt = hash & (CIPSO_V4_CACHE_BUCKETS - 1);
327 spin_lock_bh(&cipso_v4_cache[bkt].lock);
328 list_for_each_entry(entry, &cipso_v4_cache[bkt].list, list) {
329 if (entry->hash == hash &&
330 entry->key_len == key_len &&
331 memcmp(entry->key, key, key_len) == 0) {
332 entry->activity += 1;
333 atomic_inc(&entry->lsm_data->refcount);
334 secattr->cache = entry->lsm_data;
335 secattr->flags |= NETLBL_SECATTR_CACHE;
336 secattr->type = NETLBL_NLTYPE_CIPSOV4;
337 if (prev_entry == NULL) {
338 spin_unlock_bh(&cipso_v4_cache[bkt].lock);
339 return 0;
342 if (prev_entry->activity > 0)
343 prev_entry->activity -= 1;
344 if (entry->activity > prev_entry->activity &&
345 entry->activity - prev_entry->activity >
346 CIPSO_V4_CACHE_REORDERLIMIT) {
347 __list_del(entry->list.prev, entry->list.next);
348 __list_add(&entry->list,
349 prev_entry->list.prev,
350 &prev_entry->list);
353 spin_unlock_bh(&cipso_v4_cache[bkt].lock);
354 return 0;
356 prev_entry = entry;
358 spin_unlock_bh(&cipso_v4_cache[bkt].lock);
360 return -ENOENT;
364 * cipso_v4_cache_add - Add an entry to the CIPSO cache
365 * @skb: the packet
366 * @secattr: the packet's security attributes
368 * Description:
369 * Add a new entry into the CIPSO label mapping cache. Add the new entry to
370 * head of the cache bucket's list, if the cache bucket is out of room remove
371 * the last entry in the list first. It is important to note that there is
372 * currently no checking for duplicate keys. Returns zero on success,
373 * negative values on failure.
376 int cipso_v4_cache_add(const struct sk_buff *skb,
377 const struct netlbl_lsm_secattr *secattr)
379 int ret_val = -EPERM;
380 u32 bkt;
381 struct cipso_v4_map_cache_entry *entry = NULL;
382 struct cipso_v4_map_cache_entry *old_entry = NULL;
383 unsigned char *cipso_ptr;
384 u32 cipso_ptr_len;
386 if (!cipso_v4_cache_enabled || cipso_v4_cache_bucketsize <= 0)
387 return 0;
389 cipso_ptr = CIPSO_V4_OPTPTR(skb);
390 cipso_ptr_len = cipso_ptr[1];
392 entry = kzalloc(sizeof(*entry), GFP_ATOMIC);
393 if (entry == NULL)
394 return -ENOMEM;
395 entry->key = kmemdup(cipso_ptr, cipso_ptr_len, GFP_ATOMIC);
396 if (entry->key == NULL) {
397 ret_val = -ENOMEM;
398 goto cache_add_failure;
400 entry->key_len = cipso_ptr_len;
401 entry->hash = cipso_v4_map_cache_hash(cipso_ptr, cipso_ptr_len);
402 atomic_inc(&secattr->cache->refcount);
403 entry->lsm_data = secattr->cache;
405 bkt = entry->hash & (CIPSO_V4_CACHE_BUCKETS - 1);
406 spin_lock_bh(&cipso_v4_cache[bkt].lock);
407 if (cipso_v4_cache[bkt].size < cipso_v4_cache_bucketsize) {
408 list_add(&entry->list, &cipso_v4_cache[bkt].list);
409 cipso_v4_cache[bkt].size += 1;
410 } else {
411 old_entry = list_entry(cipso_v4_cache[bkt].list.prev,
412 struct cipso_v4_map_cache_entry, list);
413 list_del(&old_entry->list);
414 list_add(&entry->list, &cipso_v4_cache[bkt].list);
415 cipso_v4_cache_entry_free(old_entry);
417 spin_unlock_bh(&cipso_v4_cache[bkt].lock);
419 return 0;
421 cache_add_failure:
422 if (entry)
423 cipso_v4_cache_entry_free(entry);
424 return ret_val;
428 * DOI List Functions
432 * cipso_v4_doi_search - Searches for a DOI definition
433 * @doi: the DOI to search for
435 * Description:
436 * Search the DOI definition list for a DOI definition with a DOI value that
437 * matches @doi. The caller is responsibile for calling rcu_read_[un]lock().
438 * Returns a pointer to the DOI definition on success and NULL on failure.
440 static struct cipso_v4_doi *cipso_v4_doi_search(u32 doi)
442 struct cipso_v4_doi *iter;
444 list_for_each_entry_rcu(iter, &cipso_v4_doi_list, list)
445 if (iter->doi == doi && atomic_read(&iter->refcount))
446 return iter;
447 return NULL;
451 * cipso_v4_doi_add - Add a new DOI to the CIPSO protocol engine
452 * @doi_def: the DOI structure
453 * @audit_info: NetLabel audit information
455 * Description:
456 * The caller defines a new DOI for use by the CIPSO engine and calls this
457 * function to add it to the list of acceptable domains. The caller must
458 * ensure that the mapping table specified in @doi_def->map meets all of the
459 * requirements of the mapping type (see cipso_ipv4.h for details). Returns
460 * zero on success and non-zero on failure.
463 int cipso_v4_doi_add(struct cipso_v4_doi *doi_def,
464 struct netlbl_audit *audit_info)
466 int ret_val = -EINVAL;
467 u32 iter;
468 u32 doi;
469 u32 doi_type;
470 struct audit_buffer *audit_buf;
472 doi = doi_def->doi;
473 doi_type = doi_def->type;
475 if (doi_def == NULL || doi_def->doi == CIPSO_V4_DOI_UNKNOWN)
476 goto doi_add_return;
477 for (iter = 0; iter < CIPSO_V4_TAG_MAXCNT; iter++) {
478 switch (doi_def->tags[iter]) {
479 case CIPSO_V4_TAG_RBITMAP:
480 break;
481 case CIPSO_V4_TAG_RANGE:
482 case CIPSO_V4_TAG_ENUM:
483 if (doi_def->type != CIPSO_V4_MAP_PASS)
484 goto doi_add_return;
485 break;
486 case CIPSO_V4_TAG_LOCAL:
487 if (doi_def->type != CIPSO_V4_MAP_LOCAL)
488 goto doi_add_return;
489 break;
490 case CIPSO_V4_TAG_INVALID:
491 if (iter == 0)
492 goto doi_add_return;
493 break;
494 default:
495 goto doi_add_return;
499 atomic_set(&doi_def->refcount, 1);
501 spin_lock(&cipso_v4_doi_list_lock);
502 if (cipso_v4_doi_search(doi_def->doi) != NULL) {
503 spin_unlock(&cipso_v4_doi_list_lock);
504 ret_val = -EEXIST;
505 goto doi_add_return;
507 list_add_tail_rcu(&doi_def->list, &cipso_v4_doi_list);
508 spin_unlock(&cipso_v4_doi_list_lock);
509 ret_val = 0;
511 doi_add_return:
512 audit_buf = netlbl_audit_start(AUDIT_MAC_CIPSOV4_ADD, audit_info);
513 if (audit_buf != NULL) {
514 const char *type_str;
515 switch (doi_type) {
516 case CIPSO_V4_MAP_TRANS:
517 type_str = "trans";
518 break;
519 case CIPSO_V4_MAP_PASS:
520 type_str = "pass";
521 break;
522 case CIPSO_V4_MAP_LOCAL:
523 type_str = "local";
524 break;
525 default:
526 type_str = "(unknown)";
528 audit_log_format(audit_buf,
529 " cipso_doi=%u cipso_type=%s res=%u",
530 doi, type_str, ret_val == 0 ? 1 : 0);
531 audit_log_end(audit_buf);
534 return ret_val;
538 * cipso_v4_doi_free - Frees a DOI definition
539 * @entry: the entry's RCU field
541 * Description:
542 * This function frees all of the memory associated with a DOI definition.
545 void cipso_v4_doi_free(struct cipso_v4_doi *doi_def)
547 if (doi_def == NULL)
548 return;
550 switch (doi_def->type) {
551 case CIPSO_V4_MAP_TRANS:
552 kfree(doi_def->map.std->lvl.cipso);
553 kfree(doi_def->map.std->lvl.local);
554 kfree(doi_def->map.std->cat.cipso);
555 kfree(doi_def->map.std->cat.local);
556 break;
558 kfree(doi_def);
562 * cipso_v4_doi_free_rcu - Frees a DOI definition via the RCU pointer
563 * @entry: the entry's RCU field
565 * Description:
566 * This function is designed to be used as a callback to the call_rcu()
567 * function so that the memory allocated to the DOI definition can be released
568 * safely.
571 static void cipso_v4_doi_free_rcu(struct rcu_head *entry)
573 struct cipso_v4_doi *doi_def;
575 doi_def = container_of(entry, struct cipso_v4_doi, rcu);
576 cipso_v4_doi_free(doi_def);
580 * cipso_v4_doi_remove - Remove an existing DOI from the CIPSO protocol engine
581 * @doi: the DOI value
582 * @audit_secid: the LSM secid to use in the audit message
584 * Description:
585 * Removes a DOI definition from the CIPSO engine. The NetLabel routines will
586 * be called to release their own LSM domain mappings as well as our own
587 * domain list. Returns zero on success and negative values on failure.
590 int cipso_v4_doi_remove(u32 doi, struct netlbl_audit *audit_info)
592 int ret_val;
593 struct cipso_v4_doi *doi_def;
594 struct audit_buffer *audit_buf;
596 spin_lock(&cipso_v4_doi_list_lock);
597 doi_def = cipso_v4_doi_search(doi);
598 if (doi_def == NULL) {
599 spin_unlock(&cipso_v4_doi_list_lock);
600 ret_val = -ENOENT;
601 goto doi_remove_return;
603 if (!atomic_dec_and_test(&doi_def->refcount)) {
604 spin_unlock(&cipso_v4_doi_list_lock);
605 ret_val = -EBUSY;
606 goto doi_remove_return;
608 list_del_rcu(&doi_def->list);
609 spin_unlock(&cipso_v4_doi_list_lock);
611 cipso_v4_cache_invalidate();
612 call_rcu(&doi_def->rcu, cipso_v4_doi_free_rcu);
613 ret_val = 0;
615 doi_remove_return:
616 audit_buf = netlbl_audit_start(AUDIT_MAC_CIPSOV4_DEL, audit_info);
617 if (audit_buf != NULL) {
618 audit_log_format(audit_buf,
619 " cipso_doi=%u res=%u",
620 doi, ret_val == 0 ? 1 : 0);
621 audit_log_end(audit_buf);
624 return ret_val;
628 * cipso_v4_doi_getdef - Returns a reference to a valid DOI definition
629 * @doi: the DOI value
631 * Description:
632 * Searches for a valid DOI definition and if one is found it is returned to
633 * the caller. Otherwise NULL is returned. The caller must ensure that
634 * rcu_read_lock() is held while accessing the returned definition and the DOI
635 * definition reference count is decremented when the caller is done.
638 struct cipso_v4_doi *cipso_v4_doi_getdef(u32 doi)
640 struct cipso_v4_doi *doi_def;
642 rcu_read_lock();
643 doi_def = cipso_v4_doi_search(doi);
644 if (doi_def == NULL)
645 goto doi_getdef_return;
646 if (!atomic_inc_not_zero(&doi_def->refcount))
647 doi_def = NULL;
649 doi_getdef_return:
650 rcu_read_unlock();
651 return doi_def;
655 * cipso_v4_doi_putdef - Releases a reference for the given DOI definition
656 * @doi_def: the DOI definition
658 * Description:
659 * Releases a DOI definition reference obtained from cipso_v4_doi_getdef().
662 void cipso_v4_doi_putdef(struct cipso_v4_doi *doi_def)
664 if (doi_def == NULL)
665 return;
667 if (!atomic_dec_and_test(&doi_def->refcount))
668 return;
669 spin_lock(&cipso_v4_doi_list_lock);
670 list_del_rcu(&doi_def->list);
671 spin_unlock(&cipso_v4_doi_list_lock);
673 cipso_v4_cache_invalidate();
674 call_rcu(&doi_def->rcu, cipso_v4_doi_free_rcu);
678 * cipso_v4_doi_walk - Iterate through the DOI definitions
679 * @skip_cnt: skip past this number of DOI definitions, updated
680 * @callback: callback for each DOI definition
681 * @cb_arg: argument for the callback function
683 * Description:
684 * Iterate over the DOI definition list, skipping the first @skip_cnt entries.
685 * For each entry call @callback, if @callback returns a negative value stop
686 * 'walking' through the list and return. Updates the value in @skip_cnt upon
687 * return. Returns zero on success, negative values on failure.
690 int cipso_v4_doi_walk(u32 *skip_cnt,
691 int (*callback) (struct cipso_v4_doi *doi_def, void *arg),
692 void *cb_arg)
694 int ret_val = -ENOENT;
695 u32 doi_cnt = 0;
696 struct cipso_v4_doi *iter_doi;
698 rcu_read_lock();
699 list_for_each_entry_rcu(iter_doi, &cipso_v4_doi_list, list)
700 if (atomic_read(&iter_doi->refcount) > 0) {
701 if (doi_cnt++ < *skip_cnt)
702 continue;
703 ret_val = callback(iter_doi, cb_arg);
704 if (ret_val < 0) {
705 doi_cnt--;
706 goto doi_walk_return;
710 doi_walk_return:
711 rcu_read_unlock();
712 *skip_cnt = doi_cnt;
713 return ret_val;
717 * Label Mapping Functions
721 * cipso_v4_map_lvl_valid - Checks to see if the given level is understood
722 * @doi_def: the DOI definition
723 * @level: the level to check
725 * Description:
726 * Checks the given level against the given DOI definition and returns a
727 * negative value if the level does not have a valid mapping and a zero value
728 * if the level is defined by the DOI.
731 static int cipso_v4_map_lvl_valid(const struct cipso_v4_doi *doi_def, u8 level)
733 switch (doi_def->type) {
734 case CIPSO_V4_MAP_PASS:
735 return 0;
736 case CIPSO_V4_MAP_TRANS:
737 if (doi_def->map.std->lvl.cipso[level] < CIPSO_V4_INV_LVL)
738 return 0;
739 break;
742 return -EFAULT;
746 * cipso_v4_map_lvl_hton - Perform a level mapping from the host to the network
747 * @doi_def: the DOI definition
748 * @host_lvl: the host MLS level
749 * @net_lvl: the network/CIPSO MLS level
751 * Description:
752 * Perform a label mapping to translate a local MLS level to the correct
753 * CIPSO level using the given DOI definition. Returns zero on success,
754 * negative values otherwise.
757 static int cipso_v4_map_lvl_hton(const struct cipso_v4_doi *doi_def,
758 u32 host_lvl,
759 u32 *net_lvl)
761 switch (doi_def->type) {
762 case CIPSO_V4_MAP_PASS:
763 *net_lvl = host_lvl;
764 return 0;
765 case CIPSO_V4_MAP_TRANS:
766 if (host_lvl < doi_def->map.std->lvl.local_size &&
767 doi_def->map.std->lvl.local[host_lvl] < CIPSO_V4_INV_LVL) {
768 *net_lvl = doi_def->map.std->lvl.local[host_lvl];
769 return 0;
771 return -EPERM;
774 return -EINVAL;
778 * cipso_v4_map_lvl_ntoh - Perform a level mapping from the network to the host
779 * @doi_def: the DOI definition
780 * @net_lvl: the network/CIPSO MLS level
781 * @host_lvl: the host MLS level
783 * Description:
784 * Perform a label mapping to translate a CIPSO level to the correct local MLS
785 * level using the given DOI definition. Returns zero on success, negative
786 * values otherwise.
789 static int cipso_v4_map_lvl_ntoh(const struct cipso_v4_doi *doi_def,
790 u32 net_lvl,
791 u32 *host_lvl)
793 struct cipso_v4_std_map_tbl *map_tbl;
795 switch (doi_def->type) {
796 case CIPSO_V4_MAP_PASS:
797 *host_lvl = net_lvl;
798 return 0;
799 case CIPSO_V4_MAP_TRANS:
800 map_tbl = doi_def->map.std;
801 if (net_lvl < map_tbl->lvl.cipso_size &&
802 map_tbl->lvl.cipso[net_lvl] < CIPSO_V4_INV_LVL) {
803 *host_lvl = doi_def->map.std->lvl.cipso[net_lvl];
804 return 0;
806 return -EPERM;
809 return -EINVAL;
813 * cipso_v4_map_cat_rbm_valid - Checks to see if the category bitmap is valid
814 * @doi_def: the DOI definition
815 * @bitmap: category bitmap
816 * @bitmap_len: bitmap length in bytes
818 * Description:
819 * Checks the given category bitmap against the given DOI definition and
820 * returns a negative value if any of the categories in the bitmap do not have
821 * a valid mapping and a zero value if all of the categories are valid.
824 static int cipso_v4_map_cat_rbm_valid(const struct cipso_v4_doi *doi_def,
825 const unsigned char *bitmap,
826 u32 bitmap_len)
828 int cat = -1;
829 u32 bitmap_len_bits = bitmap_len * 8;
830 u32 cipso_cat_size;
831 u32 *cipso_array;
833 switch (doi_def->type) {
834 case CIPSO_V4_MAP_PASS:
835 return 0;
836 case CIPSO_V4_MAP_TRANS:
837 cipso_cat_size = doi_def->map.std->cat.cipso_size;
838 cipso_array = doi_def->map.std->cat.cipso;
839 for (;;) {
840 cat = cipso_v4_bitmap_walk(bitmap,
841 bitmap_len_bits,
842 cat + 1,
844 if (cat < 0)
845 break;
846 if (cat >= cipso_cat_size ||
847 cipso_array[cat] >= CIPSO_V4_INV_CAT)
848 return -EFAULT;
851 if (cat == -1)
852 return 0;
853 break;
856 return -EFAULT;
860 * cipso_v4_map_cat_rbm_hton - Perform a category mapping from host to network
861 * @doi_def: the DOI definition
862 * @secattr: the security attributes
863 * @net_cat: the zero'd out category bitmap in network/CIPSO format
864 * @net_cat_len: the length of the CIPSO bitmap in bytes
866 * Description:
867 * Perform a label mapping to translate a local MLS category bitmap to the
868 * correct CIPSO bitmap using the given DOI definition. Returns the minimum
869 * size in bytes of the network bitmap on success, negative values otherwise.
872 static int cipso_v4_map_cat_rbm_hton(const struct cipso_v4_doi *doi_def,
873 const struct netlbl_lsm_secattr *secattr,
874 unsigned char *net_cat,
875 u32 net_cat_len)
877 int host_spot = -1;
878 u32 net_spot = CIPSO_V4_INV_CAT;
879 u32 net_spot_max = 0;
880 u32 net_clen_bits = net_cat_len * 8;
881 u32 host_cat_size = 0;
882 u32 *host_cat_array = NULL;
884 if (doi_def->type == CIPSO_V4_MAP_TRANS) {
885 host_cat_size = doi_def->map.std->cat.local_size;
886 host_cat_array = doi_def->map.std->cat.local;
889 for (;;) {
890 host_spot = netlbl_secattr_catmap_walk(secattr->attr.mls.cat,
891 host_spot + 1);
892 if (host_spot < 0)
893 break;
895 switch (doi_def->type) {
896 case CIPSO_V4_MAP_PASS:
897 net_spot = host_spot;
898 break;
899 case CIPSO_V4_MAP_TRANS:
900 if (host_spot >= host_cat_size)
901 return -EPERM;
902 net_spot = host_cat_array[host_spot];
903 if (net_spot >= CIPSO_V4_INV_CAT)
904 return -EPERM;
905 break;
907 if (net_spot >= net_clen_bits)
908 return -ENOSPC;
909 cipso_v4_bitmap_setbit(net_cat, net_spot, 1);
911 if (net_spot > net_spot_max)
912 net_spot_max = net_spot;
915 if (++net_spot_max % 8)
916 return net_spot_max / 8 + 1;
917 return net_spot_max / 8;
921 * cipso_v4_map_cat_rbm_ntoh - Perform a category mapping from network to host
922 * @doi_def: the DOI definition
923 * @net_cat: the category bitmap in network/CIPSO format
924 * @net_cat_len: the length of the CIPSO bitmap in bytes
925 * @secattr: the security attributes
927 * Description:
928 * Perform a label mapping to translate a CIPSO bitmap to the correct local
929 * MLS category bitmap using the given DOI definition. Returns zero on
930 * success, negative values on failure.
933 static int cipso_v4_map_cat_rbm_ntoh(const struct cipso_v4_doi *doi_def,
934 const unsigned char *net_cat,
935 u32 net_cat_len,
936 struct netlbl_lsm_secattr *secattr)
938 int ret_val;
939 int net_spot = -1;
940 u32 host_spot = CIPSO_V4_INV_CAT;
941 u32 net_clen_bits = net_cat_len * 8;
942 u32 net_cat_size = 0;
943 u32 *net_cat_array = NULL;
945 if (doi_def->type == CIPSO_V4_MAP_TRANS) {
946 net_cat_size = doi_def->map.std->cat.cipso_size;
947 net_cat_array = doi_def->map.std->cat.cipso;
950 for (;;) {
951 net_spot = cipso_v4_bitmap_walk(net_cat,
952 net_clen_bits,
953 net_spot + 1,
955 if (net_spot < 0) {
956 if (net_spot == -2)
957 return -EFAULT;
958 return 0;
961 switch (doi_def->type) {
962 case CIPSO_V4_MAP_PASS:
963 host_spot = net_spot;
964 break;
965 case CIPSO_V4_MAP_TRANS:
966 if (net_spot >= net_cat_size)
967 return -EPERM;
968 host_spot = net_cat_array[net_spot];
969 if (host_spot >= CIPSO_V4_INV_CAT)
970 return -EPERM;
971 break;
973 ret_val = netlbl_secattr_catmap_setbit(secattr->attr.mls.cat,
974 host_spot,
975 GFP_ATOMIC);
976 if (ret_val != 0)
977 return ret_val;
980 return -EINVAL;
984 * cipso_v4_map_cat_enum_valid - Checks to see if the categories are valid
985 * @doi_def: the DOI definition
986 * @enumcat: category list
987 * @enumcat_len: length of the category list in bytes
989 * Description:
990 * Checks the given categories against the given DOI definition and returns a
991 * negative value if any of the categories do not have a valid mapping and a
992 * zero value if all of the categories are valid.
995 static int cipso_v4_map_cat_enum_valid(const struct cipso_v4_doi *doi_def,
996 const unsigned char *enumcat,
997 u32 enumcat_len)
999 u16 cat;
1000 int cat_prev = -1;
1001 u32 iter;
1003 if (doi_def->type != CIPSO_V4_MAP_PASS || enumcat_len & 0x01)
1004 return -EFAULT;
1006 for (iter = 0; iter < enumcat_len; iter += 2) {
1007 cat = get_unaligned_be16(&enumcat[iter]);
1008 if (cat <= cat_prev)
1009 return -EFAULT;
1010 cat_prev = cat;
1013 return 0;
1017 * cipso_v4_map_cat_enum_hton - Perform a category mapping from host to network
1018 * @doi_def: the DOI definition
1019 * @secattr: the security attributes
1020 * @net_cat: the zero'd out category list in network/CIPSO format
1021 * @net_cat_len: the length of the CIPSO category list in bytes
1023 * Description:
1024 * Perform a label mapping to translate a local MLS category bitmap to the
1025 * correct CIPSO category list using the given DOI definition. Returns the
1026 * size in bytes of the network category bitmap on success, negative values
1027 * otherwise.
1030 static int cipso_v4_map_cat_enum_hton(const struct cipso_v4_doi *doi_def,
1031 const struct netlbl_lsm_secattr *secattr,
1032 unsigned char *net_cat,
1033 u32 net_cat_len)
1035 int cat = -1;
1036 u32 cat_iter = 0;
1038 for (;;) {
1039 cat = netlbl_secattr_catmap_walk(secattr->attr.mls.cat,
1040 cat + 1);
1041 if (cat < 0)
1042 break;
1043 if ((cat_iter + 2) > net_cat_len)
1044 return -ENOSPC;
1046 *((__be16 *)&net_cat[cat_iter]) = htons(cat);
1047 cat_iter += 2;
1050 return cat_iter;
1054 * cipso_v4_map_cat_enum_ntoh - Perform a category mapping from network to host
1055 * @doi_def: the DOI definition
1056 * @net_cat: the category list in network/CIPSO format
1057 * @net_cat_len: the length of the CIPSO bitmap in bytes
1058 * @secattr: the security attributes
1060 * Description:
1061 * Perform a label mapping to translate a CIPSO category list to the correct
1062 * local MLS category bitmap using the given DOI definition. Returns zero on
1063 * success, negative values on failure.
1066 static int cipso_v4_map_cat_enum_ntoh(const struct cipso_v4_doi *doi_def,
1067 const unsigned char *net_cat,
1068 u32 net_cat_len,
1069 struct netlbl_lsm_secattr *secattr)
1071 int ret_val;
1072 u32 iter;
1074 for (iter = 0; iter < net_cat_len; iter += 2) {
1075 ret_val = netlbl_secattr_catmap_setbit(secattr->attr.mls.cat,
1076 get_unaligned_be16(&net_cat[iter]),
1077 GFP_ATOMIC);
1078 if (ret_val != 0)
1079 return ret_val;
1082 return 0;
1086 * cipso_v4_map_cat_rng_valid - Checks to see if the categories are valid
1087 * @doi_def: the DOI definition
1088 * @rngcat: category list
1089 * @rngcat_len: length of the category list in bytes
1091 * Description:
1092 * Checks the given categories against the given DOI definition and returns a
1093 * negative value if any of the categories do not have a valid mapping and a
1094 * zero value if all of the categories are valid.
1097 static int cipso_v4_map_cat_rng_valid(const struct cipso_v4_doi *doi_def,
1098 const unsigned char *rngcat,
1099 u32 rngcat_len)
1101 u16 cat_high;
1102 u16 cat_low;
1103 u32 cat_prev = CIPSO_V4_MAX_REM_CATS + 1;
1104 u32 iter;
1106 if (doi_def->type != CIPSO_V4_MAP_PASS || rngcat_len & 0x01)
1107 return -EFAULT;
1109 for (iter = 0; iter < rngcat_len; iter += 4) {
1110 cat_high = get_unaligned_be16(&rngcat[iter]);
1111 if ((iter + 4) <= rngcat_len)
1112 cat_low = get_unaligned_be16(&rngcat[iter + 2]);
1113 else
1114 cat_low = 0;
1116 if (cat_high > cat_prev)
1117 return -EFAULT;
1119 cat_prev = cat_low;
1122 return 0;
1126 * cipso_v4_map_cat_rng_hton - Perform a category mapping from host to network
1127 * @doi_def: the DOI definition
1128 * @secattr: the security attributes
1129 * @net_cat: the zero'd out category list in network/CIPSO format
1130 * @net_cat_len: the length of the CIPSO category list in bytes
1132 * Description:
1133 * Perform a label mapping to translate a local MLS category bitmap to the
1134 * correct CIPSO category list using the given DOI definition. Returns the
1135 * size in bytes of the network category bitmap on success, negative values
1136 * otherwise.
1139 static int cipso_v4_map_cat_rng_hton(const struct cipso_v4_doi *doi_def,
1140 const struct netlbl_lsm_secattr *secattr,
1141 unsigned char *net_cat,
1142 u32 net_cat_len)
1144 int iter = -1;
1145 u16 array[CIPSO_V4_TAG_RNG_CAT_MAX * 2];
1146 u32 array_cnt = 0;
1147 u32 cat_size = 0;
1149 /* make sure we don't overflow the 'array[]' variable */
1150 if (net_cat_len >
1151 (CIPSO_V4_OPT_LEN_MAX - CIPSO_V4_HDR_LEN - CIPSO_V4_TAG_RNG_BLEN))
1152 return -ENOSPC;
1154 for (;;) {
1155 iter = netlbl_secattr_catmap_walk(secattr->attr.mls.cat,
1156 iter + 1);
1157 if (iter < 0)
1158 break;
1159 cat_size += (iter == 0 ? 0 : sizeof(u16));
1160 if (cat_size > net_cat_len)
1161 return -ENOSPC;
1162 array[array_cnt++] = iter;
1164 iter = netlbl_secattr_catmap_walk_rng(secattr->attr.mls.cat,
1165 iter);
1166 if (iter < 0)
1167 return -EFAULT;
1168 cat_size += sizeof(u16);
1169 if (cat_size > net_cat_len)
1170 return -ENOSPC;
1171 array[array_cnt++] = iter;
1174 for (iter = 0; array_cnt > 0;) {
1175 *((__be16 *)&net_cat[iter]) = htons(array[--array_cnt]);
1176 iter += 2;
1177 array_cnt--;
1178 if (array[array_cnt] != 0) {
1179 *((__be16 *)&net_cat[iter]) = htons(array[array_cnt]);
1180 iter += 2;
1184 return cat_size;
1188 * cipso_v4_map_cat_rng_ntoh - Perform a category mapping from network to host
1189 * @doi_def: the DOI definition
1190 * @net_cat: the category list in network/CIPSO format
1191 * @net_cat_len: the length of the CIPSO bitmap in bytes
1192 * @secattr: the security attributes
1194 * Description:
1195 * Perform a label mapping to translate a CIPSO category list to the correct
1196 * local MLS category bitmap using the given DOI definition. Returns zero on
1197 * success, negative values on failure.
1200 static int cipso_v4_map_cat_rng_ntoh(const struct cipso_v4_doi *doi_def,
1201 const unsigned char *net_cat,
1202 u32 net_cat_len,
1203 struct netlbl_lsm_secattr *secattr)
1205 int ret_val;
1206 u32 net_iter;
1207 u16 cat_low;
1208 u16 cat_high;
1210 for (net_iter = 0; net_iter < net_cat_len; net_iter += 4) {
1211 cat_high = get_unaligned_be16(&net_cat[net_iter]);
1212 if ((net_iter + 4) <= net_cat_len)
1213 cat_low = get_unaligned_be16(&net_cat[net_iter + 2]);
1214 else
1215 cat_low = 0;
1217 ret_val = netlbl_secattr_catmap_setrng(secattr->attr.mls.cat,
1218 cat_low,
1219 cat_high,
1220 GFP_ATOMIC);
1221 if (ret_val != 0)
1222 return ret_val;
1225 return 0;
1229 * Protocol Handling Functions
1233 * cipso_v4_gentag_hdr - Generate a CIPSO option header
1234 * @doi_def: the DOI definition
1235 * @len: the total tag length in bytes, not including this header
1236 * @buf: the CIPSO option buffer
1238 * Description:
1239 * Write a CIPSO header into the beginning of @buffer.
1242 static void cipso_v4_gentag_hdr(const struct cipso_v4_doi *doi_def,
1243 unsigned char *buf,
1244 u32 len)
1246 buf[0] = IPOPT_CIPSO;
1247 buf[1] = CIPSO_V4_HDR_LEN + len;
1248 *(__be32 *)&buf[2] = htonl(doi_def->doi);
1252 * cipso_v4_gentag_rbm - Generate a CIPSO restricted bitmap tag (type #1)
1253 * @doi_def: the DOI definition
1254 * @secattr: the security attributes
1255 * @buffer: the option buffer
1256 * @buffer_len: length of buffer in bytes
1258 * Description:
1259 * Generate a CIPSO option using the restricted bitmap tag, tag type #1. The
1260 * actual buffer length may be larger than the indicated size due to
1261 * translation between host and network category bitmaps. Returns the size of
1262 * the tag on success, negative values on failure.
1265 static int cipso_v4_gentag_rbm(const struct cipso_v4_doi *doi_def,
1266 const struct netlbl_lsm_secattr *secattr,
1267 unsigned char *buffer,
1268 u32 buffer_len)
1270 int ret_val;
1271 u32 tag_len;
1272 u32 level;
1274 if ((secattr->flags & NETLBL_SECATTR_MLS_LVL) == 0)
1275 return -EPERM;
1277 ret_val = cipso_v4_map_lvl_hton(doi_def,
1278 secattr->attr.mls.lvl,
1279 &level);
1280 if (ret_val != 0)
1281 return ret_val;
1283 if (secattr->flags & NETLBL_SECATTR_MLS_CAT) {
1284 ret_val = cipso_v4_map_cat_rbm_hton(doi_def,
1285 secattr,
1286 &buffer[4],
1287 buffer_len - 4);
1288 if (ret_val < 0)
1289 return ret_val;
1291 /* This will send packets using the "optimized" format when
1292 * possibile as specified in section 3.4.2.6 of the
1293 * CIPSO draft. */
1294 if (cipso_v4_rbm_optfmt && ret_val > 0 && ret_val <= 10)
1295 tag_len = 14;
1296 else
1297 tag_len = 4 + ret_val;
1298 } else
1299 tag_len = 4;
1301 buffer[0] = CIPSO_V4_TAG_RBITMAP;
1302 buffer[1] = tag_len;
1303 buffer[3] = level;
1305 return tag_len;
1309 * cipso_v4_parsetag_rbm - Parse a CIPSO restricted bitmap tag
1310 * @doi_def: the DOI definition
1311 * @tag: the CIPSO tag
1312 * @secattr: the security attributes
1314 * Description:
1315 * Parse a CIPSO restricted bitmap tag (tag type #1) and return the security
1316 * attributes in @secattr. Return zero on success, negatives values on
1317 * failure.
1320 static int cipso_v4_parsetag_rbm(const struct cipso_v4_doi *doi_def,
1321 const unsigned char *tag,
1322 struct netlbl_lsm_secattr *secattr)
1324 int ret_val;
1325 u8 tag_len = tag[1];
1326 u32 level;
1328 ret_val = cipso_v4_map_lvl_ntoh(doi_def, tag[3], &level);
1329 if (ret_val != 0)
1330 return ret_val;
1331 secattr->attr.mls.lvl = level;
1332 secattr->flags |= NETLBL_SECATTR_MLS_LVL;
1334 if (tag_len > 4) {
1335 secattr->attr.mls.cat =
1336 netlbl_secattr_catmap_alloc(GFP_ATOMIC);
1337 if (secattr->attr.mls.cat == NULL)
1338 return -ENOMEM;
1340 ret_val = cipso_v4_map_cat_rbm_ntoh(doi_def,
1341 &tag[4],
1342 tag_len - 4,
1343 secattr);
1344 if (ret_val != 0) {
1345 netlbl_secattr_catmap_free(secattr->attr.mls.cat);
1346 return ret_val;
1349 secattr->flags |= NETLBL_SECATTR_MLS_CAT;
1352 return 0;
1356 * cipso_v4_gentag_enum - Generate a CIPSO enumerated tag (type #2)
1357 * @doi_def: the DOI definition
1358 * @secattr: the security attributes
1359 * @buffer: the option buffer
1360 * @buffer_len: length of buffer in bytes
1362 * Description:
1363 * Generate a CIPSO option using the enumerated tag, tag type #2. Returns the
1364 * size of the tag on success, negative values on failure.
1367 static int cipso_v4_gentag_enum(const struct cipso_v4_doi *doi_def,
1368 const struct netlbl_lsm_secattr *secattr,
1369 unsigned char *buffer,
1370 u32 buffer_len)
1372 int ret_val;
1373 u32 tag_len;
1374 u32 level;
1376 if (!(secattr->flags & NETLBL_SECATTR_MLS_LVL))
1377 return -EPERM;
1379 ret_val = cipso_v4_map_lvl_hton(doi_def,
1380 secattr->attr.mls.lvl,
1381 &level);
1382 if (ret_val != 0)
1383 return ret_val;
1385 if (secattr->flags & NETLBL_SECATTR_MLS_CAT) {
1386 ret_val = cipso_v4_map_cat_enum_hton(doi_def,
1387 secattr,
1388 &buffer[4],
1389 buffer_len - 4);
1390 if (ret_val < 0)
1391 return ret_val;
1393 tag_len = 4 + ret_val;
1394 } else
1395 tag_len = 4;
1397 buffer[0] = CIPSO_V4_TAG_ENUM;
1398 buffer[1] = tag_len;
1399 buffer[3] = level;
1401 return tag_len;
1405 * cipso_v4_parsetag_enum - Parse a CIPSO enumerated tag
1406 * @doi_def: the DOI definition
1407 * @tag: the CIPSO tag
1408 * @secattr: the security attributes
1410 * Description:
1411 * Parse a CIPSO enumerated tag (tag type #2) and return the security
1412 * attributes in @secattr. Return zero on success, negatives values on
1413 * failure.
1416 static int cipso_v4_parsetag_enum(const struct cipso_v4_doi *doi_def,
1417 const unsigned char *tag,
1418 struct netlbl_lsm_secattr *secattr)
1420 int ret_val;
1421 u8 tag_len = tag[1];
1422 u32 level;
1424 ret_val = cipso_v4_map_lvl_ntoh(doi_def, tag[3], &level);
1425 if (ret_val != 0)
1426 return ret_val;
1427 secattr->attr.mls.lvl = level;
1428 secattr->flags |= NETLBL_SECATTR_MLS_LVL;
1430 if (tag_len > 4) {
1431 secattr->attr.mls.cat =
1432 netlbl_secattr_catmap_alloc(GFP_ATOMIC);
1433 if (secattr->attr.mls.cat == NULL)
1434 return -ENOMEM;
1436 ret_val = cipso_v4_map_cat_enum_ntoh(doi_def,
1437 &tag[4],
1438 tag_len - 4,
1439 secattr);
1440 if (ret_val != 0) {
1441 netlbl_secattr_catmap_free(secattr->attr.mls.cat);
1442 return ret_val;
1445 secattr->flags |= NETLBL_SECATTR_MLS_CAT;
1448 return 0;
1452 * cipso_v4_gentag_rng - Generate a CIPSO ranged tag (type #5)
1453 * @doi_def: the DOI definition
1454 * @secattr: the security attributes
1455 * @buffer: the option buffer
1456 * @buffer_len: length of buffer in bytes
1458 * Description:
1459 * Generate a CIPSO option using the ranged tag, tag type #5. Returns the
1460 * size of the tag on success, negative values on failure.
1463 static int cipso_v4_gentag_rng(const struct cipso_v4_doi *doi_def,
1464 const struct netlbl_lsm_secattr *secattr,
1465 unsigned char *buffer,
1466 u32 buffer_len)
1468 int ret_val;
1469 u32 tag_len;
1470 u32 level;
1472 if (!(secattr->flags & NETLBL_SECATTR_MLS_LVL))
1473 return -EPERM;
1475 ret_val = cipso_v4_map_lvl_hton(doi_def,
1476 secattr->attr.mls.lvl,
1477 &level);
1478 if (ret_val != 0)
1479 return ret_val;
1481 if (secattr->flags & NETLBL_SECATTR_MLS_CAT) {
1482 ret_val = cipso_v4_map_cat_rng_hton(doi_def,
1483 secattr,
1484 &buffer[4],
1485 buffer_len - 4);
1486 if (ret_val < 0)
1487 return ret_val;
1489 tag_len = 4 + ret_val;
1490 } else
1491 tag_len = 4;
1493 buffer[0] = CIPSO_V4_TAG_RANGE;
1494 buffer[1] = tag_len;
1495 buffer[3] = level;
1497 return tag_len;
1501 * cipso_v4_parsetag_rng - Parse a CIPSO ranged tag
1502 * @doi_def: the DOI definition
1503 * @tag: the CIPSO tag
1504 * @secattr: the security attributes
1506 * Description:
1507 * Parse a CIPSO ranged tag (tag type #5) and return the security attributes
1508 * in @secattr. Return zero on success, negatives values on failure.
1511 static int cipso_v4_parsetag_rng(const struct cipso_v4_doi *doi_def,
1512 const unsigned char *tag,
1513 struct netlbl_lsm_secattr *secattr)
1515 int ret_val;
1516 u8 tag_len = tag[1];
1517 u32 level;
1519 ret_val = cipso_v4_map_lvl_ntoh(doi_def, tag[3], &level);
1520 if (ret_val != 0)
1521 return ret_val;
1522 secattr->attr.mls.lvl = level;
1523 secattr->flags |= NETLBL_SECATTR_MLS_LVL;
1525 if (tag_len > 4) {
1526 secattr->attr.mls.cat =
1527 netlbl_secattr_catmap_alloc(GFP_ATOMIC);
1528 if (secattr->attr.mls.cat == NULL)
1529 return -ENOMEM;
1531 ret_val = cipso_v4_map_cat_rng_ntoh(doi_def,
1532 &tag[4],
1533 tag_len - 4,
1534 secattr);
1535 if (ret_val != 0) {
1536 netlbl_secattr_catmap_free(secattr->attr.mls.cat);
1537 return ret_val;
1540 secattr->flags |= NETLBL_SECATTR_MLS_CAT;
1543 return 0;
1547 * cipso_v4_gentag_loc - Generate a CIPSO local tag (non-standard)
1548 * @doi_def: the DOI definition
1549 * @secattr: the security attributes
1550 * @buffer: the option buffer
1551 * @buffer_len: length of buffer in bytes
1553 * Description:
1554 * Generate a CIPSO option using the local tag. Returns the size of the tag
1555 * on success, negative values on failure.
1558 static int cipso_v4_gentag_loc(const struct cipso_v4_doi *doi_def,
1559 const struct netlbl_lsm_secattr *secattr,
1560 unsigned char *buffer,
1561 u32 buffer_len)
1563 if (!(secattr->flags & NETLBL_SECATTR_SECID))
1564 return -EPERM;
1566 buffer[0] = CIPSO_V4_TAG_LOCAL;
1567 buffer[1] = CIPSO_V4_TAG_LOC_BLEN;
1568 *(u32 *)&buffer[2] = secattr->attr.secid;
1570 return CIPSO_V4_TAG_LOC_BLEN;
1574 * cipso_v4_parsetag_loc - Parse a CIPSO local tag
1575 * @doi_def: the DOI definition
1576 * @tag: the CIPSO tag
1577 * @secattr: the security attributes
1579 * Description:
1580 * Parse a CIPSO local tag and return the security attributes in @secattr.
1581 * Return zero on success, negatives values on failure.
1584 static int cipso_v4_parsetag_loc(const struct cipso_v4_doi *doi_def,
1585 const unsigned char *tag,
1586 struct netlbl_lsm_secattr *secattr)
1588 secattr->attr.secid = *(u32 *)&tag[2];
1589 secattr->flags |= NETLBL_SECATTR_SECID;
1591 return 0;
1595 * cipso_v4_validate - Validate a CIPSO option
1596 * @option: the start of the option, on error it is set to point to the error
1598 * Description:
1599 * This routine is called to validate a CIPSO option, it checks all of the
1600 * fields to ensure that they are at least valid, see the draft snippet below
1601 * for details. If the option is valid then a zero value is returned and
1602 * the value of @option is unchanged. If the option is invalid then a
1603 * non-zero value is returned and @option is adjusted to point to the
1604 * offending portion of the option. From the IETF draft ...
1606 * "If any field within the CIPSO options, such as the DOI identifier, is not
1607 * recognized the IP datagram is discarded and an ICMP 'parameter problem'
1608 * (type 12) is generated and returned. The ICMP code field is set to 'bad
1609 * parameter' (code 0) and the pointer is set to the start of the CIPSO field
1610 * that is unrecognized."
1613 int cipso_v4_validate(const struct sk_buff *skb, unsigned char **option)
1615 unsigned char *opt = *option;
1616 unsigned char *tag;
1617 unsigned char opt_iter;
1618 unsigned char err_offset = 0;
1619 u8 opt_len;
1620 u8 tag_len;
1621 struct cipso_v4_doi *doi_def = NULL;
1622 u32 tag_iter;
1624 /* caller already checks for length values that are too large */
1625 opt_len = opt[1];
1626 if (opt_len < 8) {
1627 err_offset = 1;
1628 goto validate_return;
1631 rcu_read_lock();
1632 doi_def = cipso_v4_doi_search(get_unaligned_be32(&opt[2]));
1633 if (doi_def == NULL) {
1634 err_offset = 2;
1635 goto validate_return_locked;
1638 opt_iter = CIPSO_V4_HDR_LEN;
1639 tag = opt + opt_iter;
1640 while (opt_iter < opt_len) {
1641 for (tag_iter = 0; doi_def->tags[tag_iter] != tag[0];)
1642 if (doi_def->tags[tag_iter] == CIPSO_V4_TAG_INVALID ||
1643 ++tag_iter == CIPSO_V4_TAG_MAXCNT) {
1644 err_offset = opt_iter;
1645 goto validate_return_locked;
1648 tag_len = tag[1];
1649 if (tag_len > (opt_len - opt_iter)) {
1650 err_offset = opt_iter + 1;
1651 goto validate_return_locked;
1654 switch (tag[0]) {
1655 case CIPSO_V4_TAG_RBITMAP:
1656 if (tag_len < CIPSO_V4_TAG_RBM_BLEN) {
1657 err_offset = opt_iter + 1;
1658 goto validate_return_locked;
1661 /* We are already going to do all the verification
1662 * necessary at the socket layer so from our point of
1663 * view it is safe to turn these checks off (and less
1664 * work), however, the CIPSO draft says we should do
1665 * all the CIPSO validations here but it doesn't
1666 * really specify _exactly_ what we need to validate
1667 * ... so, just make it a sysctl tunable. */
1668 if (cipso_v4_rbm_strictvalid) {
1669 if (cipso_v4_map_lvl_valid(doi_def,
1670 tag[3]) < 0) {
1671 err_offset = opt_iter + 3;
1672 goto validate_return_locked;
1674 if (tag_len > CIPSO_V4_TAG_RBM_BLEN &&
1675 cipso_v4_map_cat_rbm_valid(doi_def,
1676 &tag[4],
1677 tag_len - 4) < 0) {
1678 err_offset = opt_iter + 4;
1679 goto validate_return_locked;
1682 break;
1683 case CIPSO_V4_TAG_ENUM:
1684 if (tag_len < CIPSO_V4_TAG_ENUM_BLEN) {
1685 err_offset = opt_iter + 1;
1686 goto validate_return_locked;
1689 if (cipso_v4_map_lvl_valid(doi_def,
1690 tag[3]) < 0) {
1691 err_offset = opt_iter + 3;
1692 goto validate_return_locked;
1694 if (tag_len > CIPSO_V4_TAG_ENUM_BLEN &&
1695 cipso_v4_map_cat_enum_valid(doi_def,
1696 &tag[4],
1697 tag_len - 4) < 0) {
1698 err_offset = opt_iter + 4;
1699 goto validate_return_locked;
1701 break;
1702 case CIPSO_V4_TAG_RANGE:
1703 if (tag_len < CIPSO_V4_TAG_RNG_BLEN) {
1704 err_offset = opt_iter + 1;
1705 goto validate_return_locked;
1708 if (cipso_v4_map_lvl_valid(doi_def,
1709 tag[3]) < 0) {
1710 err_offset = opt_iter + 3;
1711 goto validate_return_locked;
1713 if (tag_len > CIPSO_V4_TAG_RNG_BLEN &&
1714 cipso_v4_map_cat_rng_valid(doi_def,
1715 &tag[4],
1716 tag_len - 4) < 0) {
1717 err_offset = opt_iter + 4;
1718 goto validate_return_locked;
1720 break;
1721 case CIPSO_V4_TAG_LOCAL:
1722 /* This is a non-standard tag that we only allow for
1723 * local connections, so if the incoming interface is
1724 * not the loopback device drop the packet. */
1725 if (!(skb->dev->flags & IFF_LOOPBACK)) {
1726 err_offset = opt_iter;
1727 goto validate_return_locked;
1729 if (tag_len != CIPSO_V4_TAG_LOC_BLEN) {
1730 err_offset = opt_iter + 1;
1731 goto validate_return_locked;
1733 break;
1734 default:
1735 err_offset = opt_iter;
1736 goto validate_return_locked;
1739 tag += tag_len;
1740 opt_iter += tag_len;
1743 validate_return_locked:
1744 rcu_read_unlock();
1745 validate_return:
1746 *option = opt + err_offset;
1747 return err_offset;
1751 * cipso_v4_error - Send the correct reponse for a bad packet
1752 * @skb: the packet
1753 * @error: the error code
1754 * @gateway: CIPSO gateway flag
1756 * Description:
1757 * Based on the error code given in @error, send an ICMP error message back to
1758 * the originating host. From the IETF draft ...
1760 * "If the contents of the CIPSO [option] are valid but the security label is
1761 * outside of the configured host or port label range, the datagram is
1762 * discarded and an ICMP 'destination unreachable' (type 3) is generated and
1763 * returned. The code field of the ICMP is set to 'communication with
1764 * destination network administratively prohibited' (code 9) or to
1765 * 'communication with destination host administratively prohibited'
1766 * (code 10). The value of the code is dependent on whether the originator
1767 * of the ICMP message is acting as a CIPSO host or a CIPSO gateway. The
1768 * recipient of the ICMP message MUST be able to handle either value. The
1769 * same procedure is performed if a CIPSO [option] can not be added to an
1770 * IP packet because it is too large to fit in the IP options area."
1772 * "If the error is triggered by receipt of an ICMP message, the message is
1773 * discarded and no response is permitted (consistent with general ICMP
1774 * processing rules)."
1777 void cipso_v4_error(struct sk_buff *skb, int error, u32 gateway)
1779 if (ip_hdr(skb)->protocol == IPPROTO_ICMP || error != -EACCES)
1780 return;
1782 if (gateway)
1783 icmp_send(skb, ICMP_DEST_UNREACH, ICMP_NET_ANO, 0);
1784 else
1785 icmp_send(skb, ICMP_DEST_UNREACH, ICMP_HOST_ANO, 0);
1789 * cipso_v4_genopt - Generate a CIPSO option
1790 * @buf: the option buffer
1791 * @buf_len: the size of opt_buf
1792 * @doi_def: the CIPSO DOI to use
1793 * @secattr: the security attributes
1795 * Description:
1796 * Generate a CIPSO option using the DOI definition and security attributes
1797 * passed to the function. Returns the length of the option on success and
1798 * negative values on failure.
1801 static int cipso_v4_genopt(unsigned char *buf, u32 buf_len,
1802 const struct cipso_v4_doi *doi_def,
1803 const struct netlbl_lsm_secattr *secattr)
1805 int ret_val;
1806 u32 iter;
1808 if (buf_len <= CIPSO_V4_HDR_LEN)
1809 return -ENOSPC;
1811 iter = 0;
1812 do {
1813 memset(buf, 0, buf_len);
1814 switch (doi_def->tags[iter]) {
1815 case CIPSO_V4_TAG_RBITMAP:
1816 ret_val = cipso_v4_gentag_rbm(doi_def,
1817 secattr,
1818 &buf[CIPSO_V4_HDR_LEN],
1819 buf_len - CIPSO_V4_HDR_LEN);
1820 break;
1821 case CIPSO_V4_TAG_ENUM:
1822 ret_val = cipso_v4_gentag_enum(doi_def,
1823 secattr,
1824 &buf[CIPSO_V4_HDR_LEN],
1825 buf_len - CIPSO_V4_HDR_LEN);
1826 break;
1827 case CIPSO_V4_TAG_RANGE:
1828 ret_val = cipso_v4_gentag_rng(doi_def,
1829 secattr,
1830 &buf[CIPSO_V4_HDR_LEN],
1831 buf_len - CIPSO_V4_HDR_LEN);
1832 break;
1833 case CIPSO_V4_TAG_LOCAL:
1834 ret_val = cipso_v4_gentag_loc(doi_def,
1835 secattr,
1836 &buf[CIPSO_V4_HDR_LEN],
1837 buf_len - CIPSO_V4_HDR_LEN);
1838 break;
1839 default:
1840 return -EPERM;
1843 iter++;
1844 } while (ret_val < 0 &&
1845 iter < CIPSO_V4_TAG_MAXCNT &&
1846 doi_def->tags[iter] != CIPSO_V4_TAG_INVALID);
1847 if (ret_val < 0)
1848 return ret_val;
1849 cipso_v4_gentag_hdr(doi_def, buf, ret_val);
1850 return CIPSO_V4_HDR_LEN + ret_val;
1854 * cipso_v4_sock_setattr - Add a CIPSO option to a socket
1855 * @sk: the socket
1856 * @doi_def: the CIPSO DOI to use
1857 * @secattr: the specific security attributes of the socket
1859 * Description:
1860 * Set the CIPSO option on the given socket using the DOI definition and
1861 * security attributes passed to the function. This function requires
1862 * exclusive access to @sk, which means it either needs to be in the
1863 * process of being created or locked. Returns zero on success and negative
1864 * values on failure.
1867 int cipso_v4_sock_setattr(struct sock *sk,
1868 const struct cipso_v4_doi *doi_def,
1869 const struct netlbl_lsm_secattr *secattr)
1871 int ret_val = -EPERM;
1872 unsigned char *buf = NULL;
1873 u32 buf_len;
1874 u32 opt_len;
1875 struct ip_options *opt = NULL;
1876 struct inet_sock *sk_inet;
1877 struct inet_connection_sock *sk_conn;
1879 /* In the case of sock_create_lite(), the sock->sk field is not
1880 * defined yet but it is not a problem as the only users of these
1881 * "lite" PF_INET sockets are functions which do an accept() call
1882 * afterwards so we will label the socket as part of the accept(). */
1883 if (sk == NULL)
1884 return 0;
1886 /* We allocate the maximum CIPSO option size here so we are probably
1887 * being a little wasteful, but it makes our life _much_ easier later
1888 * on and after all we are only talking about 40 bytes. */
1889 buf_len = CIPSO_V4_OPT_LEN_MAX;
1890 buf = kmalloc(buf_len, GFP_ATOMIC);
1891 if (buf == NULL) {
1892 ret_val = -ENOMEM;
1893 goto socket_setattr_failure;
1896 ret_val = cipso_v4_genopt(buf, buf_len, doi_def, secattr);
1897 if (ret_val < 0)
1898 goto socket_setattr_failure;
1899 buf_len = ret_val;
1901 /* We can't use ip_options_get() directly because it makes a call to
1902 * ip_options_get_alloc() which allocates memory with GFP_KERNEL and
1903 * we won't always have CAP_NET_RAW even though we _always_ want to
1904 * set the IPOPT_CIPSO option. */
1905 opt_len = (buf_len + 3) & ~3;
1906 opt = kzalloc(sizeof(*opt) + opt_len, GFP_ATOMIC);
1907 if (opt == NULL) {
1908 ret_val = -ENOMEM;
1909 goto socket_setattr_failure;
1911 memcpy(opt->__data, buf, buf_len);
1912 opt->optlen = opt_len;
1913 opt->cipso = sizeof(struct iphdr);
1914 kfree(buf);
1915 buf = NULL;
1917 sk_inet = inet_sk(sk);
1918 if (sk_inet->is_icsk) {
1919 sk_conn = inet_csk(sk);
1920 if (sk_inet->opt)
1921 sk_conn->icsk_ext_hdr_len -= sk_inet->opt->optlen;
1922 sk_conn->icsk_ext_hdr_len += opt->optlen;
1923 sk_conn->icsk_sync_mss(sk, sk_conn->icsk_pmtu_cookie);
1925 opt = xchg(&sk_inet->opt, opt);
1926 kfree(opt);
1928 return 0;
1930 socket_setattr_failure:
1931 kfree(buf);
1932 kfree(opt);
1933 return ret_val;
1937 * cipso_v4_req_setattr - Add a CIPSO option to a connection request socket
1938 * @req: the connection request socket
1939 * @doi_def: the CIPSO DOI to use
1940 * @secattr: the specific security attributes of the socket
1942 * Description:
1943 * Set the CIPSO option on the given socket using the DOI definition and
1944 * security attributes passed to the function. Returns zero on success and
1945 * negative values on failure.
1948 int cipso_v4_req_setattr(struct request_sock *req,
1949 const struct cipso_v4_doi *doi_def,
1950 const struct netlbl_lsm_secattr *secattr)
1952 int ret_val = -EPERM;
1953 unsigned char *buf = NULL;
1954 u32 buf_len;
1955 u32 opt_len;
1956 struct ip_options *opt = NULL;
1957 struct inet_request_sock *req_inet;
1959 /* We allocate the maximum CIPSO option size here so we are probably
1960 * being a little wasteful, but it makes our life _much_ easier later
1961 * on and after all we are only talking about 40 bytes. */
1962 buf_len = CIPSO_V4_OPT_LEN_MAX;
1963 buf = kmalloc(buf_len, GFP_ATOMIC);
1964 if (buf == NULL) {
1965 ret_val = -ENOMEM;
1966 goto req_setattr_failure;
1969 ret_val = cipso_v4_genopt(buf, buf_len, doi_def, secattr);
1970 if (ret_val < 0)
1971 goto req_setattr_failure;
1972 buf_len = ret_val;
1974 /* We can't use ip_options_get() directly because it makes a call to
1975 * ip_options_get_alloc() which allocates memory with GFP_KERNEL and
1976 * we won't always have CAP_NET_RAW even though we _always_ want to
1977 * set the IPOPT_CIPSO option. */
1978 opt_len = (buf_len + 3) & ~3;
1979 opt = kzalloc(sizeof(*opt) + opt_len, GFP_ATOMIC);
1980 if (opt == NULL) {
1981 ret_val = -ENOMEM;
1982 goto req_setattr_failure;
1984 memcpy(opt->__data, buf, buf_len);
1985 opt->optlen = opt_len;
1986 opt->cipso = sizeof(struct iphdr);
1987 kfree(buf);
1988 buf = NULL;
1990 req_inet = inet_rsk(req);
1991 opt = xchg(&req_inet->opt, opt);
1992 kfree(opt);
1994 return 0;
1996 req_setattr_failure:
1997 kfree(buf);
1998 kfree(opt);
1999 return ret_val;
2003 * cipso_v4_delopt - Delete the CIPSO option from a set of IP options
2004 * @opt_ptr: IP option pointer
2006 * Description:
2007 * Deletes the CIPSO IP option from a set of IP options and makes the necessary
2008 * adjustments to the IP option structure. Returns zero on success, negative
2009 * values on failure.
2012 static int cipso_v4_delopt(struct ip_options **opt_ptr)
2014 int hdr_delta = 0;
2015 struct ip_options *opt = *opt_ptr;
2017 if (opt->srr || opt->rr || opt->ts || opt->router_alert) {
2018 u8 cipso_len;
2019 u8 cipso_off;
2020 unsigned char *cipso_ptr;
2021 int iter;
2022 int optlen_new;
2024 cipso_off = opt->cipso - sizeof(struct iphdr);
2025 cipso_ptr = &opt->__data[cipso_off];
2026 cipso_len = cipso_ptr[1];
2028 if (opt->srr > opt->cipso)
2029 opt->srr -= cipso_len;
2030 if (opt->rr > opt->cipso)
2031 opt->rr -= cipso_len;
2032 if (opt->ts > opt->cipso)
2033 opt->ts -= cipso_len;
2034 if (opt->router_alert > opt->cipso)
2035 opt->router_alert -= cipso_len;
2036 opt->cipso = 0;
2038 memmove(cipso_ptr, cipso_ptr + cipso_len,
2039 opt->optlen - cipso_off - cipso_len);
2041 /* determining the new total option length is tricky because of
2042 * the padding necessary, the only thing i can think to do at
2043 * this point is walk the options one-by-one, skipping the
2044 * padding at the end to determine the actual option size and
2045 * from there we can determine the new total option length */
2046 iter = 0;
2047 optlen_new = 0;
2048 while (iter < opt->optlen)
2049 if (opt->__data[iter] != IPOPT_NOP) {
2050 iter += opt->__data[iter + 1];
2051 optlen_new = iter;
2052 } else
2053 iter++;
2054 hdr_delta = opt->optlen;
2055 opt->optlen = (optlen_new + 3) & ~3;
2056 hdr_delta -= opt->optlen;
2057 } else {
2058 /* only the cipso option was present on the socket so we can
2059 * remove the entire option struct */
2060 *opt_ptr = NULL;
2061 hdr_delta = opt->optlen;
2062 kfree(opt);
2065 return hdr_delta;
2069 * cipso_v4_sock_delattr - Delete the CIPSO option from a socket
2070 * @sk: the socket
2072 * Description:
2073 * Removes the CIPSO option from a socket, if present.
2076 void cipso_v4_sock_delattr(struct sock *sk)
2078 int hdr_delta;
2079 struct ip_options *opt;
2080 struct inet_sock *sk_inet;
2082 sk_inet = inet_sk(sk);
2083 opt = sk_inet->opt;
2084 if (opt == NULL || opt->cipso == 0)
2085 return;
2087 hdr_delta = cipso_v4_delopt(&sk_inet->opt);
2088 if (sk_inet->is_icsk && hdr_delta > 0) {
2089 struct inet_connection_sock *sk_conn = inet_csk(sk);
2090 sk_conn->icsk_ext_hdr_len -= hdr_delta;
2091 sk_conn->icsk_sync_mss(sk, sk_conn->icsk_pmtu_cookie);
2096 * cipso_v4_req_delattr - Delete the CIPSO option from a request socket
2097 * @reg: the request socket
2099 * Description:
2100 * Removes the CIPSO option from a request socket, if present.
2103 void cipso_v4_req_delattr(struct request_sock *req)
2105 struct ip_options *opt;
2106 struct inet_request_sock *req_inet;
2108 req_inet = inet_rsk(req);
2109 opt = req_inet->opt;
2110 if (opt == NULL || opt->cipso == 0)
2111 return;
2113 cipso_v4_delopt(&req_inet->opt);
2117 * cipso_v4_getattr - Helper function for the cipso_v4_*_getattr functions
2118 * @cipso: the CIPSO v4 option
2119 * @secattr: the security attributes
2121 * Description:
2122 * Inspect @cipso and return the security attributes in @secattr. Returns zero
2123 * on success and negative values on failure.
2126 static int cipso_v4_getattr(const unsigned char *cipso,
2127 struct netlbl_lsm_secattr *secattr)
2129 int ret_val = -ENOMSG;
2130 u32 doi;
2131 struct cipso_v4_doi *doi_def;
2133 if (cipso_v4_cache_check(cipso, cipso[1], secattr) == 0)
2134 return 0;
2136 doi = get_unaligned_be32(&cipso[2]);
2137 rcu_read_lock();
2138 doi_def = cipso_v4_doi_search(doi);
2139 if (doi_def == NULL)
2140 goto getattr_return;
2141 switch (cipso[6]) {
2142 case CIPSO_V4_TAG_RBITMAP:
2143 ret_val = cipso_v4_parsetag_rbm(doi_def, &cipso[6], secattr);
2144 break;
2145 case CIPSO_V4_TAG_ENUM:
2146 ret_val = cipso_v4_parsetag_enum(doi_def, &cipso[6], secattr);
2147 break;
2148 case CIPSO_V4_TAG_RANGE:
2149 ret_val = cipso_v4_parsetag_rng(doi_def, &cipso[6], secattr);
2150 break;
2151 case CIPSO_V4_TAG_LOCAL:
2152 ret_val = cipso_v4_parsetag_loc(doi_def, &cipso[6], secattr);
2153 break;
2155 if (ret_val == 0)
2156 secattr->type = NETLBL_NLTYPE_CIPSOV4;
2158 getattr_return:
2159 rcu_read_unlock();
2160 return ret_val;
2164 * cipso_v4_sock_getattr - Get the security attributes from a sock
2165 * @sk: the sock
2166 * @secattr: the security attributes
2168 * Description:
2169 * Query @sk to see if there is a CIPSO option attached to the sock and if
2170 * there is return the CIPSO security attributes in @secattr. This function
2171 * requires that @sk be locked, or privately held, but it does not do any
2172 * locking itself. Returns zero on success and negative values on failure.
2175 int cipso_v4_sock_getattr(struct sock *sk, struct netlbl_lsm_secattr *secattr)
2177 struct ip_options *opt;
2179 opt = inet_sk(sk)->opt;
2180 if (opt == NULL || opt->cipso == 0)
2181 return -ENOMSG;
2183 return cipso_v4_getattr(opt->__data + opt->cipso - sizeof(struct iphdr),
2184 secattr);
2188 * cipso_v4_skbuff_setattr - Set the CIPSO option on a packet
2189 * @skb: the packet
2190 * @secattr: the security attributes
2192 * Description:
2193 * Set the CIPSO option on the given packet based on the security attributes.
2194 * Returns a pointer to the IP header on success and NULL on failure.
2197 int cipso_v4_skbuff_setattr(struct sk_buff *skb,
2198 const struct cipso_v4_doi *doi_def,
2199 const struct netlbl_lsm_secattr *secattr)
2201 int ret_val;
2202 struct iphdr *iph;
2203 struct ip_options *opt = &IPCB(skb)->opt;
2204 unsigned char buf[CIPSO_V4_OPT_LEN_MAX];
2205 u32 buf_len = CIPSO_V4_OPT_LEN_MAX;
2206 u32 opt_len;
2207 int len_delta;
2209 ret_val = cipso_v4_genopt(buf, buf_len, doi_def, secattr);
2210 if (ret_val < 0)
2211 return ret_val;
2212 buf_len = ret_val;
2213 opt_len = (buf_len + 3) & ~3;
2215 /* we overwrite any existing options to ensure that we have enough
2216 * room for the CIPSO option, the reason is that we _need_ to guarantee
2217 * that the security label is applied to the packet - we do the same
2218 * thing when using the socket options and it hasn't caused a problem,
2219 * if we need to we can always revisit this choice later */
2221 len_delta = opt_len - opt->optlen;
2222 /* if we don't ensure enough headroom we could panic on the skb_push()
2223 * call below so make sure we have enough, we are also "mangling" the
2224 * packet so we should probably do a copy-on-write call anyway */
2225 ret_val = skb_cow(skb, skb_headroom(skb) + len_delta);
2226 if (ret_val < 0)
2227 return ret_val;
2229 if (len_delta > 0) {
2230 /* we assume that the header + opt->optlen have already been
2231 * "pushed" in ip_options_build() or similar */
2232 iph = ip_hdr(skb);
2233 skb_push(skb, len_delta);
2234 memmove((char *)iph - len_delta, iph, iph->ihl << 2);
2235 skb_reset_network_header(skb);
2236 iph = ip_hdr(skb);
2237 } else if (len_delta < 0) {
2238 iph = ip_hdr(skb);
2239 memset(iph + 1, IPOPT_NOP, opt->optlen);
2240 } else
2241 iph = ip_hdr(skb);
2243 if (opt->optlen > 0)
2244 memset(opt, 0, sizeof(*opt));
2245 opt->optlen = opt_len;
2246 opt->cipso = sizeof(struct iphdr);
2247 opt->is_changed = 1;
2249 /* we have to do the following because we are being called from a
2250 * netfilter hook which means the packet already has had the header
2251 * fields populated and the checksum calculated - yes this means we
2252 * are doing more work than needed but we do it to keep the core
2253 * stack clean and tidy */
2254 memcpy(iph + 1, buf, buf_len);
2255 if (opt_len > buf_len)
2256 memset((char *)(iph + 1) + buf_len, 0, opt_len - buf_len);
2257 if (len_delta != 0) {
2258 iph->ihl = 5 + (opt_len >> 2);
2259 iph->tot_len = htons(skb->len);
2261 ip_send_check(iph);
2263 return 0;
2267 * cipso_v4_skbuff_delattr - Delete any CIPSO options from a packet
2268 * @skb: the packet
2270 * Description:
2271 * Removes any and all CIPSO options from the given packet. Returns zero on
2272 * success, negative values on failure.
2275 int cipso_v4_skbuff_delattr(struct sk_buff *skb)
2277 int ret_val;
2278 struct iphdr *iph;
2279 struct ip_options *opt = &IPCB(skb)->opt;
2280 unsigned char *cipso_ptr;
2282 if (opt->cipso == 0)
2283 return 0;
2285 /* since we are changing the packet we should make a copy */
2286 ret_val = skb_cow(skb, skb_headroom(skb));
2287 if (ret_val < 0)
2288 return ret_val;
2290 /* the easiest thing to do is just replace the cipso option with noop
2291 * options since we don't change the size of the packet, although we
2292 * still need to recalculate the checksum */
2294 iph = ip_hdr(skb);
2295 cipso_ptr = (unsigned char *)iph + opt->cipso;
2296 memset(cipso_ptr, IPOPT_NOOP, cipso_ptr[1]);
2297 opt->cipso = 0;
2298 opt->is_changed = 1;
2300 ip_send_check(iph);
2302 return 0;
2306 * cipso_v4_skbuff_getattr - Get the security attributes from the CIPSO option
2307 * @skb: the packet
2308 * @secattr: the security attributes
2310 * Description:
2311 * Parse the given packet's CIPSO option and return the security attributes.
2312 * Returns zero on success and negative values on failure.
2315 int cipso_v4_skbuff_getattr(const struct sk_buff *skb,
2316 struct netlbl_lsm_secattr *secattr)
2318 return cipso_v4_getattr(CIPSO_V4_OPTPTR(skb), secattr);
2322 * Setup Functions
2326 * cipso_v4_init - Initialize the CIPSO module
2328 * Description:
2329 * Initialize the CIPSO module and prepare it for use. Returns zero on success
2330 * and negative values on failure.
2333 static int __init cipso_v4_init(void)
2335 int ret_val;
2337 ret_val = cipso_v4_cache_init();
2338 if (ret_val != 0)
2339 panic("Failed to initialize the CIPSO/IPv4 cache (%d)\n",
2340 ret_val);
2342 return 0;
2345 subsys_initcall(cipso_v4_init);