blob: e13d6dbb66ab5ad370b5a13037c28cc7135da2cf [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/*
Paul Moore948bf852008-10-10 10:16:32 -040016 * (c) Copyright Hewlett-Packard Development Company, L.P., 2006, 2008
Paul Moore446fda42006-08-03 16:48:06 -070017 *
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
Paul Moore446fda42006-08-03 16:48:06 -070050/* List of available DOI definitions */
Paul Moore446fda42006-08-03 16:48:06 -070051/* XXX - This currently assumes a minimal number of different DOIs in use,
52 * if in practice there are a lot of different DOIs this list should
53 * probably be turned into a hash table or something similar so we
54 * can do quick lookups. */
Adrian Bunk8ce11e62006-08-07 21:50:48 -070055static DEFINE_SPINLOCK(cipso_v4_doi_list_lock);
Denis Cheng1596c972007-12-07 00:49:47 -080056static LIST_HEAD(cipso_v4_doi_list);
Paul Moore446fda42006-08-03 16:48:06 -070057
58/* Label mapping cache */
59int cipso_v4_cache_enabled = 1;
60int cipso_v4_cache_bucketsize = 10;
61#define CIPSO_V4_CACHE_BUCKETBITS 7
62#define CIPSO_V4_CACHE_BUCKETS (1 << CIPSO_V4_CACHE_BUCKETBITS)
63#define CIPSO_V4_CACHE_REORDERLIMIT 10
64struct cipso_v4_map_cache_bkt {
65 spinlock_t lock;
66 u32 size;
67 struct list_head list;
68};
69struct cipso_v4_map_cache_entry {
70 u32 hash;
71 unsigned char *key;
72 size_t key_len;
73
paul.moore@hp.comffb733c2006-10-04 11:46:31 -040074 struct netlbl_lsm_cache *lsm_data;
Paul Moore446fda42006-08-03 16:48:06 -070075
76 u32 activity;
77 struct list_head list;
78};
79static struct cipso_v4_map_cache_bkt *cipso_v4_cache = NULL;
80
81/* Restricted bitmap (tag #1) flags */
82int cipso_v4_rbm_optfmt = 0;
83int cipso_v4_rbm_strictvalid = 1;
84
85/*
Paul Mooref998e8c2007-02-28 15:14:20 -050086 * Protocol Constants
87 */
88
89/* Maximum size of the CIPSO IP option, derived from the fact that the maximum
90 * IPv4 header size is 60 bytes and the base IPv4 header is 20 bytes long. */
91#define CIPSO_V4_OPT_LEN_MAX 40
92
93/* Length of the base CIPSO option, this includes the option type (1 byte), the
94 * option length (1 byte), and the DOI (4 bytes). */
95#define CIPSO_V4_HDR_LEN 6
96
97/* Base length of the restrictive category bitmap tag (tag #1). */
98#define CIPSO_V4_TAG_RBM_BLEN 4
99
100/* Base length of the enumerated category tag (tag #2). */
101#define CIPSO_V4_TAG_ENUM_BLEN 4
102
103/* Base length of the ranged categories bitmap tag (tag #5). */
104#define CIPSO_V4_TAG_RNG_BLEN 4
105/* The maximum number of category ranges permitted in the ranged category tag
106 * (tag #5). You may note that the IETF draft states that the maximum number
107 * of category ranges is 7, but if the low end of the last category range is
108 * zero then it is possibile to fit 8 category ranges because the zero should
109 * be omitted. */
110#define CIPSO_V4_TAG_RNG_CAT_MAX 8
111
112/*
Paul Moore446fda42006-08-03 16:48:06 -0700113 * Helper Functions
114 */
115
116/**
117 * cipso_v4_bitmap_walk - Walk a bitmap looking for a bit
118 * @bitmap: the bitmap
119 * @bitmap_len: length in bits
120 * @offset: starting offset
121 * @state: if non-zero, look for a set (1) bit else look for a cleared (0) bit
122 *
123 * Description:
124 * Starting at @offset, walk the bitmap from left to right until either the
125 * desired bit is found or we reach the end. Return the bit offset, -1 if
126 * not found, or -2 if error.
127 */
128static int cipso_v4_bitmap_walk(const unsigned char *bitmap,
129 u32 bitmap_len,
130 u32 offset,
131 u8 state)
132{
133 u32 bit_spot;
134 u32 byte_offset;
135 unsigned char bitmask;
136 unsigned char byte;
137
138 /* gcc always rounds to zero when doing integer division */
139 byte_offset = offset / 8;
140 byte = bitmap[byte_offset];
141 bit_spot = offset;
142 bitmask = 0x80 >> (offset % 8);
143
144 while (bit_spot < bitmap_len) {
145 if ((state && (byte & bitmask) == bitmask) ||
146 (state == 0 && (byte & bitmask) == 0))
147 return bit_spot;
148
149 bit_spot++;
150 bitmask >>= 1;
151 if (bitmask == 0) {
152 byte = bitmap[++byte_offset];
153 bitmask = 0x80;
154 }
155 }
156
157 return -1;
158}
159
160/**
161 * cipso_v4_bitmap_setbit - Sets a single bit in a bitmap
162 * @bitmap: the bitmap
163 * @bit: the bit
164 * @state: if non-zero, set the bit (1) else clear the bit (0)
165 *
166 * Description:
167 * Set a single bit in the bitmask. Returns zero on success, negative values
168 * on error.
169 */
170static void cipso_v4_bitmap_setbit(unsigned char *bitmap,
171 u32 bit,
172 u8 state)
173{
174 u32 byte_spot;
175 u8 bitmask;
176
177 /* gcc always rounds to zero when doing integer division */
178 byte_spot = bit / 8;
179 bitmask = 0x80 >> (bit % 8);
180 if (state)
181 bitmap[byte_spot] |= bitmask;
182 else
183 bitmap[byte_spot] &= ~bitmask;
184}
185
186/**
Paul Moore446fda42006-08-03 16:48:06 -0700187 * cipso_v4_cache_entry_free - Frees a cache entry
188 * @entry: the entry to free
189 *
190 * Description:
paul.moore@hp.comffb733c2006-10-04 11:46:31 -0400191 * This function frees the memory associated with a cache entry including the
192 * LSM cache data if there are no longer any users, i.e. reference count == 0.
Paul Moore446fda42006-08-03 16:48:06 -0700193 *
194 */
195static void cipso_v4_cache_entry_free(struct cipso_v4_map_cache_entry *entry)
196{
paul.moore@hp.comffb733c2006-10-04 11:46:31 -0400197 if (entry->lsm_data)
198 netlbl_secattr_cache_free(entry->lsm_data);
Paul Moore446fda42006-08-03 16:48:06 -0700199 kfree(entry->key);
200 kfree(entry);
201}
202
203/**
204 * cipso_v4_map_cache_hash - Hashing function for the CIPSO cache
205 * @key: the hash key
206 * @key_len: the length of the key in bytes
207 *
208 * Description:
209 * The CIPSO tag hashing function. Returns a 32-bit hash value.
210 *
211 */
212static u32 cipso_v4_map_cache_hash(const unsigned char *key, u32 key_len)
213{
214 return jhash(key, key_len, 0);
215}
216
217/*
218 * Label Mapping Cache Functions
219 */
220
221/**
222 * cipso_v4_cache_init - Initialize the CIPSO cache
223 *
224 * Description:
225 * Initializes the CIPSO label mapping cache, this function should be called
226 * before any of the other functions defined in this file. Returns zero on
227 * success, negative values on error.
228 *
229 */
230static int cipso_v4_cache_init(void)
231{
232 u32 iter;
233
234 cipso_v4_cache = kcalloc(CIPSO_V4_CACHE_BUCKETS,
235 sizeof(struct cipso_v4_map_cache_bkt),
236 GFP_KERNEL);
237 if (cipso_v4_cache == NULL)
238 return -ENOMEM;
239
240 for (iter = 0; iter < CIPSO_V4_CACHE_BUCKETS; iter++) {
241 spin_lock_init(&cipso_v4_cache[iter].lock);
242 cipso_v4_cache[iter].size = 0;
243 INIT_LIST_HEAD(&cipso_v4_cache[iter].list);
244 }
245
246 return 0;
247}
248
249/**
250 * cipso_v4_cache_invalidate - Invalidates the current CIPSO cache
251 *
252 * Description:
253 * Invalidates and frees any entries in the CIPSO cache. Returns zero on
254 * success and negative values on failure.
255 *
256 */
257void cipso_v4_cache_invalidate(void)
258{
259 struct cipso_v4_map_cache_entry *entry, *tmp_entry;
260 u32 iter;
261
262 for (iter = 0; iter < CIPSO_V4_CACHE_BUCKETS; iter++) {
Paul Moore609c92f2006-09-25 15:52:37 -0700263 spin_lock_bh(&cipso_v4_cache[iter].lock);
Paul Moore446fda42006-08-03 16:48:06 -0700264 list_for_each_entry_safe(entry,
265 tmp_entry,
266 &cipso_v4_cache[iter].list, list) {
267 list_del(&entry->list);
268 cipso_v4_cache_entry_free(entry);
269 }
270 cipso_v4_cache[iter].size = 0;
Paul Moore609c92f2006-09-25 15:52:37 -0700271 spin_unlock_bh(&cipso_v4_cache[iter].lock);
Paul Moore446fda42006-08-03 16:48:06 -0700272 }
273
274 return;
275}
276
277/**
278 * cipso_v4_cache_check - Check the CIPSO cache for a label mapping
279 * @key: the buffer to check
280 * @key_len: buffer length in bytes
281 * @secattr: the security attribute struct to use
282 *
283 * Description:
284 * This function checks the cache to see if a label mapping already exists for
285 * the given key. If there is a match then the cache is adjusted and the
286 * @secattr struct is populated with the correct LSM security attributes. The
287 * cache is adjusted in the following manner if the entry is not already the
288 * first in the cache bucket:
289 *
290 * 1. The cache entry's activity counter is incremented
291 * 2. The previous (higher ranking) entry's activity counter is decremented
292 * 3. If the difference between the two activity counters is geater than
293 * CIPSO_V4_CACHE_REORDERLIMIT the two entries are swapped
294 *
295 * Returns zero on success, -ENOENT for a cache miss, and other negative values
296 * on error.
297 *
298 */
299static int cipso_v4_cache_check(const unsigned char *key,
300 u32 key_len,
301 struct netlbl_lsm_secattr *secattr)
302{
303 u32 bkt;
304 struct cipso_v4_map_cache_entry *entry;
305 struct cipso_v4_map_cache_entry *prev_entry = NULL;
306 u32 hash;
307
308 if (!cipso_v4_cache_enabled)
309 return -ENOENT;
310
311 hash = cipso_v4_map_cache_hash(key, key_len);
Pavel Emelyanov5e0f8922008-05-13 23:23:55 -0700312 bkt = hash & (CIPSO_V4_CACHE_BUCKETS - 1);
Paul Moore609c92f2006-09-25 15:52:37 -0700313 spin_lock_bh(&cipso_v4_cache[bkt].lock);
Paul Moore446fda42006-08-03 16:48:06 -0700314 list_for_each_entry(entry, &cipso_v4_cache[bkt].list, list) {
315 if (entry->hash == hash &&
316 entry->key_len == key_len &&
317 memcmp(entry->key, key, key_len) == 0) {
318 entry->activity += 1;
paul.moore@hp.comffb733c2006-10-04 11:46:31 -0400319 atomic_inc(&entry->lsm_data->refcount);
320 secattr->cache = entry->lsm_data;
Paul Moore701a90b2006-11-17 17:38:46 -0500321 secattr->flags |= NETLBL_SECATTR_CACHE;
Paul Moore16efd452008-01-29 08:37:59 -0500322 secattr->type = NETLBL_NLTYPE_CIPSOV4;
Paul Moore446fda42006-08-03 16:48:06 -0700323 if (prev_entry == NULL) {
Paul Moore609c92f2006-09-25 15:52:37 -0700324 spin_unlock_bh(&cipso_v4_cache[bkt].lock);
Paul Moore446fda42006-08-03 16:48:06 -0700325 return 0;
326 }
327
328 if (prev_entry->activity > 0)
329 prev_entry->activity -= 1;
330 if (entry->activity > prev_entry->activity &&
331 entry->activity - prev_entry->activity >
332 CIPSO_V4_CACHE_REORDERLIMIT) {
333 __list_del(entry->list.prev, entry->list.next);
334 __list_add(&entry->list,
335 prev_entry->list.prev,
336 &prev_entry->list);
337 }
338
Paul Moore609c92f2006-09-25 15:52:37 -0700339 spin_unlock_bh(&cipso_v4_cache[bkt].lock);
Paul Moore446fda42006-08-03 16:48:06 -0700340 return 0;
341 }
342 prev_entry = entry;
343 }
Paul Moore609c92f2006-09-25 15:52:37 -0700344 spin_unlock_bh(&cipso_v4_cache[bkt].lock);
Paul Moore446fda42006-08-03 16:48:06 -0700345
346 return -ENOENT;
347}
348
349/**
350 * cipso_v4_cache_add - Add an entry to the CIPSO cache
351 * @skb: the packet
352 * @secattr: the packet's security attributes
353 *
354 * Description:
355 * Add a new entry into the CIPSO label mapping cache. Add the new entry to
356 * head of the cache bucket's list, if the cache bucket is out of room remove
357 * the last entry in the list first. It is important to note that there is
358 * currently no checking for duplicate keys. Returns zero on success,
359 * negative values on failure.
360 *
361 */
362int cipso_v4_cache_add(const struct sk_buff *skb,
363 const struct netlbl_lsm_secattr *secattr)
364{
365 int ret_val = -EPERM;
366 u32 bkt;
367 struct cipso_v4_map_cache_entry *entry = NULL;
368 struct cipso_v4_map_cache_entry *old_entry = NULL;
369 unsigned char *cipso_ptr;
370 u32 cipso_ptr_len;
371
372 if (!cipso_v4_cache_enabled || cipso_v4_cache_bucketsize <= 0)
373 return 0;
374
375 cipso_ptr = CIPSO_V4_OPTPTR(skb);
376 cipso_ptr_len = cipso_ptr[1];
377
378 entry = kzalloc(sizeof(*entry), GFP_ATOMIC);
379 if (entry == NULL)
380 return -ENOMEM;
Arnaldo Carvalho de Melofac5d732006-11-17 11:14:16 -0200381 entry->key = kmemdup(cipso_ptr, cipso_ptr_len, GFP_ATOMIC);
Paul Moore446fda42006-08-03 16:48:06 -0700382 if (entry->key == NULL) {
383 ret_val = -ENOMEM;
384 goto cache_add_failure;
385 }
Paul Moore446fda42006-08-03 16:48:06 -0700386 entry->key_len = cipso_ptr_len;
387 entry->hash = cipso_v4_map_cache_hash(cipso_ptr, cipso_ptr_len);
paul.moore@hp.comffb733c2006-10-04 11:46:31 -0400388 atomic_inc(&secattr->cache->refcount);
389 entry->lsm_data = secattr->cache;
Paul Moore446fda42006-08-03 16:48:06 -0700390
Pavel Emelyanov5e0f8922008-05-13 23:23:55 -0700391 bkt = entry->hash & (CIPSO_V4_CACHE_BUCKETS - 1);
Paul Moore609c92f2006-09-25 15:52:37 -0700392 spin_lock_bh(&cipso_v4_cache[bkt].lock);
Paul Moore446fda42006-08-03 16:48:06 -0700393 if (cipso_v4_cache[bkt].size < cipso_v4_cache_bucketsize) {
394 list_add(&entry->list, &cipso_v4_cache[bkt].list);
395 cipso_v4_cache[bkt].size += 1;
396 } else {
397 old_entry = list_entry(cipso_v4_cache[bkt].list.prev,
398 struct cipso_v4_map_cache_entry, list);
399 list_del(&old_entry->list);
400 list_add(&entry->list, &cipso_v4_cache[bkt].list);
401 cipso_v4_cache_entry_free(old_entry);
402 }
Paul Moore609c92f2006-09-25 15:52:37 -0700403 spin_unlock_bh(&cipso_v4_cache[bkt].lock);
Paul Moore446fda42006-08-03 16:48:06 -0700404
405 return 0;
406
407cache_add_failure:
408 if (entry)
409 cipso_v4_cache_entry_free(entry);
410 return ret_val;
411}
412
413/*
414 * DOI List Functions
415 */
416
417/**
418 * cipso_v4_doi_search - Searches for a DOI definition
419 * @doi: the DOI to search for
420 *
421 * Description:
422 * Search the DOI definition list for a DOI definition with a DOI value that
423 * matches @doi. The caller is responsibile for calling rcu_read_[un]lock().
424 * Returns a pointer to the DOI definition on success and NULL on failure.
425 */
426static struct cipso_v4_doi *cipso_v4_doi_search(u32 doi)
427{
428 struct cipso_v4_doi *iter;
429
430 list_for_each_entry_rcu(iter, &cipso_v4_doi_list, list)
Paul Mooreb1edeb12008-10-10 10:16:31 -0400431 if (iter->doi == doi && atomic_read(&iter->refcount))
Paul Moore446fda42006-08-03 16:48:06 -0700432 return iter;
433 return NULL;
434}
435
436/**
437 * cipso_v4_doi_add - Add a new DOI to the CIPSO protocol engine
438 * @doi_def: the DOI structure
439 *
440 * Description:
441 * The caller defines a new DOI for use by the CIPSO engine and calls this
442 * function to add it to the list of acceptable domains. The caller must
443 * ensure that the mapping table specified in @doi_def->map meets all of the
444 * requirements of the mapping type (see cipso_ipv4.h for details). Returns
445 * zero on success and non-zero on failure.
446 *
447 */
448int cipso_v4_doi_add(struct cipso_v4_doi *doi_def)
449{
Paul Moore6ce61a72006-11-17 17:38:48 -0500450 u32 iter;
451
Paul Moore446fda42006-08-03 16:48:06 -0700452 if (doi_def == NULL || doi_def->doi == CIPSO_V4_DOI_UNKNOWN)
453 return -EINVAL;
Paul Moore6ce61a72006-11-17 17:38:48 -0500454 for (iter = 0; iter < CIPSO_V4_TAG_MAXCNT; iter++) {
455 switch (doi_def->tags[iter]) {
456 case CIPSO_V4_TAG_RBITMAP:
457 break;
Paul Moore484b3662006-11-29 13:18:20 -0500458 case CIPSO_V4_TAG_RANGE:
459 if (doi_def->type != CIPSO_V4_MAP_PASS)
460 return -EINVAL;
461 break;
Paul Moore6ce61a72006-11-17 17:38:48 -0500462 case CIPSO_V4_TAG_INVALID:
463 if (iter == 0)
464 return -EINVAL;
465 break;
Paul Moore654bbc22006-11-29 13:18:19 -0500466 case CIPSO_V4_TAG_ENUM:
467 if (doi_def->type != CIPSO_V4_MAP_PASS)
468 return -EINVAL;
469 break;
Paul Moore6ce61a72006-11-17 17:38:48 -0500470 default:
471 return -EINVAL;
472 }
473 }
Paul Moore446fda42006-08-03 16:48:06 -0700474
Paul Mooreb1edeb12008-10-10 10:16:31 -0400475 atomic_set(&doi_def->refcount, 1);
Paul Moore446fda42006-08-03 16:48:06 -0700476 INIT_RCU_HEAD(&doi_def->rcu);
Paul Moore446fda42006-08-03 16:48:06 -0700477
Paul Moore446fda42006-08-03 16:48:06 -0700478 spin_lock(&cipso_v4_doi_list_lock);
479 if (cipso_v4_doi_search(doi_def->doi) != NULL)
Paul Moore4be27002007-10-26 04:29:08 -0700480 goto doi_add_failure;
Paul Moore446fda42006-08-03 16:48:06 -0700481 list_add_tail_rcu(&doi_def->list, &cipso_v4_doi_list);
482 spin_unlock(&cipso_v4_doi_list_lock);
Paul Moore446fda42006-08-03 16:48:06 -0700483
484 return 0;
485
Paul Moore4be27002007-10-26 04:29:08 -0700486doi_add_failure:
Paul Moore446fda42006-08-03 16:48:06 -0700487 spin_unlock(&cipso_v4_doi_list_lock);
Paul Moore446fda42006-08-03 16:48:06 -0700488 return -EEXIST;
489}
490
491/**
Paul Mooreb1edeb12008-10-10 10:16:31 -0400492 * cipso_v4_doi_free - Frees a DOI definition
493 * @entry: the entry's RCU field
Paul Moore446fda42006-08-03 16:48:06 -0700494 *
495 * Description:
Paul Mooreb1edeb12008-10-10 10:16:31 -0400496 * This function frees all of the memory associated with a DOI definition.
Paul Moore446fda42006-08-03 16:48:06 -0700497 *
498 */
Paul Mooreb1edeb12008-10-10 10:16:31 -0400499void cipso_v4_doi_free(struct cipso_v4_doi *doi_def)
Paul Moore446fda42006-08-03 16:48:06 -0700500{
Paul Mooreb1edeb12008-10-10 10:16:31 -0400501 if (doi_def == NULL)
502 return;
Paul Moore446fda42006-08-03 16:48:06 -0700503
Paul Mooreb1edeb12008-10-10 10:16:31 -0400504 switch (doi_def->type) {
505 case CIPSO_V4_MAP_STD:
506 kfree(doi_def->map.std->lvl.cipso);
507 kfree(doi_def->map.std->lvl.local);
508 kfree(doi_def->map.std->cat.cipso);
509 kfree(doi_def->map.std->cat.local);
510 break;
Paul Moore446fda42006-08-03 16:48:06 -0700511 }
Paul Mooreb1edeb12008-10-10 10:16:31 -0400512 kfree(doi_def);
Paul Moore446fda42006-08-03 16:48:06 -0700513}
514
515/**
Paul Mooreb1edeb12008-10-10 10:16:31 -0400516 * cipso_v4_doi_free_rcu - Frees a DOI definition via the RCU pointer
517 * @entry: the entry's RCU field
518 *
519 * Description:
520 * This function is designed to be used as a callback to the call_rcu()
521 * function so that the memory allocated to the DOI definition can be released
522 * safely.
523 *
524 */
525static void cipso_v4_doi_free_rcu(struct rcu_head *entry)
526{
527 struct cipso_v4_doi *doi_def;
528
529 doi_def = container_of(entry, struct cipso_v4_doi, rcu);
530 cipso_v4_doi_free(doi_def);
531}
532
533/**
534 * cipso_v4_doi_remove - Remove an existing DOI from the CIPSO protocol engine
535 * @doi: the DOI value
536 * @audit_secid: the LSM secid to use in the audit message
537 *
538 * Description:
539 * Removes a DOI definition from the CIPSO engine. The NetLabel routines will
540 * be called to release their own LSM domain mappings as well as our own
541 * domain list. Returns zero on success and negative values on failure.
542 *
543 */
544int cipso_v4_doi_remove(u32 doi, struct netlbl_audit *audit_info)
545{
546 struct cipso_v4_doi *doi_def;
547
548 spin_lock(&cipso_v4_doi_list_lock);
549 doi_def = cipso_v4_doi_search(doi);
550 if (doi_def == NULL) {
551 spin_unlock(&cipso_v4_doi_list_lock);
552 return -ENOENT;
553 }
554 if (!atomic_dec_and_test(&doi_def->refcount)) {
555 spin_unlock(&cipso_v4_doi_list_lock);
556 return -EBUSY;
557 }
558 list_del_rcu(&doi_def->list);
559 spin_unlock(&cipso_v4_doi_list_lock);
560
561 cipso_v4_cache_invalidate();
562 call_rcu(&doi_def->rcu, cipso_v4_doi_free_rcu);
563
564 return 0;
565}
566
567/**
568 * cipso_v4_doi_getdef - Returns a reference to a valid DOI definition
Paul Moore446fda42006-08-03 16:48:06 -0700569 * @doi: the DOI value
570 *
571 * Description:
572 * Searches for a valid DOI definition and if one is found it is returned to
573 * the caller. Otherwise NULL is returned. The caller must ensure that
Paul Mooreb1edeb12008-10-10 10:16:31 -0400574 * rcu_read_lock() is held while accessing the returned definition and the DOI
575 * definition reference count is decremented when the caller is done.
Paul Moore446fda42006-08-03 16:48:06 -0700576 *
577 */
578struct cipso_v4_doi *cipso_v4_doi_getdef(u32 doi)
579{
Paul Mooreb1edeb12008-10-10 10:16:31 -0400580 struct cipso_v4_doi *doi_def;
581
582 rcu_read_lock();
583 doi_def = cipso_v4_doi_search(doi);
584 if (doi_def == NULL)
585 goto doi_getdef_return;
586 if (!atomic_inc_not_zero(&doi_def->refcount))
587 doi_def = NULL;
588
589doi_getdef_return:
590 rcu_read_unlock();
591 return doi_def;
592}
593
594/**
595 * cipso_v4_doi_putdef - Releases a reference for the given DOI definition
596 * @doi_def: the DOI definition
597 *
598 * Description:
599 * Releases a DOI definition reference obtained from cipso_v4_doi_getdef().
600 *
601 */
602void cipso_v4_doi_putdef(struct cipso_v4_doi *doi_def)
603{
604 if (doi_def == NULL)
605 return;
606
607 if (!atomic_dec_and_test(&doi_def->refcount))
608 return;
609 spin_lock(&cipso_v4_doi_list_lock);
610 list_del_rcu(&doi_def->list);
611 spin_unlock(&cipso_v4_doi_list_lock);
612
613 cipso_v4_cache_invalidate();
614 call_rcu(&doi_def->rcu, cipso_v4_doi_free_rcu);
Paul Moore446fda42006-08-03 16:48:06 -0700615}
616
617/**
Paul Moorefcd48282006-09-25 15:56:09 -0700618 * cipso_v4_doi_walk - Iterate through the DOI definitions
619 * @skip_cnt: skip past this number of DOI definitions, updated
620 * @callback: callback for each DOI definition
621 * @cb_arg: argument for the callback function
Paul Moore446fda42006-08-03 16:48:06 -0700622 *
623 * Description:
Paul Moorefcd48282006-09-25 15:56:09 -0700624 * Iterate over the DOI definition list, skipping the first @skip_cnt entries.
625 * For each entry call @callback, if @callback returns a negative value stop
626 * 'walking' through the list and return. Updates the value in @skip_cnt upon
627 * return. Returns zero on success, negative values on failure.
Paul Moore446fda42006-08-03 16:48:06 -0700628 *
629 */
Paul Moorefcd48282006-09-25 15:56:09 -0700630int cipso_v4_doi_walk(u32 *skip_cnt,
631 int (*callback) (struct cipso_v4_doi *doi_def, void *arg),
632 void *cb_arg)
Paul Moore446fda42006-08-03 16:48:06 -0700633{
Paul Moorefcd48282006-09-25 15:56:09 -0700634 int ret_val = -ENOENT;
Paul Moore446fda42006-08-03 16:48:06 -0700635 u32 doi_cnt = 0;
Paul Moorefcd48282006-09-25 15:56:09 -0700636 struct cipso_v4_doi *iter_doi;
Paul Moore446fda42006-08-03 16:48:06 -0700637
638 rcu_read_lock();
Paul Moorefcd48282006-09-25 15:56:09 -0700639 list_for_each_entry_rcu(iter_doi, &cipso_v4_doi_list, list)
Paul Mooreb1edeb12008-10-10 10:16:31 -0400640 if (atomic_read(&iter_doi->refcount) > 0) {
Paul Moorefcd48282006-09-25 15:56:09 -0700641 if (doi_cnt++ < *skip_cnt)
642 continue;
643 ret_val = callback(iter_doi, cb_arg);
644 if (ret_val < 0) {
645 doi_cnt--;
646 goto doi_walk_return;
Paul Moore446fda42006-08-03 16:48:06 -0700647 }
Paul Moorefcd48282006-09-25 15:56:09 -0700648 }
Paul Moore446fda42006-08-03 16:48:06 -0700649
Paul Moorefcd48282006-09-25 15:56:09 -0700650doi_walk_return:
Paul Moore446fda42006-08-03 16:48:06 -0700651 rcu_read_unlock();
Paul Moorefcd48282006-09-25 15:56:09 -0700652 *skip_cnt = doi_cnt;
653 return ret_val;
Paul Moore446fda42006-08-03 16:48:06 -0700654}
655
Paul Moore446fda42006-08-03 16:48:06 -0700656/*
657 * Label Mapping Functions
658 */
659
660/**
661 * cipso_v4_map_lvl_valid - Checks to see if the given level is understood
662 * @doi_def: the DOI definition
663 * @level: the level to check
664 *
665 * Description:
666 * Checks the given level against the given DOI definition and returns a
667 * negative value if the level does not have a valid mapping and a zero value
668 * if the level is defined by the DOI.
669 *
670 */
671static int cipso_v4_map_lvl_valid(const struct cipso_v4_doi *doi_def, u8 level)
672{
673 switch (doi_def->type) {
674 case CIPSO_V4_MAP_PASS:
675 return 0;
676 case CIPSO_V4_MAP_STD:
677 if (doi_def->map.std->lvl.cipso[level] < CIPSO_V4_INV_LVL)
678 return 0;
679 break;
680 }
681
682 return -EFAULT;
683}
684
685/**
686 * cipso_v4_map_lvl_hton - Perform a level mapping from the host to the network
687 * @doi_def: the DOI definition
688 * @host_lvl: the host MLS level
689 * @net_lvl: the network/CIPSO MLS level
690 *
691 * Description:
692 * Perform a label mapping to translate a local MLS level to the correct
693 * CIPSO level using the given DOI definition. Returns zero on success,
694 * negative values otherwise.
695 *
696 */
697static int cipso_v4_map_lvl_hton(const struct cipso_v4_doi *doi_def,
698 u32 host_lvl,
699 u32 *net_lvl)
700{
701 switch (doi_def->type) {
702 case CIPSO_V4_MAP_PASS:
703 *net_lvl = host_lvl;
704 return 0;
705 case CIPSO_V4_MAP_STD:
Paul Moorec6387a82007-03-02 13:19:02 -0800706 if (host_lvl < doi_def->map.std->lvl.local_size &&
707 doi_def->map.std->lvl.local[host_lvl] < CIPSO_V4_INV_LVL) {
Paul Moore446fda42006-08-03 16:48:06 -0700708 *net_lvl = doi_def->map.std->lvl.local[host_lvl];
709 return 0;
710 }
Paul Moorec6387a82007-03-02 13:19:02 -0800711 return -EPERM;
Paul Moore446fda42006-08-03 16:48:06 -0700712 }
713
714 return -EINVAL;
715}
716
717/**
718 * cipso_v4_map_lvl_ntoh - Perform a level mapping from the network to the host
719 * @doi_def: the DOI definition
720 * @net_lvl: the network/CIPSO MLS level
721 * @host_lvl: the host MLS level
722 *
723 * Description:
724 * Perform a label mapping to translate a CIPSO level to the correct local MLS
725 * level using the given DOI definition. Returns zero on success, negative
726 * values otherwise.
727 *
728 */
729static int cipso_v4_map_lvl_ntoh(const struct cipso_v4_doi *doi_def,
730 u32 net_lvl,
731 u32 *host_lvl)
732{
733 struct cipso_v4_std_map_tbl *map_tbl;
734
735 switch (doi_def->type) {
736 case CIPSO_V4_MAP_PASS:
737 *host_lvl = net_lvl;
738 return 0;
739 case CIPSO_V4_MAP_STD:
740 map_tbl = doi_def->map.std;
741 if (net_lvl < map_tbl->lvl.cipso_size &&
742 map_tbl->lvl.cipso[net_lvl] < CIPSO_V4_INV_LVL) {
743 *host_lvl = doi_def->map.std->lvl.cipso[net_lvl];
744 return 0;
745 }
Paul Moorec6387a82007-03-02 13:19:02 -0800746 return -EPERM;
Paul Moore446fda42006-08-03 16:48:06 -0700747 }
748
749 return -EINVAL;
750}
751
752/**
753 * cipso_v4_map_cat_rbm_valid - Checks to see if the category bitmap is valid
754 * @doi_def: the DOI definition
755 * @bitmap: category bitmap
756 * @bitmap_len: bitmap length in bytes
757 *
758 * Description:
759 * Checks the given category bitmap against the given DOI definition and
760 * returns a negative value if any of the categories in the bitmap do not have
761 * a valid mapping and a zero value if all of the categories are valid.
762 *
763 */
764static int cipso_v4_map_cat_rbm_valid(const struct cipso_v4_doi *doi_def,
765 const unsigned char *bitmap,
766 u32 bitmap_len)
767{
768 int cat = -1;
769 u32 bitmap_len_bits = bitmap_len * 8;
Paul Moore044a68e2006-10-11 19:10:47 -0400770 u32 cipso_cat_size;
771 u32 *cipso_array;
Paul Moore446fda42006-08-03 16:48:06 -0700772
773 switch (doi_def->type) {
774 case CIPSO_V4_MAP_PASS:
775 return 0;
776 case CIPSO_V4_MAP_STD:
Paul Moore044a68e2006-10-11 19:10:47 -0400777 cipso_cat_size = doi_def->map.std->cat.cipso_size;
778 cipso_array = doi_def->map.std->cat.cipso;
Paul Moore446fda42006-08-03 16:48:06 -0700779 for (;;) {
780 cat = cipso_v4_bitmap_walk(bitmap,
781 bitmap_len_bits,
782 cat + 1,
783 1);
784 if (cat < 0)
785 break;
786 if (cat >= cipso_cat_size ||
787 cipso_array[cat] >= CIPSO_V4_INV_CAT)
788 return -EFAULT;
789 }
790
791 if (cat == -1)
792 return 0;
793 break;
794 }
795
796 return -EFAULT;
797}
798
799/**
800 * cipso_v4_map_cat_rbm_hton - Perform a category mapping from host to network
801 * @doi_def: the DOI definition
Paul Moore02752762006-11-29 13:18:18 -0500802 * @secattr: the security attributes
Paul Moore446fda42006-08-03 16:48:06 -0700803 * @net_cat: the zero'd out category bitmap in network/CIPSO format
804 * @net_cat_len: the length of the CIPSO bitmap in bytes
805 *
806 * Description:
807 * Perform a label mapping to translate a local MLS category bitmap to the
808 * correct CIPSO bitmap using the given DOI definition. Returns the minimum
809 * size in bytes of the network bitmap on success, negative values otherwise.
810 *
811 */
812static int cipso_v4_map_cat_rbm_hton(const struct cipso_v4_doi *doi_def,
Paul Moore02752762006-11-29 13:18:18 -0500813 const struct netlbl_lsm_secattr *secattr,
Paul Moore446fda42006-08-03 16:48:06 -0700814 unsigned char *net_cat,
815 u32 net_cat_len)
816{
817 int host_spot = -1;
Paul Moore02752762006-11-29 13:18:18 -0500818 u32 net_spot = CIPSO_V4_INV_CAT;
Paul Moore446fda42006-08-03 16:48:06 -0700819 u32 net_spot_max = 0;
Paul Moore446fda42006-08-03 16:48:06 -0700820 u32 net_clen_bits = net_cat_len * 8;
Paul Moore02752762006-11-29 13:18:18 -0500821 u32 host_cat_size = 0;
822 u32 *host_cat_array = NULL;
Paul Moore446fda42006-08-03 16:48:06 -0700823
Paul Moore02752762006-11-29 13:18:18 -0500824 if (doi_def->type == CIPSO_V4_MAP_STD) {
Paul Moore044a68e2006-10-11 19:10:47 -0400825 host_cat_size = doi_def->map.std->cat.local_size;
826 host_cat_array = doi_def->map.std->cat.local;
Paul Moore02752762006-11-29 13:18:18 -0500827 }
828
829 for (;;) {
Paul Moore16efd452008-01-29 08:37:59 -0500830 host_spot = netlbl_secattr_catmap_walk(secattr->attr.mls.cat,
Paul Moore02752762006-11-29 13:18:18 -0500831 host_spot + 1);
832 if (host_spot < 0)
833 break;
834
835 switch (doi_def->type) {
836 case CIPSO_V4_MAP_PASS:
837 net_spot = host_spot;
838 break;
839 case CIPSO_V4_MAP_STD:
Paul Moore446fda42006-08-03 16:48:06 -0700840 if (host_spot >= host_cat_size)
841 return -EPERM;
Paul Moore446fda42006-08-03 16:48:06 -0700842 net_spot = host_cat_array[host_spot];
Paul Moore9fade4b2006-11-17 17:38:50 -0500843 if (net_spot >= CIPSO_V4_INV_CAT)
844 return -EPERM;
Paul Moore02752762006-11-29 13:18:18 -0500845 break;
Paul Moore446fda42006-08-03 16:48:06 -0700846 }
Paul Moore02752762006-11-29 13:18:18 -0500847 if (net_spot >= net_clen_bits)
848 return -ENOSPC;
849 cipso_v4_bitmap_setbit(net_cat, net_spot, 1);
Paul Moore446fda42006-08-03 16:48:06 -0700850
Paul Moore02752762006-11-29 13:18:18 -0500851 if (net_spot > net_spot_max)
852 net_spot_max = net_spot;
Paul Moore446fda42006-08-03 16:48:06 -0700853 }
854
Paul Moore02752762006-11-29 13:18:18 -0500855 if (++net_spot_max % 8)
856 return net_spot_max / 8 + 1;
857 return net_spot_max / 8;
Paul Moore446fda42006-08-03 16:48:06 -0700858}
859
860/**
861 * cipso_v4_map_cat_rbm_ntoh - Perform a category mapping from network to host
862 * @doi_def: the DOI definition
863 * @net_cat: the category bitmap in network/CIPSO format
864 * @net_cat_len: the length of the CIPSO bitmap in bytes
Paul Moore02752762006-11-29 13:18:18 -0500865 * @secattr: the security attributes
Paul Moore446fda42006-08-03 16:48:06 -0700866 *
867 * Description:
868 * Perform a label mapping to translate a CIPSO bitmap to the correct local
Paul Moore02752762006-11-29 13:18:18 -0500869 * MLS category bitmap using the given DOI definition. Returns zero on
870 * success, negative values on failure.
Paul Moore446fda42006-08-03 16:48:06 -0700871 *
872 */
873static int cipso_v4_map_cat_rbm_ntoh(const struct cipso_v4_doi *doi_def,
874 const unsigned char *net_cat,
875 u32 net_cat_len,
Paul Moore02752762006-11-29 13:18:18 -0500876 struct netlbl_lsm_secattr *secattr)
Paul Moore446fda42006-08-03 16:48:06 -0700877{
Paul Moore02752762006-11-29 13:18:18 -0500878 int ret_val;
Paul Moore446fda42006-08-03 16:48:06 -0700879 int net_spot = -1;
Paul Moore02752762006-11-29 13:18:18 -0500880 u32 host_spot = CIPSO_V4_INV_CAT;
Paul Moore446fda42006-08-03 16:48:06 -0700881 u32 net_clen_bits = net_cat_len * 8;
Paul Moore02752762006-11-29 13:18:18 -0500882 u32 net_cat_size = 0;
883 u32 *net_cat_array = NULL;
Paul Moore446fda42006-08-03 16:48:06 -0700884
Paul Moore02752762006-11-29 13:18:18 -0500885 if (doi_def->type == CIPSO_V4_MAP_STD) {
Paul Moore044a68e2006-10-11 19:10:47 -0400886 net_cat_size = doi_def->map.std->cat.cipso_size;
887 net_cat_array = doi_def->map.std->cat.cipso;
Paul Moore02752762006-11-29 13:18:18 -0500888 }
Paul Moore446fda42006-08-03 16:48:06 -0700889
Paul Moore02752762006-11-29 13:18:18 -0500890 for (;;) {
891 net_spot = cipso_v4_bitmap_walk(net_cat,
892 net_clen_bits,
893 net_spot + 1,
894 1);
895 if (net_spot < 0) {
896 if (net_spot == -2)
897 return -EFAULT;
898 return 0;
899 }
900
901 switch (doi_def->type) {
902 case CIPSO_V4_MAP_PASS:
903 host_spot = net_spot;
904 break;
905 case CIPSO_V4_MAP_STD:
906 if (net_spot >= net_cat_size)
907 return -EPERM;
Paul Moore446fda42006-08-03 16:48:06 -0700908 host_spot = net_cat_array[net_spot];
Paul Moore9fade4b2006-11-17 17:38:50 -0500909 if (host_spot >= CIPSO_V4_INV_CAT)
910 return -EPERM;
Paul Moore02752762006-11-29 13:18:18 -0500911 break;
Paul Moore446fda42006-08-03 16:48:06 -0700912 }
Paul Moore16efd452008-01-29 08:37:59 -0500913 ret_val = netlbl_secattr_catmap_setbit(secattr->attr.mls.cat,
Paul Moore02752762006-11-29 13:18:18 -0500914 host_spot,
915 GFP_ATOMIC);
916 if (ret_val != 0)
917 return ret_val;
Paul Moore446fda42006-08-03 16:48:06 -0700918 }
919
920 return -EINVAL;
921}
922
Paul Moore654bbc22006-11-29 13:18:19 -0500923/**
924 * cipso_v4_map_cat_enum_valid - Checks to see if the categories are valid
925 * @doi_def: the DOI definition
926 * @enumcat: category list
927 * @enumcat_len: length of the category list in bytes
928 *
929 * Description:
930 * Checks the given categories against the given DOI definition and returns a
931 * negative value if any of the categories do not have a valid mapping and a
932 * zero value if all of the categories are valid.
933 *
934 */
935static int cipso_v4_map_cat_enum_valid(const struct cipso_v4_doi *doi_def,
936 const unsigned char *enumcat,
937 u32 enumcat_len)
938{
939 u16 cat;
940 int cat_prev = -1;
941 u32 iter;
942
943 if (doi_def->type != CIPSO_V4_MAP_PASS || enumcat_len & 0x01)
944 return -EFAULT;
945
946 for (iter = 0; iter < enumcat_len; iter += 2) {
Harvey Harrisond3e2ce32008-05-02 16:26:16 -0700947 cat = get_unaligned_be16(&enumcat[iter]);
Paul Moore654bbc22006-11-29 13:18:19 -0500948 if (cat <= cat_prev)
949 return -EFAULT;
950 cat_prev = cat;
951 }
952
953 return 0;
954}
955
956/**
957 * cipso_v4_map_cat_enum_hton - Perform a category mapping from host to network
958 * @doi_def: the DOI definition
959 * @secattr: the security attributes
960 * @net_cat: the zero'd out category list in network/CIPSO format
961 * @net_cat_len: the length of the CIPSO category list in bytes
962 *
963 * Description:
964 * Perform a label mapping to translate a local MLS category bitmap to the
965 * correct CIPSO category list using the given DOI definition. Returns the
966 * size in bytes of the network category bitmap on success, negative values
967 * otherwise.
968 *
969 */
970static int cipso_v4_map_cat_enum_hton(const struct cipso_v4_doi *doi_def,
971 const struct netlbl_lsm_secattr *secattr,
972 unsigned char *net_cat,
973 u32 net_cat_len)
974{
975 int cat = -1;
976 u32 cat_iter = 0;
977
978 for (;;) {
Paul Moore16efd452008-01-29 08:37:59 -0500979 cat = netlbl_secattr_catmap_walk(secattr->attr.mls.cat,
980 cat + 1);
Paul Moore654bbc22006-11-29 13:18:19 -0500981 if (cat < 0)
982 break;
983 if ((cat_iter + 2) > net_cat_len)
984 return -ENOSPC;
985
986 *((__be16 *)&net_cat[cat_iter]) = htons(cat);
987 cat_iter += 2;
988 }
989
990 return cat_iter;
991}
992
993/**
994 * cipso_v4_map_cat_enum_ntoh - Perform a category mapping from network to host
995 * @doi_def: the DOI definition
996 * @net_cat: the category list in network/CIPSO format
997 * @net_cat_len: the length of the CIPSO bitmap in bytes
998 * @secattr: the security attributes
999 *
1000 * Description:
1001 * Perform a label mapping to translate a CIPSO category list to the correct
1002 * local MLS category bitmap using the given DOI definition. Returns zero on
1003 * success, negative values on failure.
1004 *
1005 */
1006static int cipso_v4_map_cat_enum_ntoh(const struct cipso_v4_doi *doi_def,
1007 const unsigned char *net_cat,
1008 u32 net_cat_len,
1009 struct netlbl_lsm_secattr *secattr)
1010{
1011 int ret_val;
1012 u32 iter;
1013
1014 for (iter = 0; iter < net_cat_len; iter += 2) {
Paul Moore16efd452008-01-29 08:37:59 -05001015 ret_val = netlbl_secattr_catmap_setbit(secattr->attr.mls.cat,
Harvey Harrisond3e2ce32008-05-02 16:26:16 -07001016 get_unaligned_be16(&net_cat[iter]),
Paul Moore50e5d352007-06-07 18:38:14 -07001017 GFP_ATOMIC);
Paul Moore654bbc22006-11-29 13:18:19 -05001018 if (ret_val != 0)
1019 return ret_val;
1020 }
1021
1022 return 0;
1023}
1024
Paul Moore484b3662006-11-29 13:18:20 -05001025/**
1026 * cipso_v4_map_cat_rng_valid - Checks to see if the categories are valid
1027 * @doi_def: the DOI definition
1028 * @rngcat: category list
1029 * @rngcat_len: length of the category list in bytes
1030 *
1031 * Description:
1032 * Checks the given categories against the given DOI definition and returns a
1033 * negative value if any of the categories do not have a valid mapping and a
1034 * zero value if all of the categories are valid.
1035 *
1036 */
1037static int cipso_v4_map_cat_rng_valid(const struct cipso_v4_doi *doi_def,
1038 const unsigned char *rngcat,
1039 u32 rngcat_len)
1040{
1041 u16 cat_high;
1042 u16 cat_low;
1043 u32 cat_prev = CIPSO_V4_MAX_REM_CATS + 1;
1044 u32 iter;
1045
1046 if (doi_def->type != CIPSO_V4_MAP_PASS || rngcat_len & 0x01)
1047 return -EFAULT;
1048
1049 for (iter = 0; iter < rngcat_len; iter += 4) {
Harvey Harrisond3e2ce32008-05-02 16:26:16 -07001050 cat_high = get_unaligned_be16(&rngcat[iter]);
Paul Moore484b3662006-11-29 13:18:20 -05001051 if ((iter + 4) <= rngcat_len)
Harvey Harrisond3e2ce32008-05-02 16:26:16 -07001052 cat_low = get_unaligned_be16(&rngcat[iter + 2]);
Paul Moore484b3662006-11-29 13:18:20 -05001053 else
1054 cat_low = 0;
1055
1056 if (cat_high > cat_prev)
1057 return -EFAULT;
1058
1059 cat_prev = cat_low;
1060 }
1061
1062 return 0;
1063}
1064
1065/**
1066 * cipso_v4_map_cat_rng_hton - Perform a category mapping from host to network
1067 * @doi_def: the DOI definition
1068 * @secattr: the security attributes
1069 * @net_cat: the zero'd out category list in network/CIPSO format
1070 * @net_cat_len: the length of the CIPSO category list in bytes
1071 *
1072 * Description:
1073 * Perform a label mapping to translate a local MLS category bitmap to the
1074 * correct CIPSO category list using the given DOI definition. Returns the
1075 * size in bytes of the network category bitmap on success, negative values
1076 * otherwise.
1077 *
1078 */
1079static int cipso_v4_map_cat_rng_hton(const struct cipso_v4_doi *doi_def,
1080 const struct netlbl_lsm_secattr *secattr,
1081 unsigned char *net_cat,
1082 u32 net_cat_len)
1083{
Paul Moore484b3662006-11-29 13:18:20 -05001084 int iter = -1;
Paul Mooref998e8c2007-02-28 15:14:20 -05001085 u16 array[CIPSO_V4_TAG_RNG_CAT_MAX * 2];
Paul Moore484b3662006-11-29 13:18:20 -05001086 u32 array_cnt = 0;
1087 u32 cat_size = 0;
1088
Paul Mooref998e8c2007-02-28 15:14:20 -05001089 /* make sure we don't overflow the 'array[]' variable */
Paul Moore128c6b62007-02-28 15:14:21 -05001090 if (net_cat_len >
1091 (CIPSO_V4_OPT_LEN_MAX - CIPSO_V4_HDR_LEN - CIPSO_V4_TAG_RNG_BLEN))
1092 return -ENOSPC;
Paul Moore484b3662006-11-29 13:18:20 -05001093
1094 for (;;) {
Paul Moore16efd452008-01-29 08:37:59 -05001095 iter = netlbl_secattr_catmap_walk(secattr->attr.mls.cat,
1096 iter + 1);
Paul Moore484b3662006-11-29 13:18:20 -05001097 if (iter < 0)
1098 break;
1099 cat_size += (iter == 0 ? 0 : sizeof(u16));
1100 if (cat_size > net_cat_len)
1101 return -ENOSPC;
1102 array[array_cnt++] = iter;
1103
Paul Moore16efd452008-01-29 08:37:59 -05001104 iter = netlbl_secattr_catmap_walk_rng(secattr->attr.mls.cat,
1105 iter);
Paul Moore484b3662006-11-29 13:18:20 -05001106 if (iter < 0)
1107 return -EFAULT;
1108 cat_size += sizeof(u16);
1109 if (cat_size > net_cat_len)
1110 return -ENOSPC;
1111 array[array_cnt++] = iter;
1112 }
1113
1114 for (iter = 0; array_cnt > 0;) {
1115 *((__be16 *)&net_cat[iter]) = htons(array[--array_cnt]);
1116 iter += 2;
1117 array_cnt--;
1118 if (array[array_cnt] != 0) {
1119 *((__be16 *)&net_cat[iter]) = htons(array[array_cnt]);
1120 iter += 2;
1121 }
1122 }
1123
1124 return cat_size;
1125}
1126
1127/**
1128 * cipso_v4_map_cat_rng_ntoh - Perform a category mapping from network to host
1129 * @doi_def: the DOI definition
1130 * @net_cat: the category list in network/CIPSO format
1131 * @net_cat_len: the length of the CIPSO bitmap in bytes
1132 * @secattr: the security attributes
1133 *
1134 * Description:
1135 * Perform a label mapping to translate a CIPSO category list to the correct
1136 * local MLS category bitmap using the given DOI definition. Returns zero on
1137 * success, negative values on failure.
1138 *
1139 */
1140static int cipso_v4_map_cat_rng_ntoh(const struct cipso_v4_doi *doi_def,
1141 const unsigned char *net_cat,
1142 u32 net_cat_len,
1143 struct netlbl_lsm_secattr *secattr)
1144{
1145 int ret_val;
1146 u32 net_iter;
1147 u16 cat_low;
1148 u16 cat_high;
1149
Stephen Hemminger132adf52007-03-08 20:44:43 -08001150 for (net_iter = 0; net_iter < net_cat_len; net_iter += 4) {
Harvey Harrisond3e2ce32008-05-02 16:26:16 -07001151 cat_high = get_unaligned_be16(&net_cat[net_iter]);
Paul Moore484b3662006-11-29 13:18:20 -05001152 if ((net_iter + 4) <= net_cat_len)
Harvey Harrisond3e2ce32008-05-02 16:26:16 -07001153 cat_low = get_unaligned_be16(&net_cat[net_iter + 2]);
Paul Moore484b3662006-11-29 13:18:20 -05001154 else
1155 cat_low = 0;
1156
Paul Moore16efd452008-01-29 08:37:59 -05001157 ret_val = netlbl_secattr_catmap_setrng(secattr->attr.mls.cat,
Paul Moore484b3662006-11-29 13:18:20 -05001158 cat_low,
1159 cat_high,
1160 GFP_ATOMIC);
1161 if (ret_val != 0)
1162 return ret_val;
1163 }
1164
1165 return 0;
1166}
1167
Paul Moore446fda42006-08-03 16:48:06 -07001168/*
1169 * Protocol Handling Functions
1170 */
1171
Paul Moore446fda42006-08-03 16:48:06 -07001172/**
1173 * cipso_v4_gentag_hdr - Generate a CIPSO option header
1174 * @doi_def: the DOI definition
Paul Moore91b1ed02006-11-17 17:38:49 -05001175 * @len: the total tag length in bytes, not including this header
Paul Moore446fda42006-08-03 16:48:06 -07001176 * @buf: the CIPSO option buffer
1177 *
1178 * Description:
Paul Moore91b1ed02006-11-17 17:38:49 -05001179 * Write a CIPSO header into the beginning of @buffer.
Paul Moore446fda42006-08-03 16:48:06 -07001180 *
1181 */
Paul Moore91b1ed02006-11-17 17:38:49 -05001182static void cipso_v4_gentag_hdr(const struct cipso_v4_doi *doi_def,
1183 unsigned char *buf,
1184 u32 len)
Paul Moore446fda42006-08-03 16:48:06 -07001185{
Paul Moore446fda42006-08-03 16:48:06 -07001186 buf[0] = IPOPT_CIPSO;
1187 buf[1] = CIPSO_V4_HDR_LEN + len;
Al Viro714e85b2006-11-14 20:51:49 -08001188 *(__be32 *)&buf[2] = htonl(doi_def->doi);
Paul Moore446fda42006-08-03 16:48:06 -07001189}
1190
Paul Moore446fda42006-08-03 16:48:06 -07001191/**
1192 * cipso_v4_gentag_rbm - Generate a CIPSO restricted bitmap tag (type #1)
1193 * @doi_def: the DOI definition
1194 * @secattr: the security attributes
1195 * @buffer: the option buffer
1196 * @buffer_len: length of buffer in bytes
1197 *
1198 * Description:
1199 * Generate a CIPSO option using the restricted bitmap tag, tag type #1. The
1200 * actual buffer length may be larger than the indicated size due to
Paul Moore91b1ed02006-11-17 17:38:49 -05001201 * translation between host and network category bitmaps. Returns the size of
1202 * the tag on success, negative values on failure.
Paul Moore446fda42006-08-03 16:48:06 -07001203 *
1204 */
1205static int cipso_v4_gentag_rbm(const struct cipso_v4_doi *doi_def,
1206 const struct netlbl_lsm_secattr *secattr,
Paul Moore91b1ed02006-11-17 17:38:49 -05001207 unsigned char *buffer,
1208 u32 buffer_len)
Paul Moore446fda42006-08-03 16:48:06 -07001209{
Paul Moore701a90b2006-11-17 17:38:46 -05001210 int ret_val;
Paul Moore91b1ed02006-11-17 17:38:49 -05001211 u32 tag_len;
Paul Moore446fda42006-08-03 16:48:06 -07001212 u32 level;
1213
Paul Moore701a90b2006-11-17 17:38:46 -05001214 if ((secattr->flags & NETLBL_SECATTR_MLS_LVL) == 0)
1215 return -EPERM;
1216
Paul Moore16efd452008-01-29 08:37:59 -05001217 ret_val = cipso_v4_map_lvl_hton(doi_def,
1218 secattr->attr.mls.lvl,
1219 &level);
Paul Moore91b1ed02006-11-17 17:38:49 -05001220 if (ret_val != 0)
1221 return ret_val;
Paul Moore446fda42006-08-03 16:48:06 -07001222
Paul Moore91b1ed02006-11-17 17:38:49 -05001223 if (secattr->flags & NETLBL_SECATTR_MLS_CAT) {
Paul Moore446fda42006-08-03 16:48:06 -07001224 ret_val = cipso_v4_map_cat_rbm_hton(doi_def,
Paul Moore02752762006-11-29 13:18:18 -05001225 secattr,
Paul Moore91b1ed02006-11-17 17:38:49 -05001226 &buffer[4],
1227 buffer_len - 4);
Paul Moore446fda42006-08-03 16:48:06 -07001228 if (ret_val < 0)
Paul Moore91b1ed02006-11-17 17:38:49 -05001229 return ret_val;
Paul Moore446fda42006-08-03 16:48:06 -07001230
1231 /* This will send packets using the "optimized" format when
1232 * possibile as specified in section 3.4.2.6 of the
1233 * CIPSO draft. */
Paul Moore701a90b2006-11-17 17:38:46 -05001234 if (cipso_v4_rbm_optfmt && ret_val > 0 && ret_val <= 10)
Paul Moore91b1ed02006-11-17 17:38:49 -05001235 tag_len = 14;
Paul Moore701a90b2006-11-17 17:38:46 -05001236 else
Paul Moore91b1ed02006-11-17 17:38:49 -05001237 tag_len = 4 + ret_val;
1238 } else
1239 tag_len = 4;
Paul Moore446fda42006-08-03 16:48:06 -07001240
Paul Moore91b1ed02006-11-17 17:38:49 -05001241 buffer[0] = 0x01;
1242 buffer[1] = tag_len;
1243 buffer[3] = level;
Paul Moore446fda42006-08-03 16:48:06 -07001244
Paul Moore91b1ed02006-11-17 17:38:49 -05001245 return tag_len;
Paul Moore446fda42006-08-03 16:48:06 -07001246}
1247
1248/**
1249 * cipso_v4_parsetag_rbm - Parse a CIPSO restricted bitmap tag
1250 * @doi_def: the DOI definition
1251 * @tag: the CIPSO tag
1252 * @secattr: the security attributes
1253 *
1254 * Description:
1255 * Parse a CIPSO restricted bitmap tag (tag type #1) and return the security
1256 * attributes in @secattr. Return zero on success, negatives values on
1257 * failure.
1258 *
1259 */
1260static int cipso_v4_parsetag_rbm(const struct cipso_v4_doi *doi_def,
1261 const unsigned char *tag,
1262 struct netlbl_lsm_secattr *secattr)
1263{
1264 int ret_val;
1265 u8 tag_len = tag[1];
1266 u32 level;
1267
1268 ret_val = cipso_v4_map_lvl_ntoh(doi_def, tag[3], &level);
1269 if (ret_val != 0)
1270 return ret_val;
Paul Moore16efd452008-01-29 08:37:59 -05001271 secattr->attr.mls.lvl = level;
Paul Moore701a90b2006-11-17 17:38:46 -05001272 secattr->flags |= NETLBL_SECATTR_MLS_LVL;
Paul Moore446fda42006-08-03 16:48:06 -07001273
1274 if (tag_len > 4) {
Paul Moore16efd452008-01-29 08:37:59 -05001275 secattr->attr.mls.cat =
1276 netlbl_secattr_catmap_alloc(GFP_ATOMIC);
1277 if (secattr->attr.mls.cat == NULL)
Paul Moore446fda42006-08-03 16:48:06 -07001278 return -ENOMEM;
1279
1280 ret_val = cipso_v4_map_cat_rbm_ntoh(doi_def,
1281 &tag[4],
1282 tag_len - 4,
Paul Moore02752762006-11-29 13:18:18 -05001283 secattr);
1284 if (ret_val != 0) {
Paul Moore16efd452008-01-29 08:37:59 -05001285 netlbl_secattr_catmap_free(secattr->attr.mls.cat);
Paul Moore446fda42006-08-03 16:48:06 -07001286 return ret_val;
1287 }
Paul Moore02752762006-11-29 13:18:18 -05001288
1289 secattr->flags |= NETLBL_SECATTR_MLS_CAT;
Paul Moore446fda42006-08-03 16:48:06 -07001290 }
1291
1292 return 0;
1293}
1294
1295/**
Paul Moore654bbc22006-11-29 13:18:19 -05001296 * cipso_v4_gentag_enum - Generate a CIPSO enumerated tag (type #2)
1297 * @doi_def: the DOI definition
1298 * @secattr: the security attributes
1299 * @buffer: the option buffer
1300 * @buffer_len: length of buffer in bytes
1301 *
1302 * Description:
1303 * Generate a CIPSO option using the enumerated tag, tag type #2. Returns the
1304 * size of the tag on success, negative values on failure.
1305 *
1306 */
1307static int cipso_v4_gentag_enum(const struct cipso_v4_doi *doi_def,
1308 const struct netlbl_lsm_secattr *secattr,
1309 unsigned char *buffer,
1310 u32 buffer_len)
1311{
1312 int ret_val;
1313 u32 tag_len;
1314 u32 level;
1315
1316 if (!(secattr->flags & NETLBL_SECATTR_MLS_LVL))
1317 return -EPERM;
1318
Paul Moore16efd452008-01-29 08:37:59 -05001319 ret_val = cipso_v4_map_lvl_hton(doi_def,
1320 secattr->attr.mls.lvl,
1321 &level);
Paul Moore654bbc22006-11-29 13:18:19 -05001322 if (ret_val != 0)
1323 return ret_val;
1324
1325 if (secattr->flags & NETLBL_SECATTR_MLS_CAT) {
1326 ret_val = cipso_v4_map_cat_enum_hton(doi_def,
1327 secattr,
1328 &buffer[4],
1329 buffer_len - 4);
1330 if (ret_val < 0)
1331 return ret_val;
1332
1333 tag_len = 4 + ret_val;
1334 } else
1335 tag_len = 4;
1336
1337 buffer[0] = 0x02;
1338 buffer[1] = tag_len;
1339 buffer[3] = level;
1340
1341 return tag_len;
1342}
1343
1344/**
1345 * cipso_v4_parsetag_enum - Parse a CIPSO enumerated tag
1346 * @doi_def: the DOI definition
1347 * @tag: the CIPSO tag
1348 * @secattr: the security attributes
1349 *
1350 * Description:
1351 * Parse a CIPSO enumerated tag (tag type #2) and return the security
1352 * attributes in @secattr. Return zero on success, negatives values on
1353 * failure.
1354 *
1355 */
1356static int cipso_v4_parsetag_enum(const struct cipso_v4_doi *doi_def,
1357 const unsigned char *tag,
1358 struct netlbl_lsm_secattr *secattr)
1359{
1360 int ret_val;
1361 u8 tag_len = tag[1];
1362 u32 level;
1363
1364 ret_val = cipso_v4_map_lvl_ntoh(doi_def, tag[3], &level);
1365 if (ret_val != 0)
1366 return ret_val;
Paul Moore16efd452008-01-29 08:37:59 -05001367 secattr->attr.mls.lvl = level;
Paul Moore654bbc22006-11-29 13:18:19 -05001368 secattr->flags |= NETLBL_SECATTR_MLS_LVL;
1369
1370 if (tag_len > 4) {
Paul Moore16efd452008-01-29 08:37:59 -05001371 secattr->attr.mls.cat =
1372 netlbl_secattr_catmap_alloc(GFP_ATOMIC);
1373 if (secattr->attr.mls.cat == NULL)
Paul Moore654bbc22006-11-29 13:18:19 -05001374 return -ENOMEM;
1375
1376 ret_val = cipso_v4_map_cat_enum_ntoh(doi_def,
1377 &tag[4],
1378 tag_len - 4,
1379 secattr);
1380 if (ret_val != 0) {
Paul Moore16efd452008-01-29 08:37:59 -05001381 netlbl_secattr_catmap_free(secattr->attr.mls.cat);
Paul Moore654bbc22006-11-29 13:18:19 -05001382 return ret_val;
1383 }
1384
1385 secattr->flags |= NETLBL_SECATTR_MLS_CAT;
1386 }
1387
1388 return 0;
1389}
1390
1391/**
Paul Moore484b3662006-11-29 13:18:20 -05001392 * cipso_v4_gentag_rng - Generate a CIPSO ranged tag (type #5)
1393 * @doi_def: the DOI definition
1394 * @secattr: the security attributes
1395 * @buffer: the option buffer
1396 * @buffer_len: length of buffer in bytes
1397 *
1398 * Description:
1399 * Generate a CIPSO option using the ranged tag, tag type #5. Returns the
1400 * size of the tag on success, negative values on failure.
1401 *
1402 */
1403static int cipso_v4_gentag_rng(const struct cipso_v4_doi *doi_def,
1404 const struct netlbl_lsm_secattr *secattr,
1405 unsigned char *buffer,
1406 u32 buffer_len)
1407{
1408 int ret_val;
1409 u32 tag_len;
1410 u32 level;
1411
1412 if (!(secattr->flags & NETLBL_SECATTR_MLS_LVL))
1413 return -EPERM;
1414
Paul Moore16efd452008-01-29 08:37:59 -05001415 ret_val = cipso_v4_map_lvl_hton(doi_def,
1416 secattr->attr.mls.lvl,
1417 &level);
Paul Moore484b3662006-11-29 13:18:20 -05001418 if (ret_val != 0)
1419 return ret_val;
1420
1421 if (secattr->flags & NETLBL_SECATTR_MLS_CAT) {
1422 ret_val = cipso_v4_map_cat_rng_hton(doi_def,
1423 secattr,
1424 &buffer[4],
1425 buffer_len - 4);
1426 if (ret_val < 0)
1427 return ret_val;
1428
1429 tag_len = 4 + ret_val;
1430 } else
1431 tag_len = 4;
1432
1433 buffer[0] = 0x05;
1434 buffer[1] = tag_len;
1435 buffer[3] = level;
1436
1437 return tag_len;
1438}
1439
1440/**
1441 * cipso_v4_parsetag_rng - Parse a CIPSO ranged tag
1442 * @doi_def: the DOI definition
1443 * @tag: the CIPSO tag
1444 * @secattr: the security attributes
1445 *
1446 * Description:
1447 * Parse a CIPSO ranged tag (tag type #5) and return the security attributes
1448 * in @secattr. Return zero on success, negatives values on failure.
1449 *
1450 */
1451static int cipso_v4_parsetag_rng(const struct cipso_v4_doi *doi_def,
1452 const unsigned char *tag,
1453 struct netlbl_lsm_secattr *secattr)
1454{
1455 int ret_val;
1456 u8 tag_len = tag[1];
1457 u32 level;
1458
1459 ret_val = cipso_v4_map_lvl_ntoh(doi_def, tag[3], &level);
1460 if (ret_val != 0)
1461 return ret_val;
Paul Moore16efd452008-01-29 08:37:59 -05001462 secattr->attr.mls.lvl = level;
Paul Moore484b3662006-11-29 13:18:20 -05001463 secattr->flags |= NETLBL_SECATTR_MLS_LVL;
1464
1465 if (tag_len > 4) {
Paul Moore16efd452008-01-29 08:37:59 -05001466 secattr->attr.mls.cat =
1467 netlbl_secattr_catmap_alloc(GFP_ATOMIC);
1468 if (secattr->attr.mls.cat == NULL)
Paul Moore484b3662006-11-29 13:18:20 -05001469 return -ENOMEM;
1470
1471 ret_val = cipso_v4_map_cat_rng_ntoh(doi_def,
1472 &tag[4],
1473 tag_len - 4,
1474 secattr);
1475 if (ret_val != 0) {
Paul Moore16efd452008-01-29 08:37:59 -05001476 netlbl_secattr_catmap_free(secattr->attr.mls.cat);
Paul Moore484b3662006-11-29 13:18:20 -05001477 return ret_val;
1478 }
1479
1480 secattr->flags |= NETLBL_SECATTR_MLS_CAT;
1481 }
1482
1483 return 0;
1484}
1485
1486/**
Paul Moore446fda42006-08-03 16:48:06 -07001487 * cipso_v4_validate - Validate a CIPSO option
1488 * @option: the start of the option, on error it is set to point to the error
1489 *
1490 * Description:
1491 * This routine is called to validate a CIPSO option, it checks all of the
1492 * fields to ensure that they are at least valid, see the draft snippet below
1493 * for details. If the option is valid then a zero value is returned and
1494 * the value of @option is unchanged. If the option is invalid then a
1495 * non-zero value is returned and @option is adjusted to point to the
1496 * offending portion of the option. From the IETF draft ...
1497 *
1498 * "If any field within the CIPSO options, such as the DOI identifier, is not
1499 * recognized the IP datagram is discarded and an ICMP 'parameter problem'
1500 * (type 12) is generated and returned. The ICMP code field is set to 'bad
1501 * parameter' (code 0) and the pointer is set to the start of the CIPSO field
1502 * that is unrecognized."
1503 *
1504 */
1505int cipso_v4_validate(unsigned char **option)
1506{
1507 unsigned char *opt = *option;
1508 unsigned char *tag;
1509 unsigned char opt_iter;
1510 unsigned char err_offset = 0;
1511 u8 opt_len;
1512 u8 tag_len;
1513 struct cipso_v4_doi *doi_def = NULL;
1514 u32 tag_iter;
1515
1516 /* caller already checks for length values that are too large */
1517 opt_len = opt[1];
1518 if (opt_len < 8) {
1519 err_offset = 1;
1520 goto validate_return;
1521 }
1522
1523 rcu_read_lock();
Harvey Harrisond3e2ce32008-05-02 16:26:16 -07001524 doi_def = cipso_v4_doi_search(get_unaligned_be32(&opt[2]));
Paul Moore446fda42006-08-03 16:48:06 -07001525 if (doi_def == NULL) {
1526 err_offset = 2;
1527 goto validate_return_locked;
1528 }
1529
1530 opt_iter = 6;
1531 tag = opt + opt_iter;
1532 while (opt_iter < opt_len) {
1533 for (tag_iter = 0; doi_def->tags[tag_iter] != tag[0];)
1534 if (doi_def->tags[tag_iter] == CIPSO_V4_TAG_INVALID ||
1535 ++tag_iter == CIPSO_V4_TAG_MAXCNT) {
1536 err_offset = opt_iter;
1537 goto validate_return_locked;
1538 }
1539
1540 tag_len = tag[1];
1541 if (tag_len > (opt_len - opt_iter)) {
1542 err_offset = opt_iter + 1;
1543 goto validate_return_locked;
1544 }
1545
1546 switch (tag[0]) {
1547 case CIPSO_V4_TAG_RBITMAP:
1548 if (tag_len < 4) {
1549 err_offset = opt_iter + 1;
1550 goto validate_return_locked;
1551 }
1552
1553 /* We are already going to do all the verification
1554 * necessary at the socket layer so from our point of
1555 * view it is safe to turn these checks off (and less
1556 * work), however, the CIPSO draft says we should do
1557 * all the CIPSO validations here but it doesn't
1558 * really specify _exactly_ what we need to validate
1559 * ... so, just make it a sysctl tunable. */
1560 if (cipso_v4_rbm_strictvalid) {
1561 if (cipso_v4_map_lvl_valid(doi_def,
1562 tag[3]) < 0) {
1563 err_offset = opt_iter + 3;
1564 goto validate_return_locked;
1565 }
1566 if (tag_len > 4 &&
1567 cipso_v4_map_cat_rbm_valid(doi_def,
1568 &tag[4],
1569 tag_len - 4) < 0) {
1570 err_offset = opt_iter + 4;
1571 goto validate_return_locked;
1572 }
1573 }
1574 break;
Paul Moore654bbc22006-11-29 13:18:19 -05001575 case CIPSO_V4_TAG_ENUM:
1576 if (tag_len < 4) {
1577 err_offset = opt_iter + 1;
1578 goto validate_return_locked;
1579 }
1580
1581 if (cipso_v4_map_lvl_valid(doi_def,
1582 tag[3]) < 0) {
1583 err_offset = opt_iter + 3;
1584 goto validate_return_locked;
1585 }
1586 if (tag_len > 4 &&
1587 cipso_v4_map_cat_enum_valid(doi_def,
1588 &tag[4],
1589 tag_len - 4) < 0) {
1590 err_offset = opt_iter + 4;
1591 goto validate_return_locked;
1592 }
1593 break;
Paul Moore484b3662006-11-29 13:18:20 -05001594 case CIPSO_V4_TAG_RANGE:
1595 if (tag_len < 4) {
1596 err_offset = opt_iter + 1;
1597 goto validate_return_locked;
1598 }
1599
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_rng_valid(doi_def,
1607 &tag[4],
1608 tag_len - 4) < 0) {
1609 err_offset = opt_iter + 4;
1610 goto validate_return_locked;
1611 }
1612 break;
Paul Moore446fda42006-08-03 16:48:06 -07001613 default:
1614 err_offset = opt_iter;
1615 goto validate_return_locked;
1616 }
1617
1618 tag += tag_len;
1619 opt_iter += tag_len;
1620 }
1621
1622validate_return_locked:
1623 rcu_read_unlock();
1624validate_return:
1625 *option = opt + err_offset;
1626 return err_offset;
1627}
1628
1629/**
1630 * cipso_v4_error - Send the correct reponse for a bad packet
1631 * @skb: the packet
1632 * @error: the error code
1633 * @gateway: CIPSO gateway flag
1634 *
1635 * Description:
1636 * Based on the error code given in @error, send an ICMP error message back to
1637 * the originating host. From the IETF draft ...
1638 *
1639 * "If the contents of the CIPSO [option] are valid but the security label is
1640 * outside of the configured host or port label range, the datagram is
1641 * discarded and an ICMP 'destination unreachable' (type 3) is generated and
1642 * returned. The code field of the ICMP is set to 'communication with
1643 * destination network administratively prohibited' (code 9) or to
1644 * 'communication with destination host administratively prohibited'
1645 * (code 10). The value of the code is dependent on whether the originator
1646 * of the ICMP message is acting as a CIPSO host or a CIPSO gateway. The
1647 * recipient of the ICMP message MUST be able to handle either value. The
1648 * same procedure is performed if a CIPSO [option] can not be added to an
1649 * IP packet because it is too large to fit in the IP options area."
1650 *
1651 * "If the error is triggered by receipt of an ICMP message, the message is
1652 * discarded and no response is permitted (consistent with general ICMP
1653 * processing rules)."
1654 *
1655 */
1656void cipso_v4_error(struct sk_buff *skb, int error, u32 gateway)
1657{
Arnaldo Carvalho de Meloeddc9ec2007-04-20 22:47:35 -07001658 if (ip_hdr(skb)->protocol == IPPROTO_ICMP || error != -EACCES)
Paul Moore446fda42006-08-03 16:48:06 -07001659 return;
1660
1661 if (gateway)
1662 icmp_send(skb, ICMP_DEST_UNREACH, ICMP_NET_ANO, 0);
1663 else
1664 icmp_send(skb, ICMP_DEST_UNREACH, ICMP_HOST_ANO, 0);
1665}
1666
1667/**
Paul Moore948bf852008-10-10 10:16:32 -04001668 * cipso_v4_genopt - Generate a CIPSO option
1669 * @buf: the option buffer
1670 * @buf_len: the size of opt_buf
Paul Moore446fda42006-08-03 16:48:06 -07001671 * @doi_def: the CIPSO DOI to use
Paul Moore948bf852008-10-10 10:16:32 -04001672 * @secattr: the security attributes
Paul Moore446fda42006-08-03 16:48:06 -07001673 *
1674 * Description:
Paul Moore948bf852008-10-10 10:16:32 -04001675 * Generate a CIPSO option using the DOI definition and security attributes
1676 * passed to the function. Returns the length of the option on success and
1677 * negative values on failure.
Paul Moore446fda42006-08-03 16:48:06 -07001678 *
1679 */
Paul Moore948bf852008-10-10 10:16:32 -04001680static int cipso_v4_genopt(unsigned char *buf, u32 buf_len,
1681 const struct cipso_v4_doi *doi_def,
1682 const struct netlbl_lsm_secattr *secattr)
Paul Moore446fda42006-08-03 16:48:06 -07001683{
Paul Moore948bf852008-10-10 10:16:32 -04001684 int ret_val;
Paul Moore446fda42006-08-03 16:48:06 -07001685 u32 iter;
Paul Moore446fda42006-08-03 16:48:06 -07001686
Paul Moore948bf852008-10-10 10:16:32 -04001687 if (buf_len <= CIPSO_V4_HDR_LEN)
1688 return -ENOSPC;
Paul Moore91b1ed02006-11-17 17:38:49 -05001689
Paul Moore446fda42006-08-03 16:48:06 -07001690 /* XXX - This code assumes only one tag per CIPSO option which isn't
1691 * really a good assumption to make but since we only support the MAC
1692 * tags right now it is a safe assumption. */
1693 iter = 0;
1694 do {
Paul Moore91b1ed02006-11-17 17:38:49 -05001695 memset(buf, 0, buf_len);
Paul Moore446fda42006-08-03 16:48:06 -07001696 switch (doi_def->tags[iter]) {
1697 case CIPSO_V4_TAG_RBITMAP:
1698 ret_val = cipso_v4_gentag_rbm(doi_def,
Paul Moore91b1ed02006-11-17 17:38:49 -05001699 secattr,
1700 &buf[CIPSO_V4_HDR_LEN],
1701 buf_len - CIPSO_V4_HDR_LEN);
Paul Moore446fda42006-08-03 16:48:06 -07001702 break;
Paul Moore654bbc22006-11-29 13:18:19 -05001703 case CIPSO_V4_TAG_ENUM:
1704 ret_val = cipso_v4_gentag_enum(doi_def,
1705 secattr,
1706 &buf[CIPSO_V4_HDR_LEN],
1707 buf_len - CIPSO_V4_HDR_LEN);
1708 break;
Paul Moore484b3662006-11-29 13:18:20 -05001709 case CIPSO_V4_TAG_RANGE:
1710 ret_val = cipso_v4_gentag_rng(doi_def,
1711 secattr,
1712 &buf[CIPSO_V4_HDR_LEN],
1713 buf_len - CIPSO_V4_HDR_LEN);
1714 break;
Paul Moore446fda42006-08-03 16:48:06 -07001715 default:
Paul Moore948bf852008-10-10 10:16:32 -04001716 return -EPERM;
Paul Moore446fda42006-08-03 16:48:06 -07001717 }
1718
1719 iter++;
Paul Moore91b1ed02006-11-17 17:38:49 -05001720 } while (ret_val < 0 &&
Paul Moore446fda42006-08-03 16:48:06 -07001721 iter < CIPSO_V4_TAG_MAXCNT &&
1722 doi_def->tags[iter] != CIPSO_V4_TAG_INVALID);
Paul Moore91b1ed02006-11-17 17:38:49 -05001723 if (ret_val < 0)
Paul Moore948bf852008-10-10 10:16:32 -04001724 return ret_val;
Paul Moore91b1ed02006-11-17 17:38:49 -05001725 cipso_v4_gentag_hdr(doi_def, buf, ret_val);
Paul Moore948bf852008-10-10 10:16:32 -04001726 return CIPSO_V4_HDR_LEN + ret_val;
1727}
1728
1729/**
1730 * cipso_v4_sock_setattr - Add a CIPSO option to a socket
1731 * @sk: the socket
1732 * @doi_def: the CIPSO DOI to use
1733 * @secattr: the specific security attributes of the socket
1734 *
1735 * Description:
1736 * Set the CIPSO option on the given socket using the DOI definition and
1737 * security attributes passed to the function. This function requires
1738 * exclusive access to @sk, which means it either needs to be in the
1739 * process of being created or locked. Returns zero on success and negative
1740 * values on failure.
1741 *
1742 */
1743int cipso_v4_sock_setattr(struct sock *sk,
1744 const struct cipso_v4_doi *doi_def,
1745 const struct netlbl_lsm_secattr *secattr)
1746{
1747 int ret_val = -EPERM;
1748 unsigned char *buf = NULL;
1749 u32 buf_len;
1750 u32 opt_len;
1751 struct ip_options *opt = NULL;
1752 struct inet_sock *sk_inet;
1753 struct inet_connection_sock *sk_conn;
1754
1755 /* In the case of sock_create_lite(), the sock->sk field is not
1756 * defined yet but it is not a problem as the only users of these
1757 * "lite" PF_INET sockets are functions which do an accept() call
1758 * afterwards so we will label the socket as part of the accept(). */
1759 if (sk == NULL)
1760 return 0;
1761
1762 /* We allocate the maximum CIPSO option size here so we are probably
1763 * being a little wasteful, but it makes our life _much_ easier later
1764 * on and after all we are only talking about 40 bytes. */
1765 buf_len = CIPSO_V4_OPT_LEN_MAX;
1766 buf = kmalloc(buf_len, GFP_ATOMIC);
1767 if (buf == NULL) {
1768 ret_val = -ENOMEM;
1769 goto socket_setattr_failure;
1770 }
1771
1772 ret_val = cipso_v4_genopt(buf, buf_len, doi_def, secattr);
1773 if (ret_val < 0)
1774 goto socket_setattr_failure;
1775 buf_len = ret_val;
Paul Moore446fda42006-08-03 16:48:06 -07001776
1777 /* We can't use ip_options_get() directly because it makes a call to
1778 * ip_options_get_alloc() which allocates memory with GFP_KERNEL and
Paul Mooref8687af2006-10-30 15:22:15 -08001779 * we won't always have CAP_NET_RAW even though we _always_ want to
1780 * set the IPOPT_CIPSO option. */
Paul Moore446fda42006-08-03 16:48:06 -07001781 opt_len = (buf_len + 3) & ~3;
1782 opt = kzalloc(sizeof(*opt) + opt_len, GFP_ATOMIC);
1783 if (opt == NULL) {
1784 ret_val = -ENOMEM;
1785 goto socket_setattr_failure;
1786 }
1787 memcpy(opt->__data, buf, buf_len);
1788 opt->optlen = opt_len;
Paul Mooref8687af2006-10-30 15:22:15 -08001789 opt->cipso = sizeof(struct iphdr);
Paul Moore446fda42006-08-03 16:48:06 -07001790 kfree(buf);
1791 buf = NULL;
Paul Moore446fda42006-08-03 16:48:06 -07001792
1793 sk_inet = inet_sk(sk);
1794 if (sk_inet->is_icsk) {
1795 sk_conn = inet_csk(sk);
1796 if (sk_inet->opt)
1797 sk_conn->icsk_ext_hdr_len -= sk_inet->opt->optlen;
1798 sk_conn->icsk_ext_hdr_len += opt->optlen;
1799 sk_conn->icsk_sync_mss(sk, sk_conn->icsk_pmtu_cookie);
1800 }
1801 opt = xchg(&sk_inet->opt, opt);
1802 kfree(opt);
1803
1804 return 0;
1805
1806socket_setattr_failure:
1807 kfree(buf);
1808 kfree(opt);
1809 return ret_val;
1810}
1811
1812/**
Paul Moore63d804ea2007-09-15 21:45:13 -07001813 * cipso_v4_getattr - Helper function for the cipso_v4_*_getattr functions
1814 * @cipso: the CIPSO v4 option
1815 * @secattr: the security attributes
1816 *
1817 * Description:
1818 * Inspect @cipso and return the security attributes in @secattr. Returns zero
1819 * on success and negative values on failure.
1820 *
1821 */
1822static int cipso_v4_getattr(const unsigned char *cipso,
1823 struct netlbl_lsm_secattr *secattr)
1824{
1825 int ret_val = -ENOMSG;
1826 u32 doi;
1827 struct cipso_v4_doi *doi_def;
1828
1829 if (cipso_v4_cache_check(cipso, cipso[1], secattr) == 0)
1830 return 0;
1831
Harvey Harrisond3e2ce32008-05-02 16:26:16 -07001832 doi = get_unaligned_be32(&cipso[2]);
Paul Moore63d804ea2007-09-15 21:45:13 -07001833 rcu_read_lock();
1834 doi_def = cipso_v4_doi_search(doi);
1835 if (doi_def == NULL)
1836 goto getattr_return;
1837 /* XXX - This code assumes only one tag per CIPSO option which isn't
1838 * really a good assumption to make but since we only support the MAC
1839 * tags right now it is a safe assumption. */
1840 switch (cipso[6]) {
1841 case CIPSO_V4_TAG_RBITMAP:
1842 ret_val = cipso_v4_parsetag_rbm(doi_def, &cipso[6], secattr);
1843 break;
1844 case CIPSO_V4_TAG_ENUM:
1845 ret_val = cipso_v4_parsetag_enum(doi_def, &cipso[6], secattr);
1846 break;
1847 case CIPSO_V4_TAG_RANGE:
1848 ret_val = cipso_v4_parsetag_rng(doi_def, &cipso[6], secattr);
1849 break;
1850 }
Paul Moore16efd452008-01-29 08:37:59 -05001851 if (ret_val == 0)
1852 secattr->type = NETLBL_NLTYPE_CIPSOV4;
Paul Moore63d804ea2007-09-15 21:45:13 -07001853
1854getattr_return:
1855 rcu_read_unlock();
1856 return ret_val;
1857}
1858
1859/**
Paul Moore14a72f52006-09-25 15:52:01 -07001860 * cipso_v4_sock_getattr - Get the security attributes from a sock
1861 * @sk: the sock
1862 * @secattr: the security attributes
1863 *
1864 * Description:
1865 * Query @sk to see if there is a CIPSO option attached to the sock and if
1866 * there is return the CIPSO security attributes in @secattr. This function
1867 * requires that @sk be locked, or privately held, but it does not do any
1868 * locking itself. Returns zero on success and negative values on failure.
1869 *
1870 */
1871int cipso_v4_sock_getattr(struct sock *sk, struct netlbl_lsm_secattr *secattr)
1872{
Paul Moore63d804ea2007-09-15 21:45:13 -07001873 struct ip_options *opt;
Paul Moore14a72f52006-09-25 15:52:01 -07001874
Paul Moore63d804ea2007-09-15 21:45:13 -07001875 opt = inet_sk(sk)->opt;
1876 if (opt == NULL || opt->cipso == 0)
Paul Moore14a72f52006-09-25 15:52:01 -07001877 return -ENOMSG;
Paul Moore14a72f52006-09-25 15:52:01 -07001878
Paul Moore63d804ea2007-09-15 21:45:13 -07001879 return cipso_v4_getattr(opt->__data + opt->cipso - sizeof(struct iphdr),
1880 secattr);
Paul Moore14a72f52006-09-25 15:52:01 -07001881}
1882
1883/**
Paul Moore948bf852008-10-10 10:16:32 -04001884 * cipso_v4_skbuff_setattr - Set the CIPSO option on a packet
1885 * @skb: the packet
1886 * @secattr: the security attributes
1887 *
1888 * Description:
1889 * Set the CIPSO option on the given packet based on the security attributes.
1890 * Returns a pointer to the IP header on success and NULL on failure.
1891 *
1892 */
1893int cipso_v4_skbuff_setattr(struct sk_buff *skb,
1894 const struct cipso_v4_doi *doi_def,
1895 const struct netlbl_lsm_secattr *secattr)
1896{
1897 int ret_val;
1898 struct iphdr *iph;
1899 struct ip_options *opt = &IPCB(skb)->opt;
1900 unsigned char buf[CIPSO_V4_OPT_LEN_MAX];
1901 u32 buf_len = CIPSO_V4_OPT_LEN_MAX;
1902 u32 opt_len;
1903 int len_delta;
1904
1905 buf_len = cipso_v4_genopt(buf, buf_len, doi_def, secattr);
1906 if (buf_len < 0)
1907 return buf_len;
1908 opt_len = (buf_len + 3) & ~3;
1909
1910 /* we overwrite any existing options to ensure that we have enough
1911 * room for the CIPSO option, the reason is that we _need_ to guarantee
1912 * that the security label is applied to the packet - we do the same
1913 * thing when using the socket options and it hasn't caused a problem,
1914 * if we need to we can always revisit this choice later */
1915
1916 len_delta = opt_len - opt->optlen;
1917 /* if we don't ensure enough headroom we could panic on the skb_push()
1918 * call below so make sure we have enough, we are also "mangling" the
1919 * packet so we should probably do a copy-on-write call anyway */
1920 ret_val = skb_cow(skb, skb_headroom(skb) + len_delta);
1921 if (ret_val < 0)
1922 return ret_val;
1923
1924 if (len_delta > 0) {
1925 /* we assume that the header + opt->optlen have already been
1926 * "pushed" in ip_options_build() or similar */
1927 iph = ip_hdr(skb);
1928 skb_push(skb, len_delta);
1929 memmove((char *)iph - len_delta, iph, iph->ihl << 2);
1930 skb_reset_network_header(skb);
1931 iph = ip_hdr(skb);
1932 } else if (len_delta < 0) {
1933 iph = ip_hdr(skb);
1934 memset(iph + 1, IPOPT_NOP, opt->optlen);
1935 } else
1936 iph = ip_hdr(skb);
1937
1938 if (opt->optlen > 0)
1939 memset(opt, 0, sizeof(*opt));
1940 opt->optlen = opt_len;
1941 opt->cipso = sizeof(struct iphdr);
1942 opt->is_changed = 1;
1943
1944 /* we have to do the following because we are being called from a
1945 * netfilter hook which means the packet already has had the header
1946 * fields populated and the checksum calculated - yes this means we
1947 * are doing more work than needed but we do it to keep the core
1948 * stack clean and tidy */
1949 memcpy(iph + 1, buf, buf_len);
1950 if (opt_len > buf_len)
1951 memset((char *)(iph + 1) + buf_len, 0, opt_len - buf_len);
1952 if (len_delta != 0) {
1953 iph->ihl = 5 + (opt_len >> 2);
1954 iph->tot_len = htons(skb->len);
1955 }
1956 ip_send_check(iph);
1957
1958 return 0;
1959}
1960
1961/**
1962 * cipso_v4_skbuff_delattr - Delete any CIPSO options from a packet
1963 * @skb: the packet
1964 *
1965 * Description:
1966 * Removes any and all CIPSO options from the given packet. Returns zero on
1967 * success, negative values on failure.
1968 *
1969 */
1970int cipso_v4_skbuff_delattr(struct sk_buff *skb)
1971{
1972 int ret_val;
1973 struct iphdr *iph;
1974 struct ip_options *opt = &IPCB(skb)->opt;
1975 unsigned char *cipso_ptr;
1976
1977 if (opt->cipso == 0)
1978 return 0;
1979
1980 /* since we are changing the packet we should make a copy */
1981 ret_val = skb_cow(skb, skb_headroom(skb));
1982 if (ret_val < 0)
1983 return ret_val;
1984
1985 /* the easiest thing to do is just replace the cipso option with noop
1986 * options since we don't change the size of the packet, although we
1987 * still need to recalculate the checksum */
1988
1989 iph = ip_hdr(skb);
1990 cipso_ptr = (unsigned char *)iph + opt->cipso;
1991 memset(cipso_ptr, IPOPT_NOOP, cipso_ptr[1]);
1992 opt->cipso = 0;
1993 opt->is_changed = 1;
1994
1995 ip_send_check(iph);
1996
1997 return 0;
1998}
1999
2000/**
Paul Moore446fda42006-08-03 16:48:06 -07002001 * cipso_v4_skbuff_getattr - Get the security attributes from the CIPSO option
2002 * @skb: the packet
2003 * @secattr: the security attributes
2004 *
2005 * Description:
2006 * Parse the given packet's CIPSO option and return the security attributes.
2007 * Returns zero on success and negative values on failure.
2008 *
2009 */
2010int cipso_v4_skbuff_getattr(const struct sk_buff *skb,
2011 struct netlbl_lsm_secattr *secattr)
2012{
Paul Moore63d804ea2007-09-15 21:45:13 -07002013 return cipso_v4_getattr(CIPSO_V4_OPTPTR(skb), secattr);
Paul Moore446fda42006-08-03 16:48:06 -07002014}
2015
2016/*
2017 * Setup Functions
2018 */
2019
2020/**
2021 * cipso_v4_init - Initialize the CIPSO module
2022 *
2023 * Description:
2024 * Initialize the CIPSO module and prepare it for use. Returns zero on success
2025 * and negative values on failure.
2026 *
2027 */
2028static int __init cipso_v4_init(void)
2029{
2030 int ret_val;
2031
2032 ret_val = cipso_v4_cache_init();
2033 if (ret_val != 0)
2034 panic("Failed to initialize the CIPSO/IPv4 cache (%d)\n",
2035 ret_val);
2036
2037 return 0;
2038}
2039
2040subsys_initcall(cipso_v4_init);