blob: 2c0e4572cc9042e76fca92f5c08303d1489f513c [file] [log] [blame]
Paul Moore446fda42006-08-03 16:48:06 -07001/*
2 * CIPSO - Commercial IP Security Option
3 *
4 * This is an implementation of the CIPSO 2.2 protocol as specified in
5 * draft-ietf-cipso-ipsecurity-01.txt with additional tag types as found in
6 * FIPS-188, copies of both documents can be found in the Documentation
7 * directory. While CIPSO never became a full IETF RFC standard many vendors
8 * have chosen to adopt the protocol and over the years it has become a
9 * de-facto standard for labeled networking.
10 *
11 * Author: Paul Moore <paul.moore@hp.com>
12 *
13 */
14
15/*
16 * (c) Copyright Hewlett-Packard Development Company, L.P., 2006
17 *
18 * This program is free software; you can redistribute it and/or modify
19 * it under the terms of the GNU General Public License as published by
20 * the Free Software Foundation; either version 2 of the License, or
21 * (at your option) any later version.
22 *
23 * This program is distributed in the hope that it will be useful,
24 * but WITHOUT ANY WARRANTY; without even the implied warranty of
25 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See
26 * the GNU General Public License for more details.
27 *
28 * You should have received a copy of the GNU General Public License
29 * along with this program; if not, write to the Free Software
30 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
31 *
32 */
33
34#include <linux/init.h>
35#include <linux/types.h>
36#include <linux/rcupdate.h>
37#include <linux/list.h>
38#include <linux/spinlock.h>
39#include <linux/string.h>
40#include <linux/jhash.h>
41#include <net/ip.h>
42#include <net/icmp.h>
43#include <net/tcp.h>
44#include <net/netlabel.h>
45#include <net/cipso_ipv4.h>
paul.moore@hp.comffb733c2006-10-04 11:46:31 -040046#include <asm/atomic.h>
Paul Moore446fda42006-08-03 16:48:06 -070047#include <asm/bug.h>
Paul Moore50e5d352007-06-07 18:38:14 -070048#include <asm/unaligned.h>
Paul Moore446fda42006-08-03 16:48:06 -070049
50struct cipso_v4_domhsh_entry {
51 char *domain;
52 u32 valid;
53 struct list_head list;
54 struct rcu_head rcu;
55};
56
57/* List of available DOI definitions */
58/* XXX - Updates should be minimal so having a single lock for the
59 * cipso_v4_doi_list and the cipso_v4_doi_list->dom_list should be
60 * okay. */
61/* XXX - This currently assumes a minimal number of different DOIs in use,
62 * if in practice there are a lot of different DOIs this list should
63 * probably be turned into a hash table or something similar so we
64 * can do quick lookups. */
Adrian Bunk8ce11e62006-08-07 21:50:48 -070065static DEFINE_SPINLOCK(cipso_v4_doi_list_lock);
Denis Cheng1596c972007-12-07 00:49:47 -080066static LIST_HEAD(cipso_v4_doi_list);
Paul Moore446fda42006-08-03 16:48:06 -070067
68/* Label mapping cache */
69int cipso_v4_cache_enabled = 1;
70int cipso_v4_cache_bucketsize = 10;
71#define CIPSO_V4_CACHE_BUCKETBITS 7
72#define CIPSO_V4_CACHE_BUCKETS (1 << CIPSO_V4_CACHE_BUCKETBITS)
73#define CIPSO_V4_CACHE_REORDERLIMIT 10
74struct cipso_v4_map_cache_bkt {
75 spinlock_t lock;
76 u32 size;
77 struct list_head list;
78};
79struct cipso_v4_map_cache_entry {
80 u32 hash;
81 unsigned char *key;
82 size_t key_len;
83
paul.moore@hp.comffb733c2006-10-04 11:46:31 -040084 struct netlbl_lsm_cache *lsm_data;
Paul Moore446fda42006-08-03 16:48:06 -070085
86 u32 activity;
87 struct list_head list;
88};
89static struct cipso_v4_map_cache_bkt *cipso_v4_cache = NULL;
90
91/* Restricted bitmap (tag #1) flags */
92int cipso_v4_rbm_optfmt = 0;
93int cipso_v4_rbm_strictvalid = 1;
94
95/*
Paul Mooref998e8c2007-02-28 15:14:20 -050096 * Protocol Constants
97 */
98
99/* Maximum size of the CIPSO IP option, derived from the fact that the maximum
100 * IPv4 header size is 60 bytes and the base IPv4 header is 20 bytes long. */
101#define CIPSO_V4_OPT_LEN_MAX 40
102
103/* Length of the base CIPSO option, this includes the option type (1 byte), the
104 * option length (1 byte), and the DOI (4 bytes). */
105#define CIPSO_V4_HDR_LEN 6
106
107/* Base length of the restrictive category bitmap tag (tag #1). */
108#define CIPSO_V4_TAG_RBM_BLEN 4
109
110/* Base length of the enumerated category tag (tag #2). */
111#define CIPSO_V4_TAG_ENUM_BLEN 4
112
113/* Base length of the ranged categories bitmap tag (tag #5). */
114#define CIPSO_V4_TAG_RNG_BLEN 4
115/* The maximum number of category ranges permitted in the ranged category tag
116 * (tag #5). You may note that the IETF draft states that the maximum number
117 * of category ranges is 7, but if the low end of the last category range is
118 * zero then it is possibile to fit 8 category ranges because the zero should
119 * be omitted. */
120#define CIPSO_V4_TAG_RNG_CAT_MAX 8
121
122/*
Paul Moore446fda42006-08-03 16:48:06 -0700123 * Helper Functions
124 */
125
126/**
127 * cipso_v4_bitmap_walk - Walk a bitmap looking for a bit
128 * @bitmap: the bitmap
129 * @bitmap_len: length in bits
130 * @offset: starting offset
131 * @state: if non-zero, look for a set (1) bit else look for a cleared (0) bit
132 *
133 * Description:
134 * Starting at @offset, walk the bitmap from left to right until either the
135 * desired bit is found or we reach the end. Return the bit offset, -1 if
136 * not found, or -2 if error.
137 */
138static int cipso_v4_bitmap_walk(const unsigned char *bitmap,
139 u32 bitmap_len,
140 u32 offset,
141 u8 state)
142{
143 u32 bit_spot;
144 u32 byte_offset;
145 unsigned char bitmask;
146 unsigned char byte;
147
148 /* gcc always rounds to zero when doing integer division */
149 byte_offset = offset / 8;
150 byte = bitmap[byte_offset];
151 bit_spot = offset;
152 bitmask = 0x80 >> (offset % 8);
153
154 while (bit_spot < bitmap_len) {
155 if ((state && (byte & bitmask) == bitmask) ||
156 (state == 0 && (byte & bitmask) == 0))
157 return bit_spot;
158
159 bit_spot++;
160 bitmask >>= 1;
161 if (bitmask == 0) {
162 byte = bitmap[++byte_offset];
163 bitmask = 0x80;
164 }
165 }
166
167 return -1;
168}
169
170/**
171 * cipso_v4_bitmap_setbit - Sets a single bit in a bitmap
172 * @bitmap: the bitmap
173 * @bit: the bit
174 * @state: if non-zero, set the bit (1) else clear the bit (0)
175 *
176 * Description:
177 * Set a single bit in the bitmask. Returns zero on success, negative values
178 * on error.
179 */
180static void cipso_v4_bitmap_setbit(unsigned char *bitmap,
181 u32 bit,
182 u8 state)
183{
184 u32 byte_spot;
185 u8 bitmask;
186
187 /* gcc always rounds to zero when doing integer division */
188 byte_spot = bit / 8;
189 bitmask = 0x80 >> (bit % 8);
190 if (state)
191 bitmap[byte_spot] |= bitmask;
192 else
193 bitmap[byte_spot] &= ~bitmask;
194}
195
196/**
197 * cipso_v4_doi_domhsh_free - Frees a domain list entry
198 * @entry: the entry's RCU field
199 *
200 * Description:
201 * This function is designed to be used as a callback to the call_rcu()
202 * function so that the memory allocated to a domain list entry can be released
203 * safely.
204 *
205 */
206static void cipso_v4_doi_domhsh_free(struct rcu_head *entry)
207{
208 struct cipso_v4_domhsh_entry *ptr;
209
210 ptr = container_of(entry, struct cipso_v4_domhsh_entry, rcu);
211 kfree(ptr->domain);
212 kfree(ptr);
213}
214
215/**
216 * cipso_v4_cache_entry_free - Frees a cache entry
217 * @entry: the entry to free
218 *
219 * Description:
paul.moore@hp.comffb733c2006-10-04 11:46:31 -0400220 * This function frees the memory associated with a cache entry including the
221 * LSM cache data if there are no longer any users, i.e. reference count == 0.
Paul Moore446fda42006-08-03 16:48:06 -0700222 *
223 */
224static void cipso_v4_cache_entry_free(struct cipso_v4_map_cache_entry *entry)
225{
paul.moore@hp.comffb733c2006-10-04 11:46:31 -0400226 if (entry->lsm_data)
227 netlbl_secattr_cache_free(entry->lsm_data);
Paul Moore446fda42006-08-03 16:48:06 -0700228 kfree(entry->key);
229 kfree(entry);
230}
231
232/**
233 * cipso_v4_map_cache_hash - Hashing function for the CIPSO cache
234 * @key: the hash key
235 * @key_len: the length of the key in bytes
236 *
237 * Description:
238 * The CIPSO tag hashing function. Returns a 32-bit hash value.
239 *
240 */
241static u32 cipso_v4_map_cache_hash(const unsigned char *key, u32 key_len)
242{
243 return jhash(key, key_len, 0);
244}
245
246/*
247 * Label Mapping Cache Functions
248 */
249
250/**
251 * cipso_v4_cache_init - Initialize the CIPSO cache
252 *
253 * Description:
254 * Initializes the CIPSO label mapping cache, this function should be called
255 * before any of the other functions defined in this file. Returns zero on
256 * success, negative values on error.
257 *
258 */
259static int cipso_v4_cache_init(void)
260{
261 u32 iter;
262
263 cipso_v4_cache = kcalloc(CIPSO_V4_CACHE_BUCKETS,
264 sizeof(struct cipso_v4_map_cache_bkt),
265 GFP_KERNEL);
266 if (cipso_v4_cache == NULL)
267 return -ENOMEM;
268
269 for (iter = 0; iter < CIPSO_V4_CACHE_BUCKETS; iter++) {
270 spin_lock_init(&cipso_v4_cache[iter].lock);
271 cipso_v4_cache[iter].size = 0;
272 INIT_LIST_HEAD(&cipso_v4_cache[iter].list);
273 }
274
275 return 0;
276}
277
278/**
279 * cipso_v4_cache_invalidate - Invalidates the current CIPSO cache
280 *
281 * Description:
282 * Invalidates and frees any entries in the CIPSO cache. Returns zero on
283 * success and negative values on failure.
284 *
285 */
286void cipso_v4_cache_invalidate(void)
287{
288 struct cipso_v4_map_cache_entry *entry, *tmp_entry;
289 u32 iter;
290
291 for (iter = 0; iter < CIPSO_V4_CACHE_BUCKETS; iter++) {
Paul Moore609c92f2006-09-25 15:52:37 -0700292 spin_lock_bh(&cipso_v4_cache[iter].lock);
Paul Moore446fda42006-08-03 16:48:06 -0700293 list_for_each_entry_safe(entry,
294 tmp_entry,
295 &cipso_v4_cache[iter].list, list) {
296 list_del(&entry->list);
297 cipso_v4_cache_entry_free(entry);
298 }
299 cipso_v4_cache[iter].size = 0;
Paul Moore609c92f2006-09-25 15:52:37 -0700300 spin_unlock_bh(&cipso_v4_cache[iter].lock);
Paul Moore446fda42006-08-03 16:48:06 -0700301 }
302
303 return;
304}
305
306/**
307 * cipso_v4_cache_check - Check the CIPSO cache for a label mapping
308 * @key: the buffer to check
309 * @key_len: buffer length in bytes
310 * @secattr: the security attribute struct to use
311 *
312 * Description:
313 * This function checks the cache to see if a label mapping already exists for
314 * the given key. If there is a match then the cache is adjusted and the
315 * @secattr struct is populated with the correct LSM security attributes. The
316 * cache is adjusted in the following manner if the entry is not already the
317 * first in the cache bucket:
318 *
319 * 1. The cache entry's activity counter is incremented
320 * 2. The previous (higher ranking) entry's activity counter is decremented
321 * 3. If the difference between the two activity counters is geater than
322 * CIPSO_V4_CACHE_REORDERLIMIT the two entries are swapped
323 *
324 * Returns zero on success, -ENOENT for a cache miss, and other negative values
325 * on error.
326 *
327 */
328static int cipso_v4_cache_check(const unsigned char *key,
329 u32 key_len,
330 struct netlbl_lsm_secattr *secattr)
331{
332 u32 bkt;
333 struct cipso_v4_map_cache_entry *entry;
334 struct cipso_v4_map_cache_entry *prev_entry = NULL;
335 u32 hash;
336
337 if (!cipso_v4_cache_enabled)
338 return -ENOENT;
339
340 hash = cipso_v4_map_cache_hash(key, key_len);
Pavel Emelyanov5e0f8922008-05-13 23:23:55 -0700341 bkt = hash & (CIPSO_V4_CACHE_BUCKETS - 1);
Paul Moore609c92f2006-09-25 15:52:37 -0700342 spin_lock_bh(&cipso_v4_cache[bkt].lock);
Paul Moore446fda42006-08-03 16:48:06 -0700343 list_for_each_entry(entry, &cipso_v4_cache[bkt].list, list) {
344 if (entry->hash == hash &&
345 entry->key_len == key_len &&
346 memcmp(entry->key, key, key_len) == 0) {
347 entry->activity += 1;
paul.moore@hp.comffb733c2006-10-04 11:46:31 -0400348 atomic_inc(&entry->lsm_data->refcount);
349 secattr->cache = entry->lsm_data;
Paul Moore701a90b2006-11-17 17:38:46 -0500350 secattr->flags |= NETLBL_SECATTR_CACHE;
Paul Moore16efd452008-01-29 08:37:59 -0500351 secattr->type = NETLBL_NLTYPE_CIPSOV4;
Paul Moore446fda42006-08-03 16:48:06 -0700352 if (prev_entry == NULL) {
Paul Moore609c92f2006-09-25 15:52:37 -0700353 spin_unlock_bh(&cipso_v4_cache[bkt].lock);
Paul Moore446fda42006-08-03 16:48:06 -0700354 return 0;
355 }
356
357 if (prev_entry->activity > 0)
358 prev_entry->activity -= 1;
359 if (entry->activity > prev_entry->activity &&
360 entry->activity - prev_entry->activity >
361 CIPSO_V4_CACHE_REORDERLIMIT) {
362 __list_del(entry->list.prev, entry->list.next);
363 __list_add(&entry->list,
364 prev_entry->list.prev,
365 &prev_entry->list);
366 }
367
Paul Moore609c92f2006-09-25 15:52:37 -0700368 spin_unlock_bh(&cipso_v4_cache[bkt].lock);
Paul Moore446fda42006-08-03 16:48:06 -0700369 return 0;
370 }
371 prev_entry = entry;
372 }
Paul Moore609c92f2006-09-25 15:52:37 -0700373 spin_unlock_bh(&cipso_v4_cache[bkt].lock);
Paul Moore446fda42006-08-03 16:48:06 -0700374
375 return -ENOENT;
376}
377
378/**
379 * cipso_v4_cache_add - Add an entry to the CIPSO cache
380 * @skb: the packet
381 * @secattr: the packet's security attributes
382 *
383 * Description:
384 * Add a new entry into the CIPSO label mapping cache. Add the new entry to
385 * head of the cache bucket's list, if the cache bucket is out of room remove
386 * the last entry in the list first. It is important to note that there is
387 * currently no checking for duplicate keys. Returns zero on success,
388 * negative values on failure.
389 *
390 */
391int cipso_v4_cache_add(const struct sk_buff *skb,
392 const struct netlbl_lsm_secattr *secattr)
393{
394 int ret_val = -EPERM;
395 u32 bkt;
396 struct cipso_v4_map_cache_entry *entry = NULL;
397 struct cipso_v4_map_cache_entry *old_entry = NULL;
398 unsigned char *cipso_ptr;
399 u32 cipso_ptr_len;
400
401 if (!cipso_v4_cache_enabled || cipso_v4_cache_bucketsize <= 0)
402 return 0;
403
404 cipso_ptr = CIPSO_V4_OPTPTR(skb);
405 cipso_ptr_len = cipso_ptr[1];
406
407 entry = kzalloc(sizeof(*entry), GFP_ATOMIC);
408 if (entry == NULL)
409 return -ENOMEM;
Arnaldo Carvalho de Melofac5d732006-11-17 11:14:16 -0200410 entry->key = kmemdup(cipso_ptr, cipso_ptr_len, GFP_ATOMIC);
Paul Moore446fda42006-08-03 16:48:06 -0700411 if (entry->key == NULL) {
412 ret_val = -ENOMEM;
413 goto cache_add_failure;
414 }
Paul Moore446fda42006-08-03 16:48:06 -0700415 entry->key_len = cipso_ptr_len;
416 entry->hash = cipso_v4_map_cache_hash(cipso_ptr, cipso_ptr_len);
paul.moore@hp.comffb733c2006-10-04 11:46:31 -0400417 atomic_inc(&secattr->cache->refcount);
418 entry->lsm_data = secattr->cache;
Paul Moore446fda42006-08-03 16:48:06 -0700419
Pavel Emelyanov5e0f8922008-05-13 23:23:55 -0700420 bkt = entry->hash & (CIPSO_V4_CACHE_BUCKETS - 1);
Paul Moore609c92f2006-09-25 15:52:37 -0700421 spin_lock_bh(&cipso_v4_cache[bkt].lock);
Paul Moore446fda42006-08-03 16:48:06 -0700422 if (cipso_v4_cache[bkt].size < cipso_v4_cache_bucketsize) {
423 list_add(&entry->list, &cipso_v4_cache[bkt].list);
424 cipso_v4_cache[bkt].size += 1;
425 } else {
426 old_entry = list_entry(cipso_v4_cache[bkt].list.prev,
427 struct cipso_v4_map_cache_entry, list);
428 list_del(&old_entry->list);
429 list_add(&entry->list, &cipso_v4_cache[bkt].list);
430 cipso_v4_cache_entry_free(old_entry);
431 }
Paul Moore609c92f2006-09-25 15:52:37 -0700432 spin_unlock_bh(&cipso_v4_cache[bkt].lock);
Paul Moore446fda42006-08-03 16:48:06 -0700433
434 return 0;
435
436cache_add_failure:
437 if (entry)
438 cipso_v4_cache_entry_free(entry);
439 return ret_val;
440}
441
442/*
443 * DOI List Functions
444 */
445
446/**
447 * cipso_v4_doi_search - Searches for a DOI definition
448 * @doi: the DOI to search for
449 *
450 * Description:
451 * Search the DOI definition list for a DOI definition with a DOI value that
452 * matches @doi. The caller is responsibile for calling rcu_read_[un]lock().
453 * Returns a pointer to the DOI definition on success and NULL on failure.
454 */
455static struct cipso_v4_doi *cipso_v4_doi_search(u32 doi)
456{
457 struct cipso_v4_doi *iter;
458
459 list_for_each_entry_rcu(iter, &cipso_v4_doi_list, list)
460 if (iter->doi == doi && iter->valid)
461 return iter;
462 return NULL;
463}
464
465/**
466 * cipso_v4_doi_add - Add a new DOI to the CIPSO protocol engine
467 * @doi_def: the DOI structure
468 *
469 * Description:
470 * The caller defines a new DOI for use by the CIPSO engine and calls this
471 * function to add it to the list of acceptable domains. The caller must
472 * ensure that the mapping table specified in @doi_def->map meets all of the
473 * requirements of the mapping type (see cipso_ipv4.h for details). Returns
474 * zero on success and non-zero on failure.
475 *
476 */
477int cipso_v4_doi_add(struct cipso_v4_doi *doi_def)
478{
Paul Moore6ce61a72006-11-17 17:38:48 -0500479 u32 iter;
480
Paul Moore446fda42006-08-03 16:48:06 -0700481 if (doi_def == NULL || doi_def->doi == CIPSO_V4_DOI_UNKNOWN)
482 return -EINVAL;
Paul Moore6ce61a72006-11-17 17:38:48 -0500483 for (iter = 0; iter < CIPSO_V4_TAG_MAXCNT; iter++) {
484 switch (doi_def->tags[iter]) {
485 case CIPSO_V4_TAG_RBITMAP:
486 break;
Paul Moore484b3662006-11-29 13:18:20 -0500487 case CIPSO_V4_TAG_RANGE:
488 if (doi_def->type != CIPSO_V4_MAP_PASS)
489 return -EINVAL;
490 break;
Paul Moore6ce61a72006-11-17 17:38:48 -0500491 case CIPSO_V4_TAG_INVALID:
492 if (iter == 0)
493 return -EINVAL;
494 break;
Paul Moore654bbc22006-11-29 13:18:19 -0500495 case CIPSO_V4_TAG_ENUM:
496 if (doi_def->type != CIPSO_V4_MAP_PASS)
497 return -EINVAL;
498 break;
Paul Moore6ce61a72006-11-17 17:38:48 -0500499 default:
500 return -EINVAL;
501 }
502 }
Paul Moore446fda42006-08-03 16:48:06 -0700503
504 doi_def->valid = 1;
505 INIT_RCU_HEAD(&doi_def->rcu);
506 INIT_LIST_HEAD(&doi_def->dom_list);
507
Paul Moore446fda42006-08-03 16:48:06 -0700508 spin_lock(&cipso_v4_doi_list_lock);
509 if (cipso_v4_doi_search(doi_def->doi) != NULL)
Paul Moore4be27002007-10-26 04:29:08 -0700510 goto doi_add_failure;
Paul Moore446fda42006-08-03 16:48:06 -0700511 list_add_tail_rcu(&doi_def->list, &cipso_v4_doi_list);
512 spin_unlock(&cipso_v4_doi_list_lock);
Paul Moore446fda42006-08-03 16:48:06 -0700513
514 return 0;
515
Paul Moore4be27002007-10-26 04:29:08 -0700516doi_add_failure:
Paul Moore446fda42006-08-03 16:48:06 -0700517 spin_unlock(&cipso_v4_doi_list_lock);
Paul Moore446fda42006-08-03 16:48:06 -0700518 return -EEXIST;
519}
520
521/**
522 * cipso_v4_doi_remove - Remove an existing DOI from the CIPSO protocol engine
523 * @doi: the DOI value
Paul Moore32f50cd2006-09-28 14:51:47 -0700524 * @audit_secid: the LSM secid to use in the audit message
Paul Moore446fda42006-08-03 16:48:06 -0700525 * @callback: the DOI cleanup/free callback
526 *
527 * Description:
528 * Removes a DOI definition from the CIPSO engine, @callback is called to
529 * free any memory. The NetLabel routines will be called to release their own
530 * LSM domain mappings as well as our own domain list. Returns zero on
531 * success and negative values on failure.
532 *
533 */
Paul Moore32f50cd2006-09-28 14:51:47 -0700534int cipso_v4_doi_remove(u32 doi,
Paul Moore95d4e6b2006-09-29 17:05:05 -0700535 struct netlbl_audit *audit_info,
Paul Moore32f50cd2006-09-28 14:51:47 -0700536 void (*callback) (struct rcu_head * head))
Paul Moore446fda42006-08-03 16:48:06 -0700537{
538 struct cipso_v4_doi *doi_def;
539 struct cipso_v4_domhsh_entry *dom_iter;
540
Paul Moore4be27002007-10-26 04:29:08 -0700541 spin_lock(&cipso_v4_doi_list_lock);
542 doi_def = cipso_v4_doi_search(doi);
543 if (doi_def != NULL) {
Paul Moore446fda42006-08-03 16:48:06 -0700544 doi_def->valid = 0;
545 list_del_rcu(&doi_def->list);
546 spin_unlock(&cipso_v4_doi_list_lock);
Paul Moore4be27002007-10-26 04:29:08 -0700547 rcu_read_lock();
Paul Moore446fda42006-08-03 16:48:06 -0700548 list_for_each_entry_rcu(dom_iter, &doi_def->dom_list, list)
549 if (dom_iter->valid)
Paul Mooreeda61d32008-02-04 22:29:47 -0800550 netlbl_cfg_map_del(dom_iter->domain,
551 audit_info);
Paul Moore446fda42006-08-03 16:48:06 -0700552 rcu_read_unlock();
Paul Moore4be27002007-10-26 04:29:08 -0700553 cipso_v4_cache_invalidate();
Paul Moore446fda42006-08-03 16:48:06 -0700554 call_rcu(&doi_def->rcu, callback);
555 return 0;
556 }
Paul Moore4be27002007-10-26 04:29:08 -0700557 spin_unlock(&cipso_v4_doi_list_lock);
Paul Moore446fda42006-08-03 16:48:06 -0700558
559 return -ENOENT;
560}
561
562/**
563 * cipso_v4_doi_getdef - Returns a pointer to a valid DOI definition
564 * @doi: the DOI value
565 *
566 * Description:
567 * Searches for a valid DOI definition and if one is found it is returned to
568 * the caller. Otherwise NULL is returned. The caller must ensure that
569 * rcu_read_lock() is held while accessing the returned definition.
570 *
571 */
572struct cipso_v4_doi *cipso_v4_doi_getdef(u32 doi)
573{
574 return cipso_v4_doi_search(doi);
575}
576
577/**
Paul Moorefcd48282006-09-25 15:56:09 -0700578 * cipso_v4_doi_walk - Iterate through the DOI definitions
579 * @skip_cnt: skip past this number of DOI definitions, updated
580 * @callback: callback for each DOI definition
581 * @cb_arg: argument for the callback function
Paul Moore446fda42006-08-03 16:48:06 -0700582 *
583 * Description:
Paul Moorefcd48282006-09-25 15:56:09 -0700584 * Iterate over the DOI definition list, skipping the first @skip_cnt entries.
585 * For each entry call @callback, if @callback returns a negative value stop
586 * 'walking' through the list and return. Updates the value in @skip_cnt upon
587 * return. Returns zero on success, negative values on failure.
Paul Moore446fda42006-08-03 16:48:06 -0700588 *
589 */
Paul Moorefcd48282006-09-25 15:56:09 -0700590int cipso_v4_doi_walk(u32 *skip_cnt,
591 int (*callback) (struct cipso_v4_doi *doi_def, void *arg),
592 void *cb_arg)
Paul Moore446fda42006-08-03 16:48:06 -0700593{
Paul Moorefcd48282006-09-25 15:56:09 -0700594 int ret_val = -ENOENT;
Paul Moore446fda42006-08-03 16:48:06 -0700595 u32 doi_cnt = 0;
Paul Moorefcd48282006-09-25 15:56:09 -0700596 struct cipso_v4_doi *iter_doi;
Paul Moore446fda42006-08-03 16:48:06 -0700597
598 rcu_read_lock();
Paul Moorefcd48282006-09-25 15:56:09 -0700599 list_for_each_entry_rcu(iter_doi, &cipso_v4_doi_list, list)
600 if (iter_doi->valid) {
601 if (doi_cnt++ < *skip_cnt)
602 continue;
603 ret_val = callback(iter_doi, cb_arg);
604 if (ret_val < 0) {
605 doi_cnt--;
606 goto doi_walk_return;
Paul Moore446fda42006-08-03 16:48:06 -0700607 }
Paul Moorefcd48282006-09-25 15:56:09 -0700608 }
Paul Moore446fda42006-08-03 16:48:06 -0700609
Paul Moorefcd48282006-09-25 15:56:09 -0700610doi_walk_return:
Paul Moore446fda42006-08-03 16:48:06 -0700611 rcu_read_unlock();
Paul Moorefcd48282006-09-25 15:56:09 -0700612 *skip_cnt = doi_cnt;
613 return ret_val;
Paul Moore446fda42006-08-03 16:48:06 -0700614}
615
616/**
617 * cipso_v4_doi_domhsh_add - Adds a domain entry to a DOI definition
618 * @doi_def: the DOI definition
619 * @domain: the domain to add
620 *
621 * Description:
Michael Opdenacker59c51592007-05-09 08:57:56 +0200622 * Adds the @domain to the DOI specified by @doi_def, this function
Paul Moore446fda42006-08-03 16:48:06 -0700623 * should only be called by external functions (i.e. NetLabel). This function
624 * does allocate memory. Returns zero on success, negative values on failure.
625 *
626 */
627int cipso_v4_doi_domhsh_add(struct cipso_v4_doi *doi_def, const char *domain)
628{
629 struct cipso_v4_domhsh_entry *iter;
630 struct cipso_v4_domhsh_entry *new_dom;
631
632 new_dom = kzalloc(sizeof(*new_dom), GFP_KERNEL);
633 if (new_dom == NULL)
634 return -ENOMEM;
635 if (domain) {
636 new_dom->domain = kstrdup(domain, GFP_KERNEL);
637 if (new_dom->domain == NULL) {
638 kfree(new_dom);
639 return -ENOMEM;
640 }
641 }
642 new_dom->valid = 1;
643 INIT_RCU_HEAD(&new_dom->rcu);
644
Paul Moore446fda42006-08-03 16:48:06 -0700645 spin_lock(&cipso_v4_doi_list_lock);
Paul Moore4be27002007-10-26 04:29:08 -0700646 list_for_each_entry(iter, &doi_def->dom_list, list)
Paul Moore446fda42006-08-03 16:48:06 -0700647 if (iter->valid &&
648 ((domain != NULL && iter->domain != NULL &&
649 strcmp(iter->domain, domain) == 0) ||
650 (domain == NULL && iter->domain == NULL))) {
651 spin_unlock(&cipso_v4_doi_list_lock);
Paul Moore446fda42006-08-03 16:48:06 -0700652 kfree(new_dom->domain);
653 kfree(new_dom);
654 return -EEXIST;
655 }
656 list_add_tail_rcu(&new_dom->list, &doi_def->dom_list);
657 spin_unlock(&cipso_v4_doi_list_lock);
Paul Moore446fda42006-08-03 16:48:06 -0700658
659 return 0;
660}
661
662/**
663 * cipso_v4_doi_domhsh_remove - Removes a domain entry from a DOI definition
664 * @doi_def: the DOI definition
665 * @domain: the domain to remove
666 *
667 * Description:
668 * Removes the @domain from the DOI specified by @doi_def, this function
669 * should only be called by external functions (i.e. NetLabel). Returns zero
670 * on success and negative values on error.
671 *
672 */
673int cipso_v4_doi_domhsh_remove(struct cipso_v4_doi *doi_def,
674 const char *domain)
675{
676 struct cipso_v4_domhsh_entry *iter;
677
Paul Moore446fda42006-08-03 16:48:06 -0700678 spin_lock(&cipso_v4_doi_list_lock);
Paul Moore4be27002007-10-26 04:29:08 -0700679 list_for_each_entry(iter, &doi_def->dom_list, list)
Paul Moore446fda42006-08-03 16:48:06 -0700680 if (iter->valid &&
681 ((domain != NULL && iter->domain != NULL &&
682 strcmp(iter->domain, domain) == 0) ||
683 (domain == NULL && iter->domain == NULL))) {
684 iter->valid = 0;
685 list_del_rcu(&iter->list);
686 spin_unlock(&cipso_v4_doi_list_lock);
Paul Moore446fda42006-08-03 16:48:06 -0700687 call_rcu(&iter->rcu, cipso_v4_doi_domhsh_free);
Paul Moore446fda42006-08-03 16:48:06 -0700688 return 0;
689 }
690 spin_unlock(&cipso_v4_doi_list_lock);
Paul Moore446fda42006-08-03 16:48:06 -0700691
692 return -ENOENT;
693}
694
695/*
696 * Label Mapping Functions
697 */
698
699/**
700 * cipso_v4_map_lvl_valid - Checks to see if the given level is understood
701 * @doi_def: the DOI definition
702 * @level: the level to check
703 *
704 * Description:
705 * Checks the given level against the given DOI definition and returns a
706 * negative value if the level does not have a valid mapping and a zero value
707 * if the level is defined by the DOI.
708 *
709 */
710static int cipso_v4_map_lvl_valid(const struct cipso_v4_doi *doi_def, u8 level)
711{
712 switch (doi_def->type) {
713 case CIPSO_V4_MAP_PASS:
714 return 0;
715 case CIPSO_V4_MAP_STD:
716 if (doi_def->map.std->lvl.cipso[level] < CIPSO_V4_INV_LVL)
717 return 0;
718 break;
719 }
720
721 return -EFAULT;
722}
723
724/**
725 * cipso_v4_map_lvl_hton - Perform a level mapping from the host to the network
726 * @doi_def: the DOI definition
727 * @host_lvl: the host MLS level
728 * @net_lvl: the network/CIPSO MLS level
729 *
730 * Description:
731 * Perform a label mapping to translate a local MLS level to the correct
732 * CIPSO level using the given DOI definition. Returns zero on success,
733 * negative values otherwise.
734 *
735 */
736static int cipso_v4_map_lvl_hton(const struct cipso_v4_doi *doi_def,
737 u32 host_lvl,
738 u32 *net_lvl)
739{
740 switch (doi_def->type) {
741 case CIPSO_V4_MAP_PASS:
742 *net_lvl = host_lvl;
743 return 0;
744 case CIPSO_V4_MAP_STD:
Paul Moorec6387a82007-03-02 13:19:02 -0800745 if (host_lvl < doi_def->map.std->lvl.local_size &&
746 doi_def->map.std->lvl.local[host_lvl] < CIPSO_V4_INV_LVL) {
Paul Moore446fda42006-08-03 16:48:06 -0700747 *net_lvl = doi_def->map.std->lvl.local[host_lvl];
748 return 0;
749 }
Paul Moorec6387a82007-03-02 13:19:02 -0800750 return -EPERM;
Paul Moore446fda42006-08-03 16:48:06 -0700751 }
752
753 return -EINVAL;
754}
755
756/**
757 * cipso_v4_map_lvl_ntoh - Perform a level mapping from the network to the host
758 * @doi_def: the DOI definition
759 * @net_lvl: the network/CIPSO MLS level
760 * @host_lvl: the host MLS level
761 *
762 * Description:
763 * Perform a label mapping to translate a CIPSO level to the correct local MLS
764 * level using the given DOI definition. Returns zero on success, negative
765 * values otherwise.
766 *
767 */
768static int cipso_v4_map_lvl_ntoh(const struct cipso_v4_doi *doi_def,
769 u32 net_lvl,
770 u32 *host_lvl)
771{
772 struct cipso_v4_std_map_tbl *map_tbl;
773
774 switch (doi_def->type) {
775 case CIPSO_V4_MAP_PASS:
776 *host_lvl = net_lvl;
777 return 0;
778 case CIPSO_V4_MAP_STD:
779 map_tbl = doi_def->map.std;
780 if (net_lvl < map_tbl->lvl.cipso_size &&
781 map_tbl->lvl.cipso[net_lvl] < CIPSO_V4_INV_LVL) {
782 *host_lvl = doi_def->map.std->lvl.cipso[net_lvl];
783 return 0;
784 }
Paul Moorec6387a82007-03-02 13:19:02 -0800785 return -EPERM;
Paul Moore446fda42006-08-03 16:48:06 -0700786 }
787
788 return -EINVAL;
789}
790
791/**
792 * cipso_v4_map_cat_rbm_valid - Checks to see if the category bitmap is valid
793 * @doi_def: the DOI definition
794 * @bitmap: category bitmap
795 * @bitmap_len: bitmap length in bytes
796 *
797 * Description:
798 * Checks the given category bitmap against the given DOI definition and
799 * returns a negative value if any of the categories in the bitmap do not have
800 * a valid mapping and a zero value if all of the categories are valid.
801 *
802 */
803static int cipso_v4_map_cat_rbm_valid(const struct cipso_v4_doi *doi_def,
804 const unsigned char *bitmap,
805 u32 bitmap_len)
806{
807 int cat = -1;
808 u32 bitmap_len_bits = bitmap_len * 8;
Paul Moore044a68e2006-10-11 19:10:47 -0400809 u32 cipso_cat_size;
810 u32 *cipso_array;
Paul Moore446fda42006-08-03 16:48:06 -0700811
812 switch (doi_def->type) {
813 case CIPSO_V4_MAP_PASS:
814 return 0;
815 case CIPSO_V4_MAP_STD:
Paul Moore044a68e2006-10-11 19:10:47 -0400816 cipso_cat_size = doi_def->map.std->cat.cipso_size;
817 cipso_array = doi_def->map.std->cat.cipso;
Paul Moore446fda42006-08-03 16:48:06 -0700818 for (;;) {
819 cat = cipso_v4_bitmap_walk(bitmap,
820 bitmap_len_bits,
821 cat + 1,
822 1);
823 if (cat < 0)
824 break;
825 if (cat >= cipso_cat_size ||
826 cipso_array[cat] >= CIPSO_V4_INV_CAT)
827 return -EFAULT;
828 }
829
830 if (cat == -1)
831 return 0;
832 break;
833 }
834
835 return -EFAULT;
836}
837
838/**
839 * cipso_v4_map_cat_rbm_hton - Perform a category mapping from host to network
840 * @doi_def: the DOI definition
Paul Moore02752762006-11-29 13:18:18 -0500841 * @secattr: the security attributes
Paul Moore446fda42006-08-03 16:48:06 -0700842 * @net_cat: the zero'd out category bitmap in network/CIPSO format
843 * @net_cat_len: the length of the CIPSO bitmap in bytes
844 *
845 * Description:
846 * Perform a label mapping to translate a local MLS category bitmap to the
847 * correct CIPSO bitmap using the given DOI definition. Returns the minimum
848 * size in bytes of the network bitmap on success, negative values otherwise.
849 *
850 */
851static int cipso_v4_map_cat_rbm_hton(const struct cipso_v4_doi *doi_def,
Paul Moore02752762006-11-29 13:18:18 -0500852 const struct netlbl_lsm_secattr *secattr,
Paul Moore446fda42006-08-03 16:48:06 -0700853 unsigned char *net_cat,
854 u32 net_cat_len)
855{
856 int host_spot = -1;
Paul Moore02752762006-11-29 13:18:18 -0500857 u32 net_spot = CIPSO_V4_INV_CAT;
Paul Moore446fda42006-08-03 16:48:06 -0700858 u32 net_spot_max = 0;
Paul Moore446fda42006-08-03 16:48:06 -0700859 u32 net_clen_bits = net_cat_len * 8;
Paul Moore02752762006-11-29 13:18:18 -0500860 u32 host_cat_size = 0;
861 u32 *host_cat_array = NULL;
Paul Moore446fda42006-08-03 16:48:06 -0700862
Paul Moore02752762006-11-29 13:18:18 -0500863 if (doi_def->type == CIPSO_V4_MAP_STD) {
Paul Moore044a68e2006-10-11 19:10:47 -0400864 host_cat_size = doi_def->map.std->cat.local_size;
865 host_cat_array = doi_def->map.std->cat.local;
Paul Moore02752762006-11-29 13:18:18 -0500866 }
867
868 for (;;) {
Paul Moore16efd452008-01-29 08:37:59 -0500869 host_spot = netlbl_secattr_catmap_walk(secattr->attr.mls.cat,
Paul Moore02752762006-11-29 13:18:18 -0500870 host_spot + 1);
871 if (host_spot < 0)
872 break;
873
874 switch (doi_def->type) {
875 case CIPSO_V4_MAP_PASS:
876 net_spot = host_spot;
877 break;
878 case CIPSO_V4_MAP_STD:
Paul Moore446fda42006-08-03 16:48:06 -0700879 if (host_spot >= host_cat_size)
880 return -EPERM;
Paul Moore446fda42006-08-03 16:48:06 -0700881 net_spot = host_cat_array[host_spot];
Paul Moore9fade4b2006-11-17 17:38:50 -0500882 if (net_spot >= CIPSO_V4_INV_CAT)
883 return -EPERM;
Paul Moore02752762006-11-29 13:18:18 -0500884 break;
Paul Moore446fda42006-08-03 16:48:06 -0700885 }
Paul Moore02752762006-11-29 13:18:18 -0500886 if (net_spot >= net_clen_bits)
887 return -ENOSPC;
888 cipso_v4_bitmap_setbit(net_cat, net_spot, 1);
Paul Moore446fda42006-08-03 16:48:06 -0700889
Paul Moore02752762006-11-29 13:18:18 -0500890 if (net_spot > net_spot_max)
891 net_spot_max = net_spot;
Paul Moore446fda42006-08-03 16:48:06 -0700892 }
893
Paul Moore02752762006-11-29 13:18:18 -0500894 if (++net_spot_max % 8)
895 return net_spot_max / 8 + 1;
896 return net_spot_max / 8;
Paul Moore446fda42006-08-03 16:48:06 -0700897}
898
899/**
900 * cipso_v4_map_cat_rbm_ntoh - Perform a category mapping from network to host
901 * @doi_def: the DOI definition
902 * @net_cat: the category bitmap in network/CIPSO format
903 * @net_cat_len: the length of the CIPSO bitmap in bytes
Paul Moore02752762006-11-29 13:18:18 -0500904 * @secattr: the security attributes
Paul Moore446fda42006-08-03 16:48:06 -0700905 *
906 * Description:
907 * Perform a label mapping to translate a CIPSO bitmap to the correct local
Paul Moore02752762006-11-29 13:18:18 -0500908 * MLS category bitmap using the given DOI definition. Returns zero on
909 * success, negative values on failure.
Paul Moore446fda42006-08-03 16:48:06 -0700910 *
911 */
912static int cipso_v4_map_cat_rbm_ntoh(const struct cipso_v4_doi *doi_def,
913 const unsigned char *net_cat,
914 u32 net_cat_len,
Paul Moore02752762006-11-29 13:18:18 -0500915 struct netlbl_lsm_secattr *secattr)
Paul Moore446fda42006-08-03 16:48:06 -0700916{
Paul Moore02752762006-11-29 13:18:18 -0500917 int ret_val;
Paul Moore446fda42006-08-03 16:48:06 -0700918 int net_spot = -1;
Paul Moore02752762006-11-29 13:18:18 -0500919 u32 host_spot = CIPSO_V4_INV_CAT;
Paul Moore446fda42006-08-03 16:48:06 -0700920 u32 net_clen_bits = net_cat_len * 8;
Paul Moore02752762006-11-29 13:18:18 -0500921 u32 net_cat_size = 0;
922 u32 *net_cat_array = NULL;
Paul Moore446fda42006-08-03 16:48:06 -0700923
Paul Moore02752762006-11-29 13:18:18 -0500924 if (doi_def->type == CIPSO_V4_MAP_STD) {
Paul Moore044a68e2006-10-11 19:10:47 -0400925 net_cat_size = doi_def->map.std->cat.cipso_size;
926 net_cat_array = doi_def->map.std->cat.cipso;
Paul Moore02752762006-11-29 13:18:18 -0500927 }
Paul Moore446fda42006-08-03 16:48:06 -0700928
Paul Moore02752762006-11-29 13:18:18 -0500929 for (;;) {
930 net_spot = cipso_v4_bitmap_walk(net_cat,
931 net_clen_bits,
932 net_spot + 1,
933 1);
934 if (net_spot < 0) {
935 if (net_spot == -2)
936 return -EFAULT;
937 return 0;
938 }
939
940 switch (doi_def->type) {
941 case CIPSO_V4_MAP_PASS:
942 host_spot = net_spot;
943 break;
944 case CIPSO_V4_MAP_STD:
945 if (net_spot >= net_cat_size)
946 return -EPERM;
Paul Moore446fda42006-08-03 16:48:06 -0700947 host_spot = net_cat_array[net_spot];
Paul Moore9fade4b2006-11-17 17:38:50 -0500948 if (host_spot >= CIPSO_V4_INV_CAT)
949 return -EPERM;
Paul Moore02752762006-11-29 13:18:18 -0500950 break;
Paul Moore446fda42006-08-03 16:48:06 -0700951 }
Paul Moore16efd452008-01-29 08:37:59 -0500952 ret_val = netlbl_secattr_catmap_setbit(secattr->attr.mls.cat,
Paul Moore02752762006-11-29 13:18:18 -0500953 host_spot,
954 GFP_ATOMIC);
955 if (ret_val != 0)
956 return ret_val;
Paul Moore446fda42006-08-03 16:48:06 -0700957 }
958
959 return -EINVAL;
960}
961
Paul Moore654bbc22006-11-29 13:18:19 -0500962/**
963 * cipso_v4_map_cat_enum_valid - Checks to see if the categories are valid
964 * @doi_def: the DOI definition
965 * @enumcat: category list
966 * @enumcat_len: length of the category list in bytes
967 *
968 * Description:
969 * Checks the given categories against the given DOI definition and returns a
970 * negative value if any of the categories do not have a valid mapping and a
971 * zero value if all of the categories are valid.
972 *
973 */
974static int cipso_v4_map_cat_enum_valid(const struct cipso_v4_doi *doi_def,
975 const unsigned char *enumcat,
976 u32 enumcat_len)
977{
978 u16 cat;
979 int cat_prev = -1;
980 u32 iter;
981
982 if (doi_def->type != CIPSO_V4_MAP_PASS || enumcat_len & 0x01)
983 return -EFAULT;
984
985 for (iter = 0; iter < enumcat_len; iter += 2) {
Harvey Harrisond3e2ce32008-05-02 16:26:16 -0700986 cat = get_unaligned_be16(&enumcat[iter]);
Paul Moore654bbc22006-11-29 13:18:19 -0500987 if (cat <= cat_prev)
988 return -EFAULT;
989 cat_prev = cat;
990 }
991
992 return 0;
993}
994
995/**
996 * cipso_v4_map_cat_enum_hton - Perform a category mapping from host to network
997 * @doi_def: the DOI definition
998 * @secattr: the security attributes
999 * @net_cat: the zero'd out category list in network/CIPSO format
1000 * @net_cat_len: the length of the CIPSO category list in bytes
1001 *
1002 * Description:
1003 * Perform a label mapping to translate a local MLS category bitmap to the
1004 * correct CIPSO category list using the given DOI definition. Returns the
1005 * size in bytes of the network category bitmap on success, negative values
1006 * otherwise.
1007 *
1008 */
1009static int cipso_v4_map_cat_enum_hton(const struct cipso_v4_doi *doi_def,
1010 const struct netlbl_lsm_secattr *secattr,
1011 unsigned char *net_cat,
1012 u32 net_cat_len)
1013{
1014 int cat = -1;
1015 u32 cat_iter = 0;
1016
1017 for (;;) {
Paul Moore16efd452008-01-29 08:37:59 -05001018 cat = netlbl_secattr_catmap_walk(secattr->attr.mls.cat,
1019 cat + 1);
Paul Moore654bbc22006-11-29 13:18:19 -05001020 if (cat < 0)
1021 break;
1022 if ((cat_iter + 2) > net_cat_len)
1023 return -ENOSPC;
1024
1025 *((__be16 *)&net_cat[cat_iter]) = htons(cat);
1026 cat_iter += 2;
1027 }
1028
1029 return cat_iter;
1030}
1031
1032/**
1033 * cipso_v4_map_cat_enum_ntoh - Perform a category mapping from network to host
1034 * @doi_def: the DOI definition
1035 * @net_cat: the category list in network/CIPSO format
1036 * @net_cat_len: the length of the CIPSO bitmap in bytes
1037 * @secattr: the security attributes
1038 *
1039 * Description:
1040 * Perform a label mapping to translate a CIPSO category list to the correct
1041 * local MLS category bitmap using the given DOI definition. Returns zero on
1042 * success, negative values on failure.
1043 *
1044 */
1045static int cipso_v4_map_cat_enum_ntoh(const struct cipso_v4_doi *doi_def,
1046 const unsigned char *net_cat,
1047 u32 net_cat_len,
1048 struct netlbl_lsm_secattr *secattr)
1049{
1050 int ret_val;
1051 u32 iter;
1052
1053 for (iter = 0; iter < net_cat_len; iter += 2) {
Paul Moore16efd452008-01-29 08:37:59 -05001054 ret_val = netlbl_secattr_catmap_setbit(secattr->attr.mls.cat,
Harvey Harrisond3e2ce32008-05-02 16:26:16 -07001055 get_unaligned_be16(&net_cat[iter]),
Paul Moore50e5d352007-06-07 18:38:14 -07001056 GFP_ATOMIC);
Paul Moore654bbc22006-11-29 13:18:19 -05001057 if (ret_val != 0)
1058 return ret_val;
1059 }
1060
1061 return 0;
1062}
1063
Paul Moore484b3662006-11-29 13:18:20 -05001064/**
1065 * cipso_v4_map_cat_rng_valid - Checks to see if the categories are valid
1066 * @doi_def: the DOI definition
1067 * @rngcat: category list
1068 * @rngcat_len: length of the category list in bytes
1069 *
1070 * Description:
1071 * Checks the given categories against the given DOI definition and returns a
1072 * negative value if any of the categories do not have a valid mapping and a
1073 * zero value if all of the categories are valid.
1074 *
1075 */
1076static int cipso_v4_map_cat_rng_valid(const struct cipso_v4_doi *doi_def,
1077 const unsigned char *rngcat,
1078 u32 rngcat_len)
1079{
1080 u16 cat_high;
1081 u16 cat_low;
1082 u32 cat_prev = CIPSO_V4_MAX_REM_CATS + 1;
1083 u32 iter;
1084
1085 if (doi_def->type != CIPSO_V4_MAP_PASS || rngcat_len & 0x01)
1086 return -EFAULT;
1087
1088 for (iter = 0; iter < rngcat_len; iter += 4) {
Harvey Harrisond3e2ce32008-05-02 16:26:16 -07001089 cat_high = get_unaligned_be16(&rngcat[iter]);
Paul Moore484b3662006-11-29 13:18:20 -05001090 if ((iter + 4) <= rngcat_len)
Harvey Harrisond3e2ce32008-05-02 16:26:16 -07001091 cat_low = get_unaligned_be16(&rngcat[iter + 2]);
Paul Moore484b3662006-11-29 13:18:20 -05001092 else
1093 cat_low = 0;
1094
1095 if (cat_high > cat_prev)
1096 return -EFAULT;
1097
1098 cat_prev = cat_low;
1099 }
1100
1101 return 0;
1102}
1103
1104/**
1105 * cipso_v4_map_cat_rng_hton - Perform a category mapping from host to network
1106 * @doi_def: the DOI definition
1107 * @secattr: the security attributes
1108 * @net_cat: the zero'd out category list in network/CIPSO format
1109 * @net_cat_len: the length of the CIPSO category list in bytes
1110 *
1111 * Description:
1112 * Perform a label mapping to translate a local MLS category bitmap to the
1113 * correct CIPSO category list using the given DOI definition. Returns the
1114 * size in bytes of the network category bitmap on success, negative values
1115 * otherwise.
1116 *
1117 */
1118static int cipso_v4_map_cat_rng_hton(const struct cipso_v4_doi *doi_def,
1119 const struct netlbl_lsm_secattr *secattr,
1120 unsigned char *net_cat,
1121 u32 net_cat_len)
1122{
Paul Moore484b3662006-11-29 13:18:20 -05001123 int iter = -1;
Paul Mooref998e8c2007-02-28 15:14:20 -05001124 u16 array[CIPSO_V4_TAG_RNG_CAT_MAX * 2];
Paul Moore484b3662006-11-29 13:18:20 -05001125 u32 array_cnt = 0;
1126 u32 cat_size = 0;
1127
Paul Mooref998e8c2007-02-28 15:14:20 -05001128 /* make sure we don't overflow the 'array[]' variable */
Paul Moore128c6b62007-02-28 15:14:21 -05001129 if (net_cat_len >
1130 (CIPSO_V4_OPT_LEN_MAX - CIPSO_V4_HDR_LEN - CIPSO_V4_TAG_RNG_BLEN))
1131 return -ENOSPC;
Paul Moore484b3662006-11-29 13:18:20 -05001132
1133 for (;;) {
Paul Moore16efd452008-01-29 08:37:59 -05001134 iter = netlbl_secattr_catmap_walk(secattr->attr.mls.cat,
1135 iter + 1);
Paul Moore484b3662006-11-29 13:18:20 -05001136 if (iter < 0)
1137 break;
1138 cat_size += (iter == 0 ? 0 : sizeof(u16));
1139 if (cat_size > net_cat_len)
1140 return -ENOSPC;
1141 array[array_cnt++] = iter;
1142
Paul Moore16efd452008-01-29 08:37:59 -05001143 iter = netlbl_secattr_catmap_walk_rng(secattr->attr.mls.cat,
1144 iter);
Paul Moore484b3662006-11-29 13:18:20 -05001145 if (iter < 0)
1146 return -EFAULT;
1147 cat_size += sizeof(u16);
1148 if (cat_size > net_cat_len)
1149 return -ENOSPC;
1150 array[array_cnt++] = iter;
1151 }
1152
1153 for (iter = 0; array_cnt > 0;) {
1154 *((__be16 *)&net_cat[iter]) = htons(array[--array_cnt]);
1155 iter += 2;
1156 array_cnt--;
1157 if (array[array_cnt] != 0) {
1158 *((__be16 *)&net_cat[iter]) = htons(array[array_cnt]);
1159 iter += 2;
1160 }
1161 }
1162
1163 return cat_size;
1164}
1165
1166/**
1167 * cipso_v4_map_cat_rng_ntoh - Perform a category mapping from network to host
1168 * @doi_def: the DOI definition
1169 * @net_cat: the category list in network/CIPSO format
1170 * @net_cat_len: the length of the CIPSO bitmap in bytes
1171 * @secattr: the security attributes
1172 *
1173 * Description:
1174 * Perform a label mapping to translate a CIPSO category list to the correct
1175 * local MLS category bitmap using the given DOI definition. Returns zero on
1176 * success, negative values on failure.
1177 *
1178 */
1179static int cipso_v4_map_cat_rng_ntoh(const struct cipso_v4_doi *doi_def,
1180 const unsigned char *net_cat,
1181 u32 net_cat_len,
1182 struct netlbl_lsm_secattr *secattr)
1183{
1184 int ret_val;
1185 u32 net_iter;
1186 u16 cat_low;
1187 u16 cat_high;
1188
Stephen Hemminger132adf52007-03-08 20:44:43 -08001189 for (net_iter = 0; net_iter < net_cat_len; net_iter += 4) {
Harvey Harrisond3e2ce32008-05-02 16:26:16 -07001190 cat_high = get_unaligned_be16(&net_cat[net_iter]);
Paul Moore484b3662006-11-29 13:18:20 -05001191 if ((net_iter + 4) <= net_cat_len)
Harvey Harrisond3e2ce32008-05-02 16:26:16 -07001192 cat_low = get_unaligned_be16(&net_cat[net_iter + 2]);
Paul Moore484b3662006-11-29 13:18:20 -05001193 else
1194 cat_low = 0;
1195
Paul Moore16efd452008-01-29 08:37:59 -05001196 ret_val = netlbl_secattr_catmap_setrng(secattr->attr.mls.cat,
Paul Moore484b3662006-11-29 13:18:20 -05001197 cat_low,
1198 cat_high,
1199 GFP_ATOMIC);
1200 if (ret_val != 0)
1201 return ret_val;
1202 }
1203
1204 return 0;
1205}
1206
Paul Moore446fda42006-08-03 16:48:06 -07001207/*
1208 * Protocol Handling Functions
1209 */
1210
Paul Moore446fda42006-08-03 16:48:06 -07001211/**
1212 * cipso_v4_gentag_hdr - Generate a CIPSO option header
1213 * @doi_def: the DOI definition
Paul Moore91b1ed02006-11-17 17:38:49 -05001214 * @len: the total tag length in bytes, not including this header
Paul Moore446fda42006-08-03 16:48:06 -07001215 * @buf: the CIPSO option buffer
1216 *
1217 * Description:
Paul Moore91b1ed02006-11-17 17:38:49 -05001218 * Write a CIPSO header into the beginning of @buffer.
Paul Moore446fda42006-08-03 16:48:06 -07001219 *
1220 */
Paul Moore91b1ed02006-11-17 17:38:49 -05001221static void cipso_v4_gentag_hdr(const struct cipso_v4_doi *doi_def,
1222 unsigned char *buf,
1223 u32 len)
Paul Moore446fda42006-08-03 16:48:06 -07001224{
Paul Moore446fda42006-08-03 16:48:06 -07001225 buf[0] = IPOPT_CIPSO;
1226 buf[1] = CIPSO_V4_HDR_LEN + len;
Al Viro714e85b2006-11-14 20:51:49 -08001227 *(__be32 *)&buf[2] = htonl(doi_def->doi);
Paul Moore446fda42006-08-03 16:48:06 -07001228}
1229
Paul Moore446fda42006-08-03 16:48:06 -07001230/**
1231 * cipso_v4_gentag_rbm - Generate a CIPSO restricted bitmap tag (type #1)
1232 * @doi_def: the DOI definition
1233 * @secattr: the security attributes
1234 * @buffer: the option buffer
1235 * @buffer_len: length of buffer in bytes
1236 *
1237 * Description:
1238 * Generate a CIPSO option using the restricted bitmap tag, tag type #1. The
1239 * actual buffer length may be larger than the indicated size due to
Paul Moore91b1ed02006-11-17 17:38:49 -05001240 * translation between host and network category bitmaps. Returns the size of
1241 * the tag on success, negative values on failure.
Paul Moore446fda42006-08-03 16:48:06 -07001242 *
1243 */
1244static int cipso_v4_gentag_rbm(const struct cipso_v4_doi *doi_def,
1245 const struct netlbl_lsm_secattr *secattr,
Paul Moore91b1ed02006-11-17 17:38:49 -05001246 unsigned char *buffer,
1247 u32 buffer_len)
Paul Moore446fda42006-08-03 16:48:06 -07001248{
Paul Moore701a90b2006-11-17 17:38:46 -05001249 int ret_val;
Paul Moore91b1ed02006-11-17 17:38:49 -05001250 u32 tag_len;
Paul Moore446fda42006-08-03 16:48:06 -07001251 u32 level;
1252
Paul Moore701a90b2006-11-17 17:38:46 -05001253 if ((secattr->flags & NETLBL_SECATTR_MLS_LVL) == 0)
1254 return -EPERM;
1255
Paul Moore16efd452008-01-29 08:37:59 -05001256 ret_val = cipso_v4_map_lvl_hton(doi_def,
1257 secattr->attr.mls.lvl,
1258 &level);
Paul Moore91b1ed02006-11-17 17:38:49 -05001259 if (ret_val != 0)
1260 return ret_val;
Paul Moore446fda42006-08-03 16:48:06 -07001261
Paul Moore91b1ed02006-11-17 17:38:49 -05001262 if (secattr->flags & NETLBL_SECATTR_MLS_CAT) {
Paul Moore446fda42006-08-03 16:48:06 -07001263 ret_val = cipso_v4_map_cat_rbm_hton(doi_def,
Paul Moore02752762006-11-29 13:18:18 -05001264 secattr,
Paul Moore91b1ed02006-11-17 17:38:49 -05001265 &buffer[4],
1266 buffer_len - 4);
Paul Moore446fda42006-08-03 16:48:06 -07001267 if (ret_val < 0)
Paul Moore91b1ed02006-11-17 17:38:49 -05001268 return ret_val;
Paul Moore446fda42006-08-03 16:48:06 -07001269
1270 /* This will send packets using the "optimized" format when
1271 * possibile as specified in section 3.4.2.6 of the
1272 * CIPSO draft. */
Paul Moore701a90b2006-11-17 17:38:46 -05001273 if (cipso_v4_rbm_optfmt && ret_val > 0 && ret_val <= 10)
Paul Moore91b1ed02006-11-17 17:38:49 -05001274 tag_len = 14;
Paul Moore701a90b2006-11-17 17:38:46 -05001275 else
Paul Moore91b1ed02006-11-17 17:38:49 -05001276 tag_len = 4 + ret_val;
1277 } else
1278 tag_len = 4;
Paul Moore446fda42006-08-03 16:48:06 -07001279
Paul Moore91b1ed02006-11-17 17:38:49 -05001280 buffer[0] = 0x01;
1281 buffer[1] = tag_len;
1282 buffer[3] = level;
Paul Moore446fda42006-08-03 16:48:06 -07001283
Paul Moore91b1ed02006-11-17 17:38:49 -05001284 return tag_len;
Paul Moore446fda42006-08-03 16:48:06 -07001285}
1286
1287/**
1288 * cipso_v4_parsetag_rbm - Parse a CIPSO restricted bitmap tag
1289 * @doi_def: the DOI definition
1290 * @tag: the CIPSO tag
1291 * @secattr: the security attributes
1292 *
1293 * Description:
1294 * Parse a CIPSO restricted bitmap tag (tag type #1) and return the security
1295 * attributes in @secattr. Return zero on success, negatives values on
1296 * failure.
1297 *
1298 */
1299static int cipso_v4_parsetag_rbm(const struct cipso_v4_doi *doi_def,
1300 const unsigned char *tag,
1301 struct netlbl_lsm_secattr *secattr)
1302{
1303 int ret_val;
1304 u8 tag_len = tag[1];
1305 u32 level;
1306
1307 ret_val = cipso_v4_map_lvl_ntoh(doi_def, tag[3], &level);
1308 if (ret_val != 0)
1309 return ret_val;
Paul Moore16efd452008-01-29 08:37:59 -05001310 secattr->attr.mls.lvl = level;
Paul Moore701a90b2006-11-17 17:38:46 -05001311 secattr->flags |= NETLBL_SECATTR_MLS_LVL;
Paul Moore446fda42006-08-03 16:48:06 -07001312
1313 if (tag_len > 4) {
Paul Moore16efd452008-01-29 08:37:59 -05001314 secattr->attr.mls.cat =
1315 netlbl_secattr_catmap_alloc(GFP_ATOMIC);
1316 if (secattr->attr.mls.cat == NULL)
Paul Moore446fda42006-08-03 16:48:06 -07001317 return -ENOMEM;
1318
1319 ret_val = cipso_v4_map_cat_rbm_ntoh(doi_def,
1320 &tag[4],
1321 tag_len - 4,
Paul Moore02752762006-11-29 13:18:18 -05001322 secattr);
1323 if (ret_val != 0) {
Paul Moore16efd452008-01-29 08:37:59 -05001324 netlbl_secattr_catmap_free(secattr->attr.mls.cat);
Paul Moore446fda42006-08-03 16:48:06 -07001325 return ret_val;
1326 }
Paul Moore02752762006-11-29 13:18:18 -05001327
1328 secattr->flags |= NETLBL_SECATTR_MLS_CAT;
Paul Moore446fda42006-08-03 16:48:06 -07001329 }
1330
1331 return 0;
1332}
1333
1334/**
Paul Moore654bbc22006-11-29 13:18:19 -05001335 * cipso_v4_gentag_enum - Generate a CIPSO enumerated tag (type #2)
1336 * @doi_def: the DOI definition
1337 * @secattr: the security attributes
1338 * @buffer: the option buffer
1339 * @buffer_len: length of buffer in bytes
1340 *
1341 * Description:
1342 * Generate a CIPSO option using the enumerated tag, tag type #2. Returns the
1343 * size of the tag on success, negative values on failure.
1344 *
1345 */
1346static int cipso_v4_gentag_enum(const struct cipso_v4_doi *doi_def,
1347 const struct netlbl_lsm_secattr *secattr,
1348 unsigned char *buffer,
1349 u32 buffer_len)
1350{
1351 int ret_val;
1352 u32 tag_len;
1353 u32 level;
1354
1355 if (!(secattr->flags & NETLBL_SECATTR_MLS_LVL))
1356 return -EPERM;
1357
Paul Moore16efd452008-01-29 08:37:59 -05001358 ret_val = cipso_v4_map_lvl_hton(doi_def,
1359 secattr->attr.mls.lvl,
1360 &level);
Paul Moore654bbc22006-11-29 13:18:19 -05001361 if (ret_val != 0)
1362 return ret_val;
1363
1364 if (secattr->flags & NETLBL_SECATTR_MLS_CAT) {
1365 ret_val = cipso_v4_map_cat_enum_hton(doi_def,
1366 secattr,
1367 &buffer[4],
1368 buffer_len - 4);
1369 if (ret_val < 0)
1370 return ret_val;
1371
1372 tag_len = 4 + ret_val;
1373 } else
1374 tag_len = 4;
1375
1376 buffer[0] = 0x02;
1377 buffer[1] = tag_len;
1378 buffer[3] = level;
1379
1380 return tag_len;
1381}
1382
1383/**
1384 * cipso_v4_parsetag_enum - Parse a CIPSO enumerated tag
1385 * @doi_def: the DOI definition
1386 * @tag: the CIPSO tag
1387 * @secattr: the security attributes
1388 *
1389 * Description:
1390 * Parse a CIPSO enumerated tag (tag type #2) and return the security
1391 * attributes in @secattr. Return zero on success, negatives values on
1392 * failure.
1393 *
1394 */
1395static int cipso_v4_parsetag_enum(const struct cipso_v4_doi *doi_def,
1396 const unsigned char *tag,
1397 struct netlbl_lsm_secattr *secattr)
1398{
1399 int ret_val;
1400 u8 tag_len = tag[1];
1401 u32 level;
1402
1403 ret_val = cipso_v4_map_lvl_ntoh(doi_def, tag[3], &level);
1404 if (ret_val != 0)
1405 return ret_val;
Paul Moore16efd452008-01-29 08:37:59 -05001406 secattr->attr.mls.lvl = level;
Paul Moore654bbc22006-11-29 13:18:19 -05001407 secattr->flags |= NETLBL_SECATTR_MLS_LVL;
1408
1409 if (tag_len > 4) {
Paul Moore16efd452008-01-29 08:37:59 -05001410 secattr->attr.mls.cat =
1411 netlbl_secattr_catmap_alloc(GFP_ATOMIC);
1412 if (secattr->attr.mls.cat == NULL)
Paul Moore654bbc22006-11-29 13:18:19 -05001413 return -ENOMEM;
1414
1415 ret_val = cipso_v4_map_cat_enum_ntoh(doi_def,
1416 &tag[4],
1417 tag_len - 4,
1418 secattr);
1419 if (ret_val != 0) {
Paul Moore16efd452008-01-29 08:37:59 -05001420 netlbl_secattr_catmap_free(secattr->attr.mls.cat);
Paul Moore654bbc22006-11-29 13:18:19 -05001421 return ret_val;
1422 }
1423
1424 secattr->flags |= NETLBL_SECATTR_MLS_CAT;
1425 }
1426
1427 return 0;
1428}
1429
1430/**
Paul Moore484b3662006-11-29 13:18:20 -05001431 * cipso_v4_gentag_rng - Generate a CIPSO ranged tag (type #5)
1432 * @doi_def: the DOI definition
1433 * @secattr: the security attributes
1434 * @buffer: the option buffer
1435 * @buffer_len: length of buffer in bytes
1436 *
1437 * Description:
1438 * Generate a CIPSO option using the ranged tag, tag type #5. Returns the
1439 * size of the tag on success, negative values on failure.
1440 *
1441 */
1442static int cipso_v4_gentag_rng(const struct cipso_v4_doi *doi_def,
1443 const struct netlbl_lsm_secattr *secattr,
1444 unsigned char *buffer,
1445 u32 buffer_len)
1446{
1447 int ret_val;
1448 u32 tag_len;
1449 u32 level;
1450
1451 if (!(secattr->flags & NETLBL_SECATTR_MLS_LVL))
1452 return -EPERM;
1453
Paul Moore16efd452008-01-29 08:37:59 -05001454 ret_val = cipso_v4_map_lvl_hton(doi_def,
1455 secattr->attr.mls.lvl,
1456 &level);
Paul Moore484b3662006-11-29 13:18:20 -05001457 if (ret_val != 0)
1458 return ret_val;
1459
1460 if (secattr->flags & NETLBL_SECATTR_MLS_CAT) {
1461 ret_val = cipso_v4_map_cat_rng_hton(doi_def,
1462 secattr,
1463 &buffer[4],
1464 buffer_len - 4);
1465 if (ret_val < 0)
1466 return ret_val;
1467
1468 tag_len = 4 + ret_val;
1469 } else
1470 tag_len = 4;
1471
1472 buffer[0] = 0x05;
1473 buffer[1] = tag_len;
1474 buffer[3] = level;
1475
1476 return tag_len;
1477}
1478
1479/**
1480 * cipso_v4_parsetag_rng - Parse a CIPSO ranged tag
1481 * @doi_def: the DOI definition
1482 * @tag: the CIPSO tag
1483 * @secattr: the security attributes
1484 *
1485 * Description:
1486 * Parse a CIPSO ranged tag (tag type #5) and return the security attributes
1487 * in @secattr. Return zero on success, negatives values on failure.
1488 *
1489 */
1490static int cipso_v4_parsetag_rng(const struct cipso_v4_doi *doi_def,
1491 const unsigned char *tag,
1492 struct netlbl_lsm_secattr *secattr)
1493{
1494 int ret_val;
1495 u8 tag_len = tag[1];
1496 u32 level;
1497
1498 ret_val = cipso_v4_map_lvl_ntoh(doi_def, tag[3], &level);
1499 if (ret_val != 0)
1500 return ret_val;
Paul Moore16efd452008-01-29 08:37:59 -05001501 secattr->attr.mls.lvl = level;
Paul Moore484b3662006-11-29 13:18:20 -05001502 secattr->flags |= NETLBL_SECATTR_MLS_LVL;
1503
1504 if (tag_len > 4) {
Paul Moore16efd452008-01-29 08:37:59 -05001505 secattr->attr.mls.cat =
1506 netlbl_secattr_catmap_alloc(GFP_ATOMIC);
1507 if (secattr->attr.mls.cat == NULL)
Paul Moore484b3662006-11-29 13:18:20 -05001508 return -ENOMEM;
1509
1510 ret_val = cipso_v4_map_cat_rng_ntoh(doi_def,
1511 &tag[4],
1512 tag_len - 4,
1513 secattr);
1514 if (ret_val != 0) {
Paul Moore16efd452008-01-29 08:37:59 -05001515 netlbl_secattr_catmap_free(secattr->attr.mls.cat);
Paul Moore484b3662006-11-29 13:18:20 -05001516 return ret_val;
1517 }
1518
1519 secattr->flags |= NETLBL_SECATTR_MLS_CAT;
1520 }
1521
1522 return 0;
1523}
1524
1525/**
Paul Moore446fda42006-08-03 16:48:06 -07001526 * cipso_v4_validate - Validate a CIPSO option
1527 * @option: the start of the option, on error it is set to point to the error
1528 *
1529 * Description:
1530 * This routine is called to validate a CIPSO option, it checks all of the
1531 * fields to ensure that they are at least valid, see the draft snippet below
1532 * for details. If the option is valid then a zero value is returned and
1533 * the value of @option is unchanged. If the option is invalid then a
1534 * non-zero value is returned and @option is adjusted to point to the
1535 * offending portion of the option. From the IETF draft ...
1536 *
1537 * "If any field within the CIPSO options, such as the DOI identifier, is not
1538 * recognized the IP datagram is discarded and an ICMP 'parameter problem'
1539 * (type 12) is generated and returned. The ICMP code field is set to 'bad
1540 * parameter' (code 0) and the pointer is set to the start of the CIPSO field
1541 * that is unrecognized."
1542 *
1543 */
1544int cipso_v4_validate(unsigned char **option)
1545{
1546 unsigned char *opt = *option;
1547 unsigned char *tag;
1548 unsigned char opt_iter;
1549 unsigned char err_offset = 0;
1550 u8 opt_len;
1551 u8 tag_len;
1552 struct cipso_v4_doi *doi_def = NULL;
1553 u32 tag_iter;
1554
1555 /* caller already checks for length values that are too large */
1556 opt_len = opt[1];
1557 if (opt_len < 8) {
1558 err_offset = 1;
1559 goto validate_return;
1560 }
1561
1562 rcu_read_lock();
Harvey Harrisond3e2ce32008-05-02 16:26:16 -07001563 doi_def = cipso_v4_doi_search(get_unaligned_be32(&opt[2]));
Paul Moore446fda42006-08-03 16:48:06 -07001564 if (doi_def == NULL) {
1565 err_offset = 2;
1566 goto validate_return_locked;
1567 }
1568
1569 opt_iter = 6;
1570 tag = opt + opt_iter;
1571 while (opt_iter < opt_len) {
1572 for (tag_iter = 0; doi_def->tags[tag_iter] != tag[0];)
1573 if (doi_def->tags[tag_iter] == CIPSO_V4_TAG_INVALID ||
1574 ++tag_iter == CIPSO_V4_TAG_MAXCNT) {
1575 err_offset = opt_iter;
1576 goto validate_return_locked;
1577 }
1578
1579 tag_len = tag[1];
1580 if (tag_len > (opt_len - opt_iter)) {
1581 err_offset = opt_iter + 1;
1582 goto validate_return_locked;
1583 }
1584
1585 switch (tag[0]) {
1586 case CIPSO_V4_TAG_RBITMAP:
1587 if (tag_len < 4) {
1588 err_offset = opt_iter + 1;
1589 goto validate_return_locked;
1590 }
1591
1592 /* We are already going to do all the verification
1593 * necessary at the socket layer so from our point of
1594 * view it is safe to turn these checks off (and less
1595 * work), however, the CIPSO draft says we should do
1596 * all the CIPSO validations here but it doesn't
1597 * really specify _exactly_ what we need to validate
1598 * ... so, just make it a sysctl tunable. */
1599 if (cipso_v4_rbm_strictvalid) {
1600 if (cipso_v4_map_lvl_valid(doi_def,
1601 tag[3]) < 0) {
1602 err_offset = opt_iter + 3;
1603 goto validate_return_locked;
1604 }
1605 if (tag_len > 4 &&
1606 cipso_v4_map_cat_rbm_valid(doi_def,
1607 &tag[4],
1608 tag_len - 4) < 0) {
1609 err_offset = opt_iter + 4;
1610 goto validate_return_locked;
1611 }
1612 }
1613 break;
Paul Moore654bbc22006-11-29 13:18:19 -05001614 case CIPSO_V4_TAG_ENUM:
1615 if (tag_len < 4) {
1616 err_offset = opt_iter + 1;
1617 goto validate_return_locked;
1618 }
1619
1620 if (cipso_v4_map_lvl_valid(doi_def,
1621 tag[3]) < 0) {
1622 err_offset = opt_iter + 3;
1623 goto validate_return_locked;
1624 }
1625 if (tag_len > 4 &&
1626 cipso_v4_map_cat_enum_valid(doi_def,
1627 &tag[4],
1628 tag_len - 4) < 0) {
1629 err_offset = opt_iter + 4;
1630 goto validate_return_locked;
1631 }
1632 break;
Paul Moore484b3662006-11-29 13:18:20 -05001633 case CIPSO_V4_TAG_RANGE:
1634 if (tag_len < 4) {
1635 err_offset = opt_iter + 1;
1636 goto validate_return_locked;
1637 }
1638
1639 if (cipso_v4_map_lvl_valid(doi_def,
1640 tag[3]) < 0) {
1641 err_offset = opt_iter + 3;
1642 goto validate_return_locked;
1643 }
1644 if (tag_len > 4 &&
1645 cipso_v4_map_cat_rng_valid(doi_def,
1646 &tag[4],
1647 tag_len - 4) < 0) {
1648 err_offset = opt_iter + 4;
1649 goto validate_return_locked;
1650 }
1651 break;
Paul Moore446fda42006-08-03 16:48:06 -07001652 default:
1653 err_offset = opt_iter;
1654 goto validate_return_locked;
1655 }
1656
1657 tag += tag_len;
1658 opt_iter += tag_len;
1659 }
1660
1661validate_return_locked:
1662 rcu_read_unlock();
1663validate_return:
1664 *option = opt + err_offset;
1665 return err_offset;
1666}
1667
1668/**
1669 * cipso_v4_error - Send the correct reponse for a bad packet
1670 * @skb: the packet
1671 * @error: the error code
1672 * @gateway: CIPSO gateway flag
1673 *
1674 * Description:
1675 * Based on the error code given in @error, send an ICMP error message back to
1676 * the originating host. From the IETF draft ...
1677 *
1678 * "If the contents of the CIPSO [option] are valid but the security label is
1679 * outside of the configured host or port label range, the datagram is
1680 * discarded and an ICMP 'destination unreachable' (type 3) is generated and
1681 * returned. The code field of the ICMP is set to 'communication with
1682 * destination network administratively prohibited' (code 9) or to
1683 * 'communication with destination host administratively prohibited'
1684 * (code 10). The value of the code is dependent on whether the originator
1685 * of the ICMP message is acting as a CIPSO host or a CIPSO gateway. The
1686 * recipient of the ICMP message MUST be able to handle either value. The
1687 * same procedure is performed if a CIPSO [option] can not be added to an
1688 * IP packet because it is too large to fit in the IP options area."
1689 *
1690 * "If the error is triggered by receipt of an ICMP message, the message is
1691 * discarded and no response is permitted (consistent with general ICMP
1692 * processing rules)."
1693 *
1694 */
1695void cipso_v4_error(struct sk_buff *skb, int error, u32 gateway)
1696{
Arnaldo Carvalho de Meloeddc9ec2007-04-20 22:47:35 -07001697 if (ip_hdr(skb)->protocol == IPPROTO_ICMP || error != -EACCES)
Paul Moore446fda42006-08-03 16:48:06 -07001698 return;
1699
1700 if (gateway)
1701 icmp_send(skb, ICMP_DEST_UNREACH, ICMP_NET_ANO, 0);
1702 else
1703 icmp_send(skb, ICMP_DEST_UNREACH, ICMP_HOST_ANO, 0);
1704}
1705
1706/**
Paul Mooreba6ff9f2007-06-07 18:37:15 -07001707 * cipso_v4_sock_setattr - Add a CIPSO option to a socket
1708 * @sk: the socket
Paul Moore446fda42006-08-03 16:48:06 -07001709 * @doi_def: the CIPSO DOI to use
1710 * @secattr: the specific security attributes of the socket
1711 *
1712 * Description:
1713 * Set the CIPSO option on the given socket using the DOI definition and
1714 * security attributes passed to the function. This function requires
Paul Mooreba6ff9f2007-06-07 18:37:15 -07001715 * exclusive access to @sk, which means it either needs to be in the
1716 * process of being created or locked. Returns zero on success and negative
1717 * values on failure.
Paul Moore446fda42006-08-03 16:48:06 -07001718 *
1719 */
Paul Mooreba6ff9f2007-06-07 18:37:15 -07001720int cipso_v4_sock_setattr(struct sock *sk,
1721 const struct cipso_v4_doi *doi_def,
1722 const struct netlbl_lsm_secattr *secattr)
Paul Moore446fda42006-08-03 16:48:06 -07001723{
1724 int ret_val = -EPERM;
1725 u32 iter;
Paul Moore91b1ed02006-11-17 17:38:49 -05001726 unsigned char *buf;
Paul Moore446fda42006-08-03 16:48:06 -07001727 u32 buf_len = 0;
1728 u32 opt_len;
1729 struct ip_options *opt = NULL;
Paul Moore446fda42006-08-03 16:48:06 -07001730 struct inet_sock *sk_inet;
1731 struct inet_connection_sock *sk_conn;
1732
1733 /* In the case of sock_create_lite(), the sock->sk field is not
1734 * defined yet but it is not a problem as the only users of these
1735 * "lite" PF_INET sockets are functions which do an accept() call
1736 * afterwards so we will label the socket as part of the accept(). */
Paul Moore446fda42006-08-03 16:48:06 -07001737 if (sk == NULL)
1738 return 0;
1739
Paul Moore91b1ed02006-11-17 17:38:49 -05001740 /* We allocate the maximum CIPSO option size here so we are probably
1741 * being a little wasteful, but it makes our life _much_ easier later
1742 * on and after all we are only talking about 40 bytes. */
1743 buf_len = CIPSO_V4_OPT_LEN_MAX;
1744 buf = kmalloc(buf_len, GFP_ATOMIC);
1745 if (buf == NULL) {
1746 ret_val = -ENOMEM;
1747 goto socket_setattr_failure;
1748 }
1749
Paul Moore446fda42006-08-03 16:48:06 -07001750 /* XXX - This code assumes only one tag per CIPSO option which isn't
1751 * really a good assumption to make but since we only support the MAC
1752 * tags right now it is a safe assumption. */
1753 iter = 0;
1754 do {
Paul Moore91b1ed02006-11-17 17:38:49 -05001755 memset(buf, 0, buf_len);
Paul Moore446fda42006-08-03 16:48:06 -07001756 switch (doi_def->tags[iter]) {
1757 case CIPSO_V4_TAG_RBITMAP:
1758 ret_val = cipso_v4_gentag_rbm(doi_def,
Paul Moore91b1ed02006-11-17 17:38:49 -05001759 secattr,
1760 &buf[CIPSO_V4_HDR_LEN],
1761 buf_len - CIPSO_V4_HDR_LEN);
Paul Moore446fda42006-08-03 16:48:06 -07001762 break;
Paul Moore654bbc22006-11-29 13:18:19 -05001763 case CIPSO_V4_TAG_ENUM:
1764 ret_val = cipso_v4_gentag_enum(doi_def,
1765 secattr,
1766 &buf[CIPSO_V4_HDR_LEN],
1767 buf_len - CIPSO_V4_HDR_LEN);
1768 break;
Paul Moore484b3662006-11-29 13:18:20 -05001769 case CIPSO_V4_TAG_RANGE:
1770 ret_val = cipso_v4_gentag_rng(doi_def,
1771 secattr,
1772 &buf[CIPSO_V4_HDR_LEN],
1773 buf_len - CIPSO_V4_HDR_LEN);
1774 break;
Paul Moore446fda42006-08-03 16:48:06 -07001775 default:
1776 ret_val = -EPERM;
1777 goto socket_setattr_failure;
1778 }
1779
1780 iter++;
Paul Moore91b1ed02006-11-17 17:38:49 -05001781 } while (ret_val < 0 &&
Paul Moore446fda42006-08-03 16:48:06 -07001782 iter < CIPSO_V4_TAG_MAXCNT &&
1783 doi_def->tags[iter] != CIPSO_V4_TAG_INVALID);
Paul Moore91b1ed02006-11-17 17:38:49 -05001784 if (ret_val < 0)
Paul Moore446fda42006-08-03 16:48:06 -07001785 goto socket_setattr_failure;
Paul Moore91b1ed02006-11-17 17:38:49 -05001786 cipso_v4_gentag_hdr(doi_def, buf, ret_val);
1787 buf_len = CIPSO_V4_HDR_LEN + ret_val;
Paul Moore446fda42006-08-03 16:48:06 -07001788
1789 /* We can't use ip_options_get() directly because it makes a call to
1790 * ip_options_get_alloc() which allocates memory with GFP_KERNEL and
Paul Mooref8687af2006-10-30 15:22:15 -08001791 * we won't always have CAP_NET_RAW even though we _always_ want to
1792 * set the IPOPT_CIPSO option. */
Paul Moore446fda42006-08-03 16:48:06 -07001793 opt_len = (buf_len + 3) & ~3;
1794 opt = kzalloc(sizeof(*opt) + opt_len, GFP_ATOMIC);
1795 if (opt == NULL) {
1796 ret_val = -ENOMEM;
1797 goto socket_setattr_failure;
1798 }
1799 memcpy(opt->__data, buf, buf_len);
1800 opt->optlen = opt_len;
Paul Mooref8687af2006-10-30 15:22:15 -08001801 opt->cipso = sizeof(struct iphdr);
Paul Moore446fda42006-08-03 16:48:06 -07001802 kfree(buf);
1803 buf = NULL;
Paul Moore446fda42006-08-03 16:48:06 -07001804
1805 sk_inet = inet_sk(sk);
1806 if (sk_inet->is_icsk) {
1807 sk_conn = inet_csk(sk);
1808 if (sk_inet->opt)
1809 sk_conn->icsk_ext_hdr_len -= sk_inet->opt->optlen;
1810 sk_conn->icsk_ext_hdr_len += opt->optlen;
1811 sk_conn->icsk_sync_mss(sk, sk_conn->icsk_pmtu_cookie);
1812 }
1813 opt = xchg(&sk_inet->opt, opt);
1814 kfree(opt);
1815
1816 return 0;
1817
1818socket_setattr_failure:
1819 kfree(buf);
1820 kfree(opt);
1821 return ret_val;
1822}
1823
1824/**
Paul Moore63d804ea2007-09-15 21:45:13 -07001825 * cipso_v4_getattr - Helper function for the cipso_v4_*_getattr functions
1826 * @cipso: the CIPSO v4 option
1827 * @secattr: the security attributes
1828 *
1829 * Description:
1830 * Inspect @cipso and return the security attributes in @secattr. Returns zero
1831 * on success and negative values on failure.
1832 *
1833 */
1834static int cipso_v4_getattr(const unsigned char *cipso,
1835 struct netlbl_lsm_secattr *secattr)
1836{
1837 int ret_val = -ENOMSG;
1838 u32 doi;
1839 struct cipso_v4_doi *doi_def;
1840
1841 if (cipso_v4_cache_check(cipso, cipso[1], secattr) == 0)
1842 return 0;
1843
Harvey Harrisond3e2ce32008-05-02 16:26:16 -07001844 doi = get_unaligned_be32(&cipso[2]);
Paul Moore63d804ea2007-09-15 21:45:13 -07001845 rcu_read_lock();
1846 doi_def = cipso_v4_doi_search(doi);
1847 if (doi_def == NULL)
1848 goto getattr_return;
1849 /* XXX - This code assumes only one tag per CIPSO option which isn't
1850 * really a good assumption to make but since we only support the MAC
1851 * tags right now it is a safe assumption. */
1852 switch (cipso[6]) {
1853 case CIPSO_V4_TAG_RBITMAP:
1854 ret_val = cipso_v4_parsetag_rbm(doi_def, &cipso[6], secattr);
1855 break;
1856 case CIPSO_V4_TAG_ENUM:
1857 ret_val = cipso_v4_parsetag_enum(doi_def, &cipso[6], secattr);
1858 break;
1859 case CIPSO_V4_TAG_RANGE:
1860 ret_val = cipso_v4_parsetag_rng(doi_def, &cipso[6], secattr);
1861 break;
1862 }
Paul Moore16efd452008-01-29 08:37:59 -05001863 if (ret_val == 0)
1864 secattr->type = NETLBL_NLTYPE_CIPSOV4;
Paul Moore63d804ea2007-09-15 21:45:13 -07001865
1866getattr_return:
1867 rcu_read_unlock();
1868 return ret_val;
1869}
1870
1871/**
Paul Moore14a72f52006-09-25 15:52:01 -07001872 * cipso_v4_sock_getattr - Get the security attributes from a sock
1873 * @sk: the sock
1874 * @secattr: the security attributes
1875 *
1876 * Description:
1877 * Query @sk to see if there is a CIPSO option attached to the sock and if
1878 * there is return the CIPSO security attributes in @secattr. This function
1879 * requires that @sk be locked, or privately held, but it does not do any
1880 * locking itself. Returns zero on success and negative values on failure.
1881 *
1882 */
1883int cipso_v4_sock_getattr(struct sock *sk, struct netlbl_lsm_secattr *secattr)
1884{
Paul Moore63d804ea2007-09-15 21:45:13 -07001885 struct ip_options *opt;
Paul Moore14a72f52006-09-25 15:52:01 -07001886
Paul Moore63d804ea2007-09-15 21:45:13 -07001887 opt = inet_sk(sk)->opt;
1888 if (opt == NULL || opt->cipso == 0)
Paul Moore14a72f52006-09-25 15:52:01 -07001889 return -ENOMSG;
Paul Moore14a72f52006-09-25 15:52:01 -07001890
Paul Moore63d804ea2007-09-15 21:45:13 -07001891 return cipso_v4_getattr(opt->__data + opt->cipso - sizeof(struct iphdr),
1892 secattr);
Paul Moore14a72f52006-09-25 15:52:01 -07001893}
1894
1895/**
Paul Moore446fda42006-08-03 16:48:06 -07001896 * cipso_v4_skbuff_getattr - Get the security attributes from the CIPSO option
1897 * @skb: the packet
1898 * @secattr: the security attributes
1899 *
1900 * Description:
1901 * Parse the given packet's CIPSO option and return the security attributes.
1902 * Returns zero on success and negative values on failure.
1903 *
1904 */
1905int cipso_v4_skbuff_getattr(const struct sk_buff *skb,
1906 struct netlbl_lsm_secattr *secattr)
1907{
Paul Moore63d804ea2007-09-15 21:45:13 -07001908 return cipso_v4_getattr(CIPSO_V4_OPTPTR(skb), secattr);
Paul Moore446fda42006-08-03 16:48:06 -07001909}
1910
1911/*
1912 * Setup Functions
1913 */
1914
1915/**
1916 * cipso_v4_init - Initialize the CIPSO module
1917 *
1918 * Description:
1919 * Initialize the CIPSO module and prepare it for use. Returns zero on success
1920 * and negative values on failure.
1921 *
1922 */
1923static int __init cipso_v4_init(void)
1924{
1925 int ret_val;
1926
1927 ret_val = cipso_v4_cache_init();
1928 if (ret_val != 0)
1929 panic("Failed to initialize the CIPSO/IPv4 cache (%d)\n",
1930 ret_val);
1931
1932 return 0;
1933}
1934
1935subsys_initcall(cipso_v4_init);