2 * CALIPSO - Common Architecture Label IPv6 Security Option
4 * This is an implementation of the CALIPSO protocol as specified in
7 * Authors: Paul Moore <paul.moore@hp.com>
8 * Huw Davies <huw@codeweavers.com>
12 /* (c) Copyright Hewlett-Packard Development Company, L.P., 2006, 2008
13 * (c) Copyright Huw Davies <huw@codeweavers.com>, 2015
15 * This program is free software; you can redistribute it and/or modify
16 * it under the terms of the GNU General Public License as published by
17 * the Free Software Foundation; either version 2 of the License, or
18 * (at your option) any later version.
20 * This program is distributed in the hope that it will be useful,
21 * but WITHOUT ANY WARRANTY; without even the implied warranty of
22 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See
23 * the GNU General Public License for more details.
25 * You should have received a copy of the GNU General Public License
26 * along with this program; if not, see <http://www.gnu.org/licenses/>.
30 #include <linux/init.h>
31 #include <linux/types.h>
32 #include <linux/rcupdate.h>
33 #include <linux/list.h>
34 #include <linux/spinlock.h>
35 #include <linux/string.h>
36 #include <linux/jhash.h>
37 #include <linux/audit.h>
38 #include <linux/slab.h>
42 #include <net/netlabel.h>
43 #include <net/calipso.h>
44 #include <linux/atomic.h>
45 #include <linux/bug.h>
46 #include <asm/unaligned.h>
47 #include <linux/crc-ccitt.h>
49 /* Maximium size of the calipso option including
50 * the two-byte TLV header.
52 #define CALIPSO_OPT_LEN_MAX (2 + 252)
54 /* Size of the minimum calipso option including
55 * the two-byte TLV header.
57 #define CALIPSO_HDR_LEN (2 + 8)
59 /* Maximium size of the calipso option including
60 * the two-byte TLV header and upto 3 bytes of
61 * leading pad and 7 bytes of trailing pad.
63 #define CALIPSO_OPT_LEN_MAX_WITH_PAD (3 + CALIPSO_OPT_LEN_MAX + 7)
65 /* Maximium size of u32 aligned buffer required to hold calipso
66 * option. Max of 3 initial pad bytes starting from buffer + 3.
67 * i.e. the worst case is when the previous tlv finishes on 4n + 3.
69 #define CALIPSO_MAX_BUFFER (6 + CALIPSO_OPT_LEN_MAX)
71 /* List of available DOI definitions */
72 static DEFINE_SPINLOCK(calipso_doi_list_lock
);
73 static LIST_HEAD(calipso_doi_list
);
75 /* Label mapping cache */
76 int calipso_cache_enabled
= 1;
77 int calipso_cache_bucketsize
= 10;
78 #define CALIPSO_CACHE_BUCKETBITS 7
79 #define CALIPSO_CACHE_BUCKETS BIT(CALIPSO_CACHE_BUCKETBITS)
80 #define CALIPSO_CACHE_REORDERLIMIT 10
81 struct calipso_map_cache_bkt
{
84 struct list_head list
;
87 struct calipso_map_cache_entry
{
92 struct netlbl_lsm_cache
*lsm_data
;
95 struct list_head list
;
98 static struct calipso_map_cache_bkt
*calipso_cache
;
100 /* Label Mapping Cache Functions
104 * calipso_cache_entry_free - Frees a cache entry
105 * @entry: the entry to free
108 * This function frees the memory associated with a cache entry including the
109 * LSM cache data if there are no longer any users, i.e. reference count == 0.
112 static void calipso_cache_entry_free(struct calipso_map_cache_entry
*entry
)
115 netlbl_secattr_cache_free(entry
->lsm_data
);
121 * calipso_map_cache_hash - Hashing function for the CALIPSO cache
123 * @key_len: the length of the key in bytes
126 * The CALIPSO tag hashing function. Returns a 32-bit hash value.
129 static u32
calipso_map_cache_hash(const unsigned char *key
, u32 key_len
)
131 return jhash(key
, key_len
, 0);
135 * calipso_cache_init - Initialize the CALIPSO cache
138 * Initializes the CALIPSO label mapping cache, this function should be called
139 * before any of the other functions defined in this file. Returns zero on
140 * success, negative values on error.
143 static int __init
calipso_cache_init(void)
147 calipso_cache
= kcalloc(CALIPSO_CACHE_BUCKETS
,
148 sizeof(struct calipso_map_cache_bkt
),
153 for (iter
= 0; iter
< CALIPSO_CACHE_BUCKETS
; iter
++) {
154 spin_lock_init(&calipso_cache
[iter
].lock
);
155 calipso_cache
[iter
].size
= 0;
156 INIT_LIST_HEAD(&calipso_cache
[iter
].list
);
163 * calipso_cache_invalidate - Invalidates the current CALIPSO cache
166 * Invalidates and frees any entries in the CALIPSO cache. Returns zero on
167 * success and negative values on failure.
170 static void calipso_cache_invalidate(void)
172 struct calipso_map_cache_entry
*entry
, *tmp_entry
;
175 for (iter
= 0; iter
< CALIPSO_CACHE_BUCKETS
; iter
++) {
176 spin_lock_bh(&calipso_cache
[iter
].lock
);
177 list_for_each_entry_safe(entry
,
179 &calipso_cache
[iter
].list
, list
) {
180 list_del(&entry
->list
);
181 calipso_cache_entry_free(entry
);
183 calipso_cache
[iter
].size
= 0;
184 spin_unlock_bh(&calipso_cache
[iter
].lock
);
189 * calipso_cache_check - Check the CALIPSO cache for a label mapping
190 * @key: the buffer to check
191 * @key_len: buffer length in bytes
192 * @secattr: the security attribute struct to use
195 * This function checks the cache to see if a label mapping already exists for
196 * the given key. If there is a match then the cache is adjusted and the
197 * @secattr struct is populated with the correct LSM security attributes. The
198 * cache is adjusted in the following manner if the entry is not already the
199 * first in the cache bucket:
201 * 1. The cache entry's activity counter is incremented
202 * 2. The previous (higher ranking) entry's activity counter is decremented
203 * 3. If the difference between the two activity counters is geater than
204 * CALIPSO_CACHE_REORDERLIMIT the two entries are swapped
206 * Returns zero on success, -ENOENT for a cache miss, and other negative values
210 static int calipso_cache_check(const unsigned char *key
,
212 struct netlbl_lsm_secattr
*secattr
)
215 struct calipso_map_cache_entry
*entry
;
216 struct calipso_map_cache_entry
*prev_entry
= NULL
;
219 if (!calipso_cache_enabled
)
222 hash
= calipso_map_cache_hash(key
, key_len
);
223 bkt
= hash
& (CALIPSO_CACHE_BUCKETS
- 1);
224 spin_lock_bh(&calipso_cache
[bkt
].lock
);
225 list_for_each_entry(entry
, &calipso_cache
[bkt
].list
, list
) {
226 if (entry
->hash
== hash
&&
227 entry
->key_len
== key_len
&&
228 memcmp(entry
->key
, key
, key_len
) == 0) {
229 entry
->activity
+= 1;
230 refcount_inc(&entry
->lsm_data
->refcount
);
231 secattr
->cache
= entry
->lsm_data
;
232 secattr
->flags
|= NETLBL_SECATTR_CACHE
;
233 secattr
->type
= NETLBL_NLTYPE_CALIPSO
;
235 spin_unlock_bh(&calipso_cache
[bkt
].lock
);
239 if (prev_entry
->activity
> 0)
240 prev_entry
->activity
-= 1;
241 if (entry
->activity
> prev_entry
->activity
&&
242 entry
->activity
- prev_entry
->activity
>
243 CALIPSO_CACHE_REORDERLIMIT
) {
244 __list_del(entry
->list
.prev
, entry
->list
.next
);
245 __list_add(&entry
->list
,
246 prev_entry
->list
.prev
,
250 spin_unlock_bh(&calipso_cache
[bkt
].lock
);
255 spin_unlock_bh(&calipso_cache
[bkt
].lock
);
261 * calipso_cache_add - Add an entry to the CALIPSO cache
262 * @calipso_ptr: the CALIPSO option
263 * @secattr: the packet's security attributes
266 * Add a new entry into the CALIPSO label mapping cache. Add the new entry to
267 * head of the cache bucket's list, if the cache bucket is out of room remove
268 * the last entry in the list first. It is important to note that there is
269 * currently no checking for duplicate keys. Returns zero on success,
270 * negative values on failure. The key stored starts at calipso_ptr + 2,
271 * i.e. the type and length bytes are not stored, this corresponds to
272 * calipso_ptr[1] bytes of data.
275 static int calipso_cache_add(const unsigned char *calipso_ptr
,
276 const struct netlbl_lsm_secattr
*secattr
)
278 int ret_val
= -EPERM
;
280 struct calipso_map_cache_entry
*entry
= NULL
;
281 struct calipso_map_cache_entry
*old_entry
= NULL
;
284 if (!calipso_cache_enabled
|| calipso_cache_bucketsize
<= 0)
287 calipso_ptr_len
= calipso_ptr
[1];
289 entry
= kzalloc(sizeof(*entry
), GFP_ATOMIC
);
292 entry
->key
= kmemdup(calipso_ptr
+ 2, calipso_ptr_len
, GFP_ATOMIC
);
295 goto cache_add_failure
;
297 entry
->key_len
= calipso_ptr_len
;
298 entry
->hash
= calipso_map_cache_hash(calipso_ptr
, calipso_ptr_len
);
299 refcount_inc(&secattr
->cache
->refcount
);
300 entry
->lsm_data
= secattr
->cache
;
302 bkt
= entry
->hash
& (CALIPSO_CACHE_BUCKETS
- 1);
303 spin_lock_bh(&calipso_cache
[bkt
].lock
);
304 if (calipso_cache
[bkt
].size
< calipso_cache_bucketsize
) {
305 list_add(&entry
->list
, &calipso_cache
[bkt
].list
);
306 calipso_cache
[bkt
].size
+= 1;
308 old_entry
= list_entry(calipso_cache
[bkt
].list
.prev
,
309 struct calipso_map_cache_entry
, list
);
310 list_del(&old_entry
->list
);
311 list_add(&entry
->list
, &calipso_cache
[bkt
].list
);
312 calipso_cache_entry_free(old_entry
);
314 spin_unlock_bh(&calipso_cache
[bkt
].lock
);
320 calipso_cache_entry_free(entry
);
324 /* DOI List Functions
328 * calipso_doi_search - Searches for a DOI definition
329 * @doi: the DOI to search for
332 * Search the DOI definition list for a DOI definition with a DOI value that
333 * matches @doi. The caller is responsible for calling rcu_read_[un]lock().
334 * Returns a pointer to the DOI definition on success and NULL on failure.
336 static struct calipso_doi
*calipso_doi_search(u32 doi
)
338 struct calipso_doi
*iter
;
340 list_for_each_entry_rcu(iter
, &calipso_doi_list
, list
)
341 if (iter
->doi
== doi
&& refcount_read(&iter
->refcount
))
347 * calipso_doi_add - Add a new DOI to the CALIPSO protocol engine
348 * @doi_def: the DOI structure
349 * @audit_info: NetLabel audit information
352 * The caller defines a new DOI for use by the CALIPSO engine and calls this
353 * function to add it to the list of acceptable domains. The caller must
354 * ensure that the mapping table specified in @doi_def->map meets all of the
355 * requirements of the mapping type (see calipso.h for details). Returns
356 * zero on success and non-zero on failure.
359 static int calipso_doi_add(struct calipso_doi
*doi_def
,
360 struct netlbl_audit
*audit_info
)
362 int ret_val
= -EINVAL
;
365 struct audit_buffer
*audit_buf
;
368 doi_type
= doi_def
->type
;
370 if (doi_def
->doi
== CALIPSO_DOI_UNKNOWN
)
373 refcount_set(&doi_def
->refcount
, 1);
375 spin_lock(&calipso_doi_list_lock
);
376 if (calipso_doi_search(doi_def
->doi
)) {
377 spin_unlock(&calipso_doi_list_lock
);
381 list_add_tail_rcu(&doi_def
->list
, &calipso_doi_list
);
382 spin_unlock(&calipso_doi_list_lock
);
386 audit_buf
= netlbl_audit_start(AUDIT_MAC_CALIPSO_ADD
, audit_info
);
388 const char *type_str
;
391 case CALIPSO_MAP_PASS
:
395 type_str
= "(unknown)";
397 audit_log_format(audit_buf
,
398 " calipso_doi=%u calipso_type=%s res=%u",
399 doi
, type_str
, ret_val
== 0 ? 1 : 0);
400 audit_log_end(audit_buf
);
407 * calipso_doi_free - Frees a DOI definition
408 * @doi_def: the DOI definition
411 * This function frees all of the memory associated with a DOI definition.
414 static void calipso_doi_free(struct calipso_doi
*doi_def
)
420 * calipso_doi_free_rcu - Frees a DOI definition via the RCU pointer
421 * @entry: the entry's RCU field
424 * This function is designed to be used as a callback to the call_rcu()
425 * function so that the memory allocated to the DOI definition can be released
429 static void calipso_doi_free_rcu(struct rcu_head
*entry
)
431 struct calipso_doi
*doi_def
;
433 doi_def
= container_of(entry
, struct calipso_doi
, rcu
);
434 calipso_doi_free(doi_def
);
438 * calipso_doi_remove - Remove an existing DOI from the CALIPSO protocol engine
439 * @doi: the DOI value
440 * @audit_secid: the LSM secid to use in the audit message
443 * Removes a DOI definition from the CALIPSO engine. The NetLabel routines will
444 * be called to release their own LSM domain mappings as well as our own
445 * domain list. Returns zero on success and negative values on failure.
448 static int calipso_doi_remove(u32 doi
, struct netlbl_audit
*audit_info
)
451 struct calipso_doi
*doi_def
;
452 struct audit_buffer
*audit_buf
;
454 spin_lock(&calipso_doi_list_lock
);
455 doi_def
= calipso_doi_search(doi
);
457 spin_unlock(&calipso_doi_list_lock
);
459 goto doi_remove_return
;
461 if (!refcount_dec_and_test(&doi_def
->refcount
)) {
462 spin_unlock(&calipso_doi_list_lock
);
464 goto doi_remove_return
;
466 list_del_rcu(&doi_def
->list
);
467 spin_unlock(&calipso_doi_list_lock
);
469 call_rcu(&doi_def
->rcu
, calipso_doi_free_rcu
);
473 audit_buf
= netlbl_audit_start(AUDIT_MAC_CALIPSO_DEL
, audit_info
);
475 audit_log_format(audit_buf
,
476 " calipso_doi=%u res=%u",
477 doi
, ret_val
== 0 ? 1 : 0);
478 audit_log_end(audit_buf
);
485 * calipso_doi_getdef - Returns a reference to a valid DOI definition
486 * @doi: the DOI value
489 * Searches for a valid DOI definition and if one is found it is returned to
490 * the caller. Otherwise NULL is returned. The caller must ensure that
491 * calipso_doi_putdef() is called when the caller is done.
494 static struct calipso_doi
*calipso_doi_getdef(u32 doi
)
496 struct calipso_doi
*doi_def
;
499 doi_def
= calipso_doi_search(doi
);
501 goto doi_getdef_return
;
502 if (!refcount_inc_not_zero(&doi_def
->refcount
))
511 * calipso_doi_putdef - Releases a reference for the given DOI definition
512 * @doi_def: the DOI definition
515 * Releases a DOI definition reference obtained from calipso_doi_getdef().
518 static void calipso_doi_putdef(struct calipso_doi
*doi_def
)
523 if (!refcount_dec_and_test(&doi_def
->refcount
))
525 spin_lock(&calipso_doi_list_lock
);
526 list_del_rcu(&doi_def
->list
);
527 spin_unlock(&calipso_doi_list_lock
);
529 call_rcu(&doi_def
->rcu
, calipso_doi_free_rcu
);
533 * calipso_doi_walk - Iterate through the DOI definitions
534 * @skip_cnt: skip past this number of DOI definitions, updated
535 * @callback: callback for each DOI definition
536 * @cb_arg: argument for the callback function
539 * Iterate over the DOI definition list, skipping the first @skip_cnt entries.
540 * For each entry call @callback, if @callback returns a negative value stop
541 * 'walking' through the list and return. Updates the value in @skip_cnt upon
542 * return. Returns zero on success, negative values on failure.
545 static int calipso_doi_walk(u32
*skip_cnt
,
546 int (*callback
)(struct calipso_doi
*doi_def
,
550 int ret_val
= -ENOENT
;
552 struct calipso_doi
*iter_doi
;
555 list_for_each_entry_rcu(iter_doi
, &calipso_doi_list
, list
)
556 if (refcount_read(&iter_doi
->refcount
) > 0) {
557 if (doi_cnt
++ < *skip_cnt
)
559 ret_val
= callback(iter_doi
, cb_arg
);
562 goto doi_walk_return
;
573 * calipso_validate - Validate a CALIPSO option
575 * @option: the start of the option
578 * This routine is called to validate a CALIPSO option.
579 * If the option is valid then %true is returned, otherwise
580 * %false is returned.
582 * The caller should have already checked that the length of the
583 * option (including the TLV header) is >= 10 and that the catmap
584 * length is consistent with the option length.
586 * We leave checks on the level and categories to the socket layer.
588 bool calipso_validate(const struct sk_buff
*skb
, const unsigned char *option
)
590 struct calipso_doi
*doi_def
;
592 u16 crc
, len
= option
[1] + 2;
593 static const u8 zero
[2];
595 /* The original CRC runs over the option including the TLV header
596 * with the CRC-16 field (at offset 8) zeroed out. */
597 crc
= crc_ccitt(0xffff, option
, 8);
598 crc
= crc_ccitt(crc
, zero
, sizeof(zero
));
600 crc
= crc_ccitt(crc
, option
+ 10, len
- 10);
602 if (option
[8] != (crc
& 0xff) || option
[9] != ((crc
>> 8) & 0xff))
606 doi_def
= calipso_doi_search(get_unaligned_be32(option
+ 2));
614 * calipso_map_cat_hton - Perform a category mapping from host to network
615 * @doi_def: the DOI definition
616 * @secattr: the security attributes
617 * @net_cat: the zero'd out category bitmap in network/CALIPSO format
618 * @net_cat_len: the length of the CALIPSO bitmap in bytes
621 * Perform a label mapping to translate a local MLS category bitmap to the
622 * correct CALIPSO bitmap using the given DOI definition. Returns the minimum
623 * size in bytes of the network bitmap on success, negative values otherwise.
626 static int calipso_map_cat_hton(const struct calipso_doi
*doi_def
,
627 const struct netlbl_lsm_secattr
*secattr
,
628 unsigned char *net_cat
,
632 u32 net_spot_max
= 0;
633 u32 net_clen_bits
= net_cat_len
* 8;
636 spot
= netlbl_catmap_walk(secattr
->attr
.mls
.cat
,
640 if (spot
>= net_clen_bits
)
642 netlbl_bitmap_setbit(net_cat
, spot
, 1);
644 if (spot
> net_spot_max
)
648 return (net_spot_max
/ 32 + 1) * 4;
652 * calipso_map_cat_ntoh - Perform a category mapping from network to host
653 * @doi_def: the DOI definition
654 * @net_cat: the category bitmap in network/CALIPSO format
655 * @net_cat_len: the length of the CALIPSO bitmap in bytes
656 * @secattr: the security attributes
659 * Perform a label mapping to translate a CALIPSO bitmap to the correct local
660 * MLS category bitmap using the given DOI definition. Returns zero on
661 * success, negative values on failure.
664 static int calipso_map_cat_ntoh(const struct calipso_doi
*doi_def
,
665 const unsigned char *net_cat
,
667 struct netlbl_lsm_secattr
*secattr
)
671 u32 net_clen_bits
= net_cat_len
* 8;
674 spot
= netlbl_bitmap_walk(net_cat
,
684 ret_val
= netlbl_catmap_setbit(&secattr
->attr
.mls
.cat
,
695 * calipso_pad_write - Writes pad bytes in TLV format
697 * @offset: offset from start of buffer to write padding
698 * @count: number of pad bytes to write
701 * Write @count bytes of TLV padding into @buffer starting at offset @offset.
702 * @count should be less than 8 - see RFC 4942.
705 static int calipso_pad_write(unsigned char *buf
, unsigned int offset
,
708 if (WARN_ON_ONCE(count
>= 8))
715 buf
[offset
] = IPV6_TLV_PAD1
;
718 buf
[offset
] = IPV6_TLV_PADN
;
719 buf
[offset
+ 1] = count
- 2;
721 memset(buf
+ offset
+ 2, 0, count
- 2);
728 * calipso_genopt - Generate a CALIPSO option
729 * @buf: the option buffer
730 * @start: offset from which to write
731 * @buf_len: the size of opt_buf
732 * @doi_def: the CALIPSO DOI to use
733 * @secattr: the security attributes
736 * Generate a CALIPSO option using the DOI definition and security attributes
737 * passed to the function. This also generates upto three bytes of leading
738 * padding that ensures that the option is 4n + 2 aligned. It returns the
739 * number of bytes written (including any initial padding).
741 static int calipso_genopt(unsigned char *buf
, u32 start
, u32 buf_len
,
742 const struct calipso_doi
*doi_def
,
743 const struct netlbl_lsm_secattr
*secattr
)
748 static const unsigned char padding
[4] = {2, 1, 0, 3};
749 unsigned char *calipso
;
751 /* CALIPSO has 4n + 2 alignment */
752 pad
= padding
[start
& 3];
753 if (buf_len
<= start
+ pad
+ CALIPSO_HDR_LEN
)
756 if ((secattr
->flags
& NETLBL_SECATTR_MLS_LVL
) == 0)
759 len
= CALIPSO_HDR_LEN
;
761 if (secattr
->flags
& NETLBL_SECATTR_MLS_CAT
) {
762 ret_val
= calipso_map_cat_hton(doi_def
,
764 buf
+ start
+ pad
+ len
,
765 buf_len
- start
- pad
- len
);
771 calipso_pad_write(buf
, start
, pad
);
772 calipso
= buf
+ start
+ pad
;
774 calipso
[0] = IPV6_TLV_CALIPSO
;
775 calipso
[1] = len
- 2;
776 *(__be32
*)(calipso
+ 2) = htonl(doi_def
->doi
);
777 calipso
[6] = (len
- CALIPSO_HDR_LEN
) / 4;
778 calipso
[7] = secattr
->attr
.mls
.lvl
,
779 crc
= ~crc_ccitt(0xffff, calipso
, len
);
780 calipso
[8] = crc
& 0xff;
781 calipso
[9] = (crc
>> 8) & 0xff;
785 /* Hop-by-hop hdr helper functions
789 * calipso_opt_update - Replaces socket's hop options with a new set
791 * @hop: new hop options
794 * Replaces @sk's hop options with @hop. @hop may be NULL to leave
795 * the socket with no hop options.
798 static int calipso_opt_update(struct sock
*sk
, struct ipv6_opt_hdr
*hop
)
800 struct ipv6_txoptions
*old
= txopt_get(inet6_sk(sk
)), *txopts
;
802 txopts
= ipv6_renew_options(sk
, old
, IPV6_HOPOPTS
, hop
);
805 return PTR_ERR(txopts
);
807 txopts
= ipv6_update_options(sk
, txopts
);
809 atomic_sub(txopts
->tot_len
, &sk
->sk_omem_alloc
);
817 * calipso_tlv_len - Returns the length of the TLV
818 * @opt: the option header
819 * @offset: offset of the TLV within the header
822 * Returns the length of the TLV option at offset @offset within
823 * the option header @opt. Checks that the entire TLV fits inside
824 * the option header, returns a negative value if this is not the case.
826 static int calipso_tlv_len(struct ipv6_opt_hdr
*opt
, unsigned int offset
)
828 unsigned char *tlv
= (unsigned char *)opt
;
829 unsigned int opt_len
= ipv6_optlen(opt
), tlv_len
;
831 if (offset
< sizeof(*opt
) || offset
>= opt_len
)
833 if (tlv
[offset
] == IPV6_TLV_PAD1
)
835 if (offset
+ 1 >= opt_len
)
837 tlv_len
= tlv
[offset
+ 1] + 2;
838 if (offset
+ tlv_len
> opt_len
)
844 * calipso_opt_find - Finds the CALIPSO option in an IPv6 hop options header
845 * @hop: the hop options header
846 * @start: on return holds the offset of any leading padding
847 * @end: on return holds the offset of the first non-pad TLV after CALIPSO
850 * Finds the space occupied by a CALIPSO option (including any leading and
853 * If a CALIPSO option exists set @start and @end to the
854 * offsets within @hop of the start of padding before the first
855 * CALIPSO option and the end of padding after the first CALIPSO
856 * option. In this case the function returns 0.
858 * In the absence of a CALIPSO option, @start and @end will be
859 * set to the start and end of any trailing padding in the header.
860 * This is useful when appending a new option, as the caller may want
861 * to overwrite some of this padding. In this case the function will
864 static int calipso_opt_find(struct ipv6_opt_hdr
*hop
, unsigned int *start
,
867 int ret_val
= -ENOENT
, tlv_len
;
868 unsigned int opt_len
, offset
, offset_s
= 0, offset_e
= 0;
869 unsigned char *opt
= (unsigned char *)hop
;
871 opt_len
= ipv6_optlen(hop
);
872 offset
= sizeof(*hop
);
874 while (offset
< opt_len
) {
875 tlv_len
= calipso_tlv_len(hop
, offset
);
879 switch (opt
[offset
]) {
885 case IPV6_TLV_CALIPSO
:
900 *start
= offset_s
+ calipso_tlv_len(hop
, offset_s
);
902 *start
= sizeof(*hop
);
904 *end
= offset_e
+ calipso_tlv_len(hop
, offset_e
);
912 * calipso_opt_insert - Inserts a CALIPSO option into an IPv6 hop opt hdr
913 * @hop: the original hop options header
914 * @doi_def: the CALIPSO DOI to use
915 * @secattr: the specific security attributes of the socket
918 * Creates a new hop options header based on @hop with a
919 * CALIPSO option added to it. If @hop already contains a CALIPSO
920 * option this is overwritten, otherwise the new option is appended
921 * after any existing options. If @hop is NULL then the new header
922 * will contain just the CALIPSO option and any needed padding.
925 static struct ipv6_opt_hdr
*
926 calipso_opt_insert(struct ipv6_opt_hdr
*hop
,
927 const struct calipso_doi
*doi_def
,
928 const struct netlbl_lsm_secattr
*secattr
)
930 unsigned int start
, end
, buf_len
, pad
, hop_len
;
931 struct ipv6_opt_hdr
*new;
935 hop_len
= ipv6_optlen(hop
);
936 ret_val
= calipso_opt_find(hop
, &start
, &end
);
937 if (ret_val
&& ret_val
!= -ENOENT
)
938 return ERR_PTR(ret_val
);
941 start
= sizeof(*hop
);
945 buf_len
= hop_len
+ start
- end
+ CALIPSO_OPT_LEN_MAX_WITH_PAD
;
946 new = kzalloc(buf_len
, GFP_ATOMIC
);
948 return ERR_PTR(-ENOMEM
);
950 if (start
> sizeof(*hop
))
951 memcpy(new, hop
, start
);
952 ret_val
= calipso_genopt((unsigned char *)new, start
, buf_len
, doi_def
,
956 return ERR_PTR(ret_val
);
959 buf_len
= start
+ ret_val
;
960 /* At this point buf_len aligns to 4n, so (buf_len & 4) pads to 8n */
961 pad
= ((buf_len
& 4) + (end
& 7)) & 7;
962 calipso_pad_write((unsigned char *)new, buf_len
, pad
);
965 if (end
!= hop_len
) {
966 memcpy((char *)new + buf_len
, (char *)hop
+ end
, hop_len
- end
);
967 buf_len
+= hop_len
- end
;
970 new->hdrlen
= buf_len
/ 8 - 1;
976 * calipso_opt_del - Removes the CALIPSO option from an option header
977 * @hop: the original header
978 * @new: the new header
981 * Creates a new header based on @hop without any CALIPSO option. If @hop
982 * doesn't contain a CALIPSO option it returns -ENOENT. If @hop contains
983 * no other non-padding options, it returns zero with @new set to NULL.
984 * Otherwise it returns zero, creates a new header without the CALIPSO
985 * option (and removing as much padding as possible) and returns with
986 * @new set to that header.
989 static int calipso_opt_del(struct ipv6_opt_hdr
*hop
,
990 struct ipv6_opt_hdr
**new)
993 unsigned int start
, end
, delta
, pad
, hop_len
;
995 ret_val
= calipso_opt_find(hop
, &start
, &end
);
999 hop_len
= ipv6_optlen(hop
);
1000 if (start
== sizeof(*hop
) && end
== hop_len
) {
1001 /* There's no other option in the header so return NULL */
1006 delta
= (end
- start
) & ~7;
1007 *new = kzalloc(hop_len
- delta
, GFP_ATOMIC
);
1011 memcpy(*new, hop
, start
);
1012 (*new)->hdrlen
-= delta
/ 8;
1013 pad
= (end
- start
) & 7;
1014 calipso_pad_write((unsigned char *)*new, start
, pad
);
1016 memcpy((char *)*new + start
+ pad
, (char *)hop
+ end
,
1023 * calipso_opt_getattr - Get the security attributes from a memory block
1024 * @calipso: the CALIPSO option
1025 * @secattr: the security attributes
1028 * Inspect @calipso and return the security attributes in @secattr.
1029 * Returns zero on success and negative values on failure.
1032 static int calipso_opt_getattr(const unsigned char *calipso
,
1033 struct netlbl_lsm_secattr
*secattr
)
1035 int ret_val
= -ENOMSG
;
1036 u32 doi
, len
= calipso
[1], cat_len
= calipso
[6] * 4;
1037 struct calipso_doi
*doi_def
;
1039 if (cat_len
+ 8 > len
)
1042 if (calipso_cache_check(calipso
+ 2, calipso
[1], secattr
) == 0)
1045 doi
= get_unaligned_be32(calipso
+ 2);
1047 doi_def
= calipso_doi_search(doi
);
1049 goto getattr_return
;
1051 secattr
->attr
.mls
.lvl
= calipso
[7];
1052 secattr
->flags
|= NETLBL_SECATTR_MLS_LVL
;
1055 ret_val
= calipso_map_cat_ntoh(doi_def
,
1060 netlbl_catmap_free(secattr
->attr
.mls
.cat
);
1061 goto getattr_return
;
1064 secattr
->flags
|= NETLBL_SECATTR_MLS_CAT
;
1067 secattr
->type
= NETLBL_NLTYPE_CALIPSO
;
1078 * calipso_sock_getattr - Get the security attributes from a sock
1080 * @secattr: the security attributes
1083 * Query @sk to see if there is a CALIPSO option attached to the sock and if
1084 * there is return the CALIPSO security attributes in @secattr. This function
1085 * requires that @sk be locked, or privately held, but it does not do any
1086 * locking itself. Returns zero on success and negative values on failure.
1089 static int calipso_sock_getattr(struct sock
*sk
,
1090 struct netlbl_lsm_secattr
*secattr
)
1092 struct ipv6_opt_hdr
*hop
;
1093 int opt_len
, len
, ret_val
= -ENOMSG
, offset
;
1095 struct ipv6_txoptions
*txopts
= txopt_get(inet6_sk(sk
));
1097 if (!txopts
|| !txopts
->hopopt
)
1100 hop
= txopts
->hopopt
;
1101 opt
= (unsigned char *)hop
;
1102 opt_len
= ipv6_optlen(hop
);
1103 offset
= sizeof(*hop
);
1104 while (offset
< opt_len
) {
1105 len
= calipso_tlv_len(hop
, offset
);
1110 switch (opt
[offset
]) {
1111 case IPV6_TLV_CALIPSO
:
1112 if (len
< CALIPSO_HDR_LEN
)
1115 ret_val
= calipso_opt_getattr(&opt
[offset
],
1129 * calipso_sock_setattr - Add a CALIPSO option to a socket
1131 * @doi_def: the CALIPSO DOI to use
1132 * @secattr: the specific security attributes of the socket
1135 * Set the CALIPSO option on the given socket using the DOI definition and
1136 * security attributes passed to the function. This function requires
1137 * exclusive access to @sk, which means it either needs to be in the
1138 * process of being created or locked. Returns zero on success and negative
1139 * values on failure.
1142 static int calipso_sock_setattr(struct sock
*sk
,
1143 const struct calipso_doi
*doi_def
,
1144 const struct netlbl_lsm_secattr
*secattr
)
1147 struct ipv6_opt_hdr
*old
, *new;
1148 struct ipv6_txoptions
*txopts
= txopt_get(inet6_sk(sk
));
1152 old
= txopts
->hopopt
;
1154 new = calipso_opt_insert(old
, doi_def
, secattr
);
1157 return PTR_ERR(new);
1159 ret_val
= calipso_opt_update(sk
, new);
1166 * calipso_sock_delattr - Delete the CALIPSO option from a socket
1170 * Removes the CALIPSO option from a socket, if present.
1173 static void calipso_sock_delattr(struct sock
*sk
)
1175 struct ipv6_opt_hdr
*new_hop
;
1176 struct ipv6_txoptions
*txopts
= txopt_get(inet6_sk(sk
));
1178 if (!txopts
|| !txopts
->hopopt
)
1181 if (calipso_opt_del(txopts
->hopopt
, &new_hop
))
1184 calipso_opt_update(sk
, new_hop
);
1191 /* request sock functions.
1195 * calipso_req_setattr - Add a CALIPSO option to a connection request socket
1196 * @req: the connection request socket
1197 * @doi_def: the CALIPSO DOI to use
1198 * @secattr: the specific security attributes of the socket
1201 * Set the CALIPSO option on the given socket using the DOI definition and
1202 * security attributes passed to the function. Returns zero on success and
1203 * negative values on failure.
1206 static int calipso_req_setattr(struct request_sock
*req
,
1207 const struct calipso_doi
*doi_def
,
1208 const struct netlbl_lsm_secattr
*secattr
)
1210 struct ipv6_txoptions
*txopts
;
1211 struct inet_request_sock
*req_inet
= inet_rsk(req
);
1212 struct ipv6_opt_hdr
*old
, *new;
1213 struct sock
*sk
= sk_to_full_sk(req_to_sk(req
));
1215 if (req_inet
->ipv6_opt
&& req_inet
->ipv6_opt
->hopopt
)
1216 old
= req_inet
->ipv6_opt
->hopopt
;
1220 new = calipso_opt_insert(old
, doi_def
, secattr
);
1222 return PTR_ERR(new);
1224 txopts
= ipv6_renew_options(sk
, req_inet
->ipv6_opt
, IPV6_HOPOPTS
, new);
1229 return PTR_ERR(txopts
);
1231 txopts
= xchg(&req_inet
->ipv6_opt
, txopts
);
1233 atomic_sub(txopts
->tot_len
, &sk
->sk_omem_alloc
);
1241 * calipso_req_delattr - Delete the CALIPSO option from a request socket
1242 * @reg: the request socket
1245 * Removes the CALIPSO option from a request socket, if present.
1248 static void calipso_req_delattr(struct request_sock
*req
)
1250 struct inet_request_sock
*req_inet
= inet_rsk(req
);
1251 struct ipv6_opt_hdr
*new;
1252 struct ipv6_txoptions
*txopts
;
1253 struct sock
*sk
= sk_to_full_sk(req_to_sk(req
));
1255 if (!req_inet
->ipv6_opt
|| !req_inet
->ipv6_opt
->hopopt
)
1258 if (calipso_opt_del(req_inet
->ipv6_opt
->hopopt
, &new))
1259 return; /* Nothing to do */
1261 txopts
= ipv6_renew_options(sk
, req_inet
->ipv6_opt
, IPV6_HOPOPTS
, new);
1263 if (!IS_ERR(txopts
)) {
1264 txopts
= xchg(&req_inet
->ipv6_opt
, txopts
);
1266 atomic_sub(txopts
->tot_len
, &sk
->sk_omem_alloc
);
1273 /* skbuff functions.
1277 * calipso_skbuff_optptr - Find the CALIPSO option in the packet
1281 * Parse the packet's IP header looking for a CALIPSO option. Returns a pointer
1282 * to the start of the CALIPSO option on success, NULL if one if not found.
1285 static unsigned char *calipso_skbuff_optptr(const struct sk_buff
*skb
)
1287 const struct ipv6hdr
*ip6_hdr
= ipv6_hdr(skb
);
1290 if (ip6_hdr
->nexthdr
!= NEXTHDR_HOP
)
1293 offset
= ipv6_find_tlv(skb
, sizeof(*ip6_hdr
), IPV6_TLV_CALIPSO
);
1295 return (unsigned char *)ip6_hdr
+ offset
;
1301 * calipso_skbuff_setattr - Set the CALIPSO option on a packet
1303 * @doi_def: the CALIPSO DOI to use
1304 * @secattr: the security attributes
1307 * Set the CALIPSO option on the given packet based on the security attributes.
1308 * Returns a pointer to the IP header on success and NULL on failure.
1311 static int calipso_skbuff_setattr(struct sk_buff
*skb
,
1312 const struct calipso_doi
*doi_def
,
1313 const struct netlbl_lsm_secattr
*secattr
)
1316 struct ipv6hdr
*ip6_hdr
;
1317 struct ipv6_opt_hdr
*hop
;
1318 unsigned char buf
[CALIPSO_MAX_BUFFER
];
1319 int len_delta
, new_end
, pad
, payload
;
1320 unsigned int start
, end
;
1322 ip6_hdr
= ipv6_hdr(skb
);
1323 if (ip6_hdr
->nexthdr
== NEXTHDR_HOP
) {
1324 hop
= (struct ipv6_opt_hdr
*)(ip6_hdr
+ 1);
1325 ret_val
= calipso_opt_find(hop
, &start
, &end
);
1326 if (ret_val
&& ret_val
!= -ENOENT
)
1333 memset(buf
, 0, sizeof(buf
));
1334 ret_val
= calipso_genopt(buf
, start
& 3, sizeof(buf
), doi_def
, secattr
);
1338 new_end
= start
+ ret_val
;
1339 /* At this point new_end aligns to 4n, so (new_end & 4) pads to 8n */
1340 pad
= ((new_end
& 4) + (end
& 7)) & 7;
1341 len_delta
= new_end
- (int)end
+ pad
;
1342 ret_val
= skb_cow(skb
, skb_headroom(skb
) + len_delta
);
1346 ip6_hdr
= ipv6_hdr(skb
); /* Reset as skb_cow() may have moved it */
1350 skb_push(skb
, len_delta
);
1352 skb_pull(skb
, -len_delta
);
1353 memmove((char *)ip6_hdr
- len_delta
, ip6_hdr
,
1354 sizeof(*ip6_hdr
) + start
);
1355 skb_reset_network_header(skb
);
1356 ip6_hdr
= ipv6_hdr(skb
);
1357 payload
= ntohs(ip6_hdr
->payload_len
);
1358 ip6_hdr
->payload_len
= htons(payload
+ len_delta
);
1361 hop
= (struct ipv6_opt_hdr
*)(ip6_hdr
+ 1);
1363 struct ipv6_opt_hdr
*new_hop
= (struct ipv6_opt_hdr
*)buf
;
1365 new_hop
->nexthdr
= ip6_hdr
->nexthdr
;
1366 new_hop
->hdrlen
= len_delta
/ 8 - 1;
1367 ip6_hdr
->nexthdr
= NEXTHDR_HOP
;
1369 hop
->hdrlen
+= len_delta
/ 8;
1371 memcpy((char *)hop
+ start
, buf
+ (start
& 3), new_end
- start
);
1372 calipso_pad_write((unsigned char *)hop
, new_end
, pad
);
1378 * calipso_skbuff_delattr - Delete any CALIPSO options from a packet
1382 * Removes any and all CALIPSO options from the given packet. Returns zero on
1383 * success, negative values on failure.
1386 static int calipso_skbuff_delattr(struct sk_buff
*skb
)
1389 struct ipv6hdr
*ip6_hdr
;
1390 struct ipv6_opt_hdr
*old_hop
;
1391 u32 old_hop_len
, start
= 0, end
= 0, delta
, size
, pad
;
1393 if (!calipso_skbuff_optptr(skb
))
1396 /* since we are changing the packet we should make a copy */
1397 ret_val
= skb_cow(skb
, skb_headroom(skb
));
1401 ip6_hdr
= ipv6_hdr(skb
);
1402 old_hop
= (struct ipv6_opt_hdr
*)(ip6_hdr
+ 1);
1403 old_hop_len
= ipv6_optlen(old_hop
);
1405 ret_val
= calipso_opt_find(old_hop
, &start
, &end
);
1409 if (start
== sizeof(*old_hop
) && end
== old_hop_len
) {
1410 /* There's no other option in the header so we delete
1411 * the whole thing. */
1412 delta
= old_hop_len
;
1413 size
= sizeof(*ip6_hdr
);
1414 ip6_hdr
->nexthdr
= old_hop
->nexthdr
;
1416 delta
= (end
- start
) & ~7;
1418 old_hop
->hdrlen
-= delta
/ 8;
1419 pad
= (end
- start
) & 7;
1420 size
= sizeof(*ip6_hdr
) + start
+ pad
;
1421 calipso_pad_write((unsigned char *)old_hop
, start
, pad
);
1425 skb_pull(skb
, delta
);
1426 memmove((char *)ip6_hdr
+ delta
, ip6_hdr
, size
);
1427 skb_reset_network_header(skb
);
1433 static const struct netlbl_calipso_ops ops
= {
1434 .doi_add
= calipso_doi_add
,
1435 .doi_free
= calipso_doi_free
,
1436 .doi_remove
= calipso_doi_remove
,
1437 .doi_getdef
= calipso_doi_getdef
,
1438 .doi_putdef
= calipso_doi_putdef
,
1439 .doi_walk
= calipso_doi_walk
,
1440 .sock_getattr
= calipso_sock_getattr
,
1441 .sock_setattr
= calipso_sock_setattr
,
1442 .sock_delattr
= calipso_sock_delattr
,
1443 .req_setattr
= calipso_req_setattr
,
1444 .req_delattr
= calipso_req_delattr
,
1445 .opt_getattr
= calipso_opt_getattr
,
1446 .skbuff_optptr
= calipso_skbuff_optptr
,
1447 .skbuff_setattr
= calipso_skbuff_setattr
,
1448 .skbuff_delattr
= calipso_skbuff_delattr
,
1449 .cache_invalidate
= calipso_cache_invalidate
,
1450 .cache_add
= calipso_cache_add
1454 * calipso_init - Initialize the CALIPSO module
1457 * Initialize the CALIPSO module and prepare it for use. Returns zero on
1458 * success and negative values on failure.
1461 int __init
calipso_init(void)
1465 ret_val
= calipso_cache_init();
1467 netlbl_calipso_ops_register(&ops
);
1471 void calipso_exit(void)
1473 netlbl_calipso_ops_register(NULL
);
1474 calipso_cache_invalidate();
1475 kfree(calipso_cache
);