blob: e52799047a5feac1c018bc55b01bbce4ff8a8c06 [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
Paul Moore15c45f72008-10-10 10:16:34 -0400112/* Base length of the local tag (non-standard tag).
113 * Tag definition (may change between kernel versions)
114 *
115 * 0 8 16 24 32
116 * +----------+----------+----------+----------+
117 * | 10000000 | 00000110 | 32-bit secid value |
118 * +----------+----------+----------+----------+
119 * | in (host byte order)|
120 * +----------+----------+
121 *
122 */
123#define CIPSO_V4_TAG_LOC_BLEN 6
124
Paul Mooref998e8c2007-02-28 15:14:20 -0500125/*
Paul Moore446fda42006-08-03 16:48:06 -0700126 * Helper Functions
127 */
128
129/**
130 * cipso_v4_bitmap_walk - Walk a bitmap looking for a bit
131 * @bitmap: the bitmap
132 * @bitmap_len: length in bits
133 * @offset: starting offset
134 * @state: if non-zero, look for a set (1) bit else look for a cleared (0) bit
135 *
136 * Description:
137 * Starting at @offset, walk the bitmap from left to right until either the
138 * desired bit is found or we reach the end. Return the bit offset, -1 if
139 * not found, or -2 if error.
140 */
141static int cipso_v4_bitmap_walk(const unsigned char *bitmap,
142 u32 bitmap_len,
143 u32 offset,
144 u8 state)
145{
146 u32 bit_spot;
147 u32 byte_offset;
148 unsigned char bitmask;
149 unsigned char byte;
150
151 /* gcc always rounds to zero when doing integer division */
152 byte_offset = offset / 8;
153 byte = bitmap[byte_offset];
154 bit_spot = offset;
155 bitmask = 0x80 >> (offset % 8);
156
157 while (bit_spot < bitmap_len) {
158 if ((state && (byte & bitmask) == bitmask) ||
159 (state == 0 && (byte & bitmask) == 0))
160 return bit_spot;
161
162 bit_spot++;
163 bitmask >>= 1;
164 if (bitmask == 0) {
165 byte = bitmap[++byte_offset];
166 bitmask = 0x80;
167 }
168 }
169
170 return -1;
171}
172
173/**
174 * cipso_v4_bitmap_setbit - Sets a single bit in a bitmap
175 * @bitmap: the bitmap
176 * @bit: the bit
177 * @state: if non-zero, set the bit (1) else clear the bit (0)
178 *
179 * Description:
180 * Set a single bit in the bitmask. Returns zero on success, negative values
181 * on error.
182 */
183static void cipso_v4_bitmap_setbit(unsigned char *bitmap,
184 u32 bit,
185 u8 state)
186{
187 u32 byte_spot;
188 u8 bitmask;
189
190 /* gcc always rounds to zero when doing integer division */
191 byte_spot = bit / 8;
192 bitmask = 0x80 >> (bit % 8);
193 if (state)
194 bitmap[byte_spot] |= bitmask;
195 else
196 bitmap[byte_spot] &= ~bitmask;
197}
198
199/**
Paul Moore446fda42006-08-03 16:48:06 -0700200 * cipso_v4_cache_entry_free - Frees a cache entry
201 * @entry: the entry to free
202 *
203 * Description:
paul.moore@hp.comffb733c2006-10-04 11:46:31 -0400204 * This function frees the memory associated with a cache entry including the
205 * LSM cache data if there are no longer any users, i.e. reference count == 0.
Paul Moore446fda42006-08-03 16:48:06 -0700206 *
207 */
208static void cipso_v4_cache_entry_free(struct cipso_v4_map_cache_entry *entry)
209{
paul.moore@hp.comffb733c2006-10-04 11:46:31 -0400210 if (entry->lsm_data)
211 netlbl_secattr_cache_free(entry->lsm_data);
Paul Moore446fda42006-08-03 16:48:06 -0700212 kfree(entry->key);
213 kfree(entry);
214}
215
216/**
217 * cipso_v4_map_cache_hash - Hashing function for the CIPSO cache
218 * @key: the hash key
219 * @key_len: the length of the key in bytes
220 *
221 * Description:
222 * The CIPSO tag hashing function. Returns a 32-bit hash value.
223 *
224 */
225static u32 cipso_v4_map_cache_hash(const unsigned char *key, u32 key_len)
226{
227 return jhash(key, key_len, 0);
228}
229
230/*
231 * Label Mapping Cache Functions
232 */
233
234/**
235 * cipso_v4_cache_init - Initialize the CIPSO cache
236 *
237 * Description:
238 * Initializes the CIPSO label mapping cache, this function should be called
239 * before any of the other functions defined in this file. Returns zero on
240 * success, negative values on error.
241 *
242 */
243static int cipso_v4_cache_init(void)
244{
245 u32 iter;
246
247 cipso_v4_cache = kcalloc(CIPSO_V4_CACHE_BUCKETS,
248 sizeof(struct cipso_v4_map_cache_bkt),
249 GFP_KERNEL);
250 if (cipso_v4_cache == NULL)
251 return -ENOMEM;
252
253 for (iter = 0; iter < CIPSO_V4_CACHE_BUCKETS; iter++) {
254 spin_lock_init(&cipso_v4_cache[iter].lock);
255 cipso_v4_cache[iter].size = 0;
256 INIT_LIST_HEAD(&cipso_v4_cache[iter].list);
257 }
258
259 return 0;
260}
261
262/**
263 * cipso_v4_cache_invalidate - Invalidates the current CIPSO cache
264 *
265 * Description:
266 * Invalidates and frees any entries in the CIPSO cache. Returns zero on
267 * success and negative values on failure.
268 *
269 */
270void cipso_v4_cache_invalidate(void)
271{
272 struct cipso_v4_map_cache_entry *entry, *tmp_entry;
273 u32 iter;
274
275 for (iter = 0; iter < CIPSO_V4_CACHE_BUCKETS; iter++) {
Paul Moore609c92f2006-09-25 15:52:37 -0700276 spin_lock_bh(&cipso_v4_cache[iter].lock);
Paul Moore446fda42006-08-03 16:48:06 -0700277 list_for_each_entry_safe(entry,
278 tmp_entry,
279 &cipso_v4_cache[iter].list, list) {
280 list_del(&entry->list);
281 cipso_v4_cache_entry_free(entry);
282 }
283 cipso_v4_cache[iter].size = 0;
Paul Moore609c92f2006-09-25 15:52:37 -0700284 spin_unlock_bh(&cipso_v4_cache[iter].lock);
Paul Moore446fda42006-08-03 16:48:06 -0700285 }
286
287 return;
288}
289
290/**
291 * cipso_v4_cache_check - Check the CIPSO cache for a label mapping
292 * @key: the buffer to check
293 * @key_len: buffer length in bytes
294 * @secattr: the security attribute struct to use
295 *
296 * Description:
297 * This function checks the cache to see if a label mapping already exists for
298 * the given key. If there is a match then the cache is adjusted and the
299 * @secattr struct is populated with the correct LSM security attributes. The
300 * cache is adjusted in the following manner if the entry is not already the
301 * first in the cache bucket:
302 *
303 * 1. The cache entry's activity counter is incremented
304 * 2. The previous (higher ranking) entry's activity counter is decremented
305 * 3. If the difference between the two activity counters is geater than
306 * CIPSO_V4_CACHE_REORDERLIMIT the two entries are swapped
307 *
308 * Returns zero on success, -ENOENT for a cache miss, and other negative values
309 * on error.
310 *
311 */
312static int cipso_v4_cache_check(const unsigned char *key,
313 u32 key_len,
314 struct netlbl_lsm_secattr *secattr)
315{
316 u32 bkt;
317 struct cipso_v4_map_cache_entry *entry;
318 struct cipso_v4_map_cache_entry *prev_entry = NULL;
319 u32 hash;
320
321 if (!cipso_v4_cache_enabled)
322 return -ENOENT;
323
324 hash = cipso_v4_map_cache_hash(key, key_len);
Pavel Emelyanov5e0f8922008-05-13 23:23:55 -0700325 bkt = hash & (CIPSO_V4_CACHE_BUCKETS - 1);
Paul Moore609c92f2006-09-25 15:52:37 -0700326 spin_lock_bh(&cipso_v4_cache[bkt].lock);
Paul Moore446fda42006-08-03 16:48:06 -0700327 list_for_each_entry(entry, &cipso_v4_cache[bkt].list, list) {
328 if (entry->hash == hash &&
329 entry->key_len == key_len &&
330 memcmp(entry->key, key, key_len) == 0) {
331 entry->activity += 1;
paul.moore@hp.comffb733c2006-10-04 11:46:31 -0400332 atomic_inc(&entry->lsm_data->refcount);
333 secattr->cache = entry->lsm_data;
Paul Moore701a90b2006-11-17 17:38:46 -0500334 secattr->flags |= NETLBL_SECATTR_CACHE;
Paul Moore16efd452008-01-29 08:37:59 -0500335 secattr->type = NETLBL_NLTYPE_CIPSOV4;
Paul Moore446fda42006-08-03 16:48:06 -0700336 if (prev_entry == NULL) {
Paul Moore609c92f2006-09-25 15:52:37 -0700337 spin_unlock_bh(&cipso_v4_cache[bkt].lock);
Paul Moore446fda42006-08-03 16:48:06 -0700338 return 0;
339 }
340
341 if (prev_entry->activity > 0)
342 prev_entry->activity -= 1;
343 if (entry->activity > prev_entry->activity &&
344 entry->activity - prev_entry->activity >
345 CIPSO_V4_CACHE_REORDERLIMIT) {
346 __list_del(entry->list.prev, entry->list.next);
347 __list_add(&entry->list,
348 prev_entry->list.prev,
349 &prev_entry->list);
350 }
351
Paul Moore609c92f2006-09-25 15:52:37 -0700352 spin_unlock_bh(&cipso_v4_cache[bkt].lock);
Paul Moore446fda42006-08-03 16:48:06 -0700353 return 0;
354 }
355 prev_entry = entry;
356 }
Paul Moore609c92f2006-09-25 15:52:37 -0700357 spin_unlock_bh(&cipso_v4_cache[bkt].lock);
Paul Moore446fda42006-08-03 16:48:06 -0700358
359 return -ENOENT;
360}
361
362/**
363 * cipso_v4_cache_add - Add an entry to the CIPSO cache
364 * @skb: the packet
365 * @secattr: the packet's security attributes
366 *
367 * Description:
368 * Add a new entry into the CIPSO label mapping cache. Add the new entry to
369 * head of the cache bucket's list, if the cache bucket is out of room remove
370 * the last entry in the list first. It is important to note that there is
371 * currently no checking for duplicate keys. Returns zero on success,
372 * negative values on failure.
373 *
374 */
375int cipso_v4_cache_add(const struct sk_buff *skb,
376 const struct netlbl_lsm_secattr *secattr)
377{
378 int ret_val = -EPERM;
379 u32 bkt;
380 struct cipso_v4_map_cache_entry *entry = NULL;
381 struct cipso_v4_map_cache_entry *old_entry = NULL;
382 unsigned char *cipso_ptr;
383 u32 cipso_ptr_len;
384
385 if (!cipso_v4_cache_enabled || cipso_v4_cache_bucketsize <= 0)
386 return 0;
387
388 cipso_ptr = CIPSO_V4_OPTPTR(skb);
389 cipso_ptr_len = cipso_ptr[1];
390
391 entry = kzalloc(sizeof(*entry), GFP_ATOMIC);
392 if (entry == NULL)
393 return -ENOMEM;
Arnaldo Carvalho de Melofac5d732006-11-17 11:14:16 -0200394 entry->key = kmemdup(cipso_ptr, cipso_ptr_len, GFP_ATOMIC);
Paul Moore446fda42006-08-03 16:48:06 -0700395 if (entry->key == NULL) {
396 ret_val = -ENOMEM;
397 goto cache_add_failure;
398 }
Paul Moore446fda42006-08-03 16:48:06 -0700399 entry->key_len = cipso_ptr_len;
400 entry->hash = cipso_v4_map_cache_hash(cipso_ptr, cipso_ptr_len);
paul.moore@hp.comffb733c2006-10-04 11:46:31 -0400401 atomic_inc(&secattr->cache->refcount);
402 entry->lsm_data = secattr->cache;
Paul Moore446fda42006-08-03 16:48:06 -0700403
Pavel Emelyanov5e0f8922008-05-13 23:23:55 -0700404 bkt = entry->hash & (CIPSO_V4_CACHE_BUCKETS - 1);
Paul Moore609c92f2006-09-25 15:52:37 -0700405 spin_lock_bh(&cipso_v4_cache[bkt].lock);
Paul Moore446fda42006-08-03 16:48:06 -0700406 if (cipso_v4_cache[bkt].size < cipso_v4_cache_bucketsize) {
407 list_add(&entry->list, &cipso_v4_cache[bkt].list);
408 cipso_v4_cache[bkt].size += 1;
409 } else {
410 old_entry = list_entry(cipso_v4_cache[bkt].list.prev,
411 struct cipso_v4_map_cache_entry, list);
412 list_del(&old_entry->list);
413 list_add(&entry->list, &cipso_v4_cache[bkt].list);
414 cipso_v4_cache_entry_free(old_entry);
415 }
Paul Moore609c92f2006-09-25 15:52:37 -0700416 spin_unlock_bh(&cipso_v4_cache[bkt].lock);
Paul Moore446fda42006-08-03 16:48:06 -0700417
418 return 0;
419
420cache_add_failure:
421 if (entry)
422 cipso_v4_cache_entry_free(entry);
423 return ret_val;
424}
425
426/*
427 * DOI List Functions
428 */
429
430/**
431 * cipso_v4_doi_search - Searches for a DOI definition
432 * @doi: the DOI to search for
433 *
434 * Description:
435 * Search the DOI definition list for a DOI definition with a DOI value that
436 * matches @doi. The caller is responsibile for calling rcu_read_[un]lock().
437 * Returns a pointer to the DOI definition on success and NULL on failure.
438 */
439static struct cipso_v4_doi *cipso_v4_doi_search(u32 doi)
440{
441 struct cipso_v4_doi *iter;
442
443 list_for_each_entry_rcu(iter, &cipso_v4_doi_list, list)
Paul Mooreb1edeb12008-10-10 10:16:31 -0400444 if (iter->doi == doi && atomic_read(&iter->refcount))
Paul Moore446fda42006-08-03 16:48:06 -0700445 return iter;
446 return NULL;
447}
448
449/**
450 * cipso_v4_doi_add - Add a new DOI to the CIPSO protocol engine
451 * @doi_def: the DOI structure
452 *
453 * Description:
454 * The caller defines a new DOI for use by the CIPSO engine and calls this
455 * function to add it to the list of acceptable domains. The caller must
456 * ensure that the mapping table specified in @doi_def->map meets all of the
457 * requirements of the mapping type (see cipso_ipv4.h for details). Returns
458 * zero on success and non-zero on failure.
459 *
460 */
461int cipso_v4_doi_add(struct cipso_v4_doi *doi_def)
462{
Paul Moore6ce61a72006-11-17 17:38:48 -0500463 u32 iter;
464
Paul Moore446fda42006-08-03 16:48:06 -0700465 if (doi_def == NULL || doi_def->doi == CIPSO_V4_DOI_UNKNOWN)
466 return -EINVAL;
Paul Moore6ce61a72006-11-17 17:38:48 -0500467 for (iter = 0; iter < CIPSO_V4_TAG_MAXCNT; iter++) {
468 switch (doi_def->tags[iter]) {
469 case CIPSO_V4_TAG_RBITMAP:
470 break;
Paul Moore484b3662006-11-29 13:18:20 -0500471 case CIPSO_V4_TAG_RANGE:
472 if (doi_def->type != CIPSO_V4_MAP_PASS)
473 return -EINVAL;
474 break;
Paul Moore6ce61a72006-11-17 17:38:48 -0500475 case CIPSO_V4_TAG_INVALID:
476 if (iter == 0)
477 return -EINVAL;
478 break;
Paul Moore654bbc22006-11-29 13:18:19 -0500479 case CIPSO_V4_TAG_ENUM:
480 if (doi_def->type != CIPSO_V4_MAP_PASS)
481 return -EINVAL;
482 break;
Paul Moore15c45f72008-10-10 10:16:34 -0400483 case CIPSO_V4_TAG_LOCAL:
484 if (doi_def->type != CIPSO_V4_MAP_LOCAL)
485 return -EINVAL;
486 break;
Paul Moore6ce61a72006-11-17 17:38:48 -0500487 default:
488 return -EINVAL;
489 }
490 }
Paul Moore446fda42006-08-03 16:48:06 -0700491
Paul Mooreb1edeb12008-10-10 10:16:31 -0400492 atomic_set(&doi_def->refcount, 1);
Paul Moore446fda42006-08-03 16:48:06 -0700493
Paul Moore446fda42006-08-03 16:48:06 -0700494 spin_lock(&cipso_v4_doi_list_lock);
495 if (cipso_v4_doi_search(doi_def->doi) != NULL)
Paul Moore4be27002007-10-26 04:29:08 -0700496 goto doi_add_failure;
Paul Moore446fda42006-08-03 16:48:06 -0700497 list_add_tail_rcu(&doi_def->list, &cipso_v4_doi_list);
498 spin_unlock(&cipso_v4_doi_list_lock);
Paul Moore446fda42006-08-03 16:48:06 -0700499
500 return 0;
501
Paul Moore4be27002007-10-26 04:29:08 -0700502doi_add_failure:
Paul Moore446fda42006-08-03 16:48:06 -0700503 spin_unlock(&cipso_v4_doi_list_lock);
Paul Moore446fda42006-08-03 16:48:06 -0700504 return -EEXIST;
505}
506
507/**
Paul Mooreb1edeb12008-10-10 10:16:31 -0400508 * cipso_v4_doi_free - Frees a DOI definition
509 * @entry: the entry's RCU field
Paul Moore446fda42006-08-03 16:48:06 -0700510 *
511 * Description:
Paul Mooreb1edeb12008-10-10 10:16:31 -0400512 * This function frees all of the memory associated with a DOI definition.
Paul Moore446fda42006-08-03 16:48:06 -0700513 *
514 */
Paul Mooreb1edeb12008-10-10 10:16:31 -0400515void cipso_v4_doi_free(struct cipso_v4_doi *doi_def)
Paul Moore446fda42006-08-03 16:48:06 -0700516{
Paul Mooreb1edeb12008-10-10 10:16:31 -0400517 if (doi_def == NULL)
518 return;
Paul Moore446fda42006-08-03 16:48:06 -0700519
Paul Mooreb1edeb12008-10-10 10:16:31 -0400520 switch (doi_def->type) {
Paul Moore15c45f72008-10-10 10:16:34 -0400521 case CIPSO_V4_MAP_TRANS:
Paul Mooreb1edeb12008-10-10 10:16:31 -0400522 kfree(doi_def->map.std->lvl.cipso);
523 kfree(doi_def->map.std->lvl.local);
524 kfree(doi_def->map.std->cat.cipso);
525 kfree(doi_def->map.std->cat.local);
526 break;
Paul Moore446fda42006-08-03 16:48:06 -0700527 }
Paul Mooreb1edeb12008-10-10 10:16:31 -0400528 kfree(doi_def);
Paul Moore446fda42006-08-03 16:48:06 -0700529}
530
531/**
Paul Mooreb1edeb12008-10-10 10:16:31 -0400532 * cipso_v4_doi_free_rcu - Frees a DOI definition via the RCU pointer
533 * @entry: the entry's RCU field
534 *
535 * Description:
536 * This function is designed to be used as a callback to the call_rcu()
537 * function so that the memory allocated to the DOI definition can be released
538 * safely.
539 *
540 */
541static void cipso_v4_doi_free_rcu(struct rcu_head *entry)
542{
543 struct cipso_v4_doi *doi_def;
544
545 doi_def = container_of(entry, struct cipso_v4_doi, rcu);
546 cipso_v4_doi_free(doi_def);
547}
548
549/**
550 * cipso_v4_doi_remove - Remove an existing DOI from the CIPSO protocol engine
551 * @doi: the DOI value
552 * @audit_secid: the LSM secid to use in the audit message
553 *
554 * Description:
555 * Removes a DOI definition from the CIPSO engine. The NetLabel routines will
556 * be called to release their own LSM domain mappings as well as our own
557 * domain list. Returns zero on success and negative values on failure.
558 *
559 */
560int cipso_v4_doi_remove(u32 doi, struct netlbl_audit *audit_info)
561{
562 struct cipso_v4_doi *doi_def;
563
564 spin_lock(&cipso_v4_doi_list_lock);
565 doi_def = cipso_v4_doi_search(doi);
566 if (doi_def == NULL) {
567 spin_unlock(&cipso_v4_doi_list_lock);
568 return -ENOENT;
569 }
570 if (!atomic_dec_and_test(&doi_def->refcount)) {
571 spin_unlock(&cipso_v4_doi_list_lock);
572 return -EBUSY;
573 }
574 list_del_rcu(&doi_def->list);
575 spin_unlock(&cipso_v4_doi_list_lock);
576
577 cipso_v4_cache_invalidate();
578 call_rcu(&doi_def->rcu, cipso_v4_doi_free_rcu);
579
580 return 0;
581}
582
583/**
584 * cipso_v4_doi_getdef - Returns a reference to a valid DOI definition
Paul Moore446fda42006-08-03 16:48:06 -0700585 * @doi: the DOI value
586 *
587 * Description:
588 * Searches for a valid DOI definition and if one is found it is returned to
589 * the caller. Otherwise NULL is returned. The caller must ensure that
Paul Mooreb1edeb12008-10-10 10:16:31 -0400590 * rcu_read_lock() is held while accessing the returned definition and the DOI
591 * definition reference count is decremented when the caller is done.
Paul Moore446fda42006-08-03 16:48:06 -0700592 *
593 */
594struct cipso_v4_doi *cipso_v4_doi_getdef(u32 doi)
595{
Paul Mooreb1edeb12008-10-10 10:16:31 -0400596 struct cipso_v4_doi *doi_def;
597
598 rcu_read_lock();
599 doi_def = cipso_v4_doi_search(doi);
600 if (doi_def == NULL)
601 goto doi_getdef_return;
602 if (!atomic_inc_not_zero(&doi_def->refcount))
603 doi_def = NULL;
604
605doi_getdef_return:
606 rcu_read_unlock();
607 return doi_def;
608}
609
610/**
611 * cipso_v4_doi_putdef - Releases a reference for the given DOI definition
612 * @doi_def: the DOI definition
613 *
614 * Description:
615 * Releases a DOI definition reference obtained from cipso_v4_doi_getdef().
616 *
617 */
618void cipso_v4_doi_putdef(struct cipso_v4_doi *doi_def)
619{
620 if (doi_def == NULL)
621 return;
622
623 if (!atomic_dec_and_test(&doi_def->refcount))
624 return;
625 spin_lock(&cipso_v4_doi_list_lock);
626 list_del_rcu(&doi_def->list);
627 spin_unlock(&cipso_v4_doi_list_lock);
628
629 cipso_v4_cache_invalidate();
630 call_rcu(&doi_def->rcu, cipso_v4_doi_free_rcu);
Paul Moore446fda42006-08-03 16:48:06 -0700631}
632
633/**
Paul Moorefcd48282006-09-25 15:56:09 -0700634 * cipso_v4_doi_walk - Iterate through the DOI definitions
635 * @skip_cnt: skip past this number of DOI definitions, updated
636 * @callback: callback for each DOI definition
637 * @cb_arg: argument for the callback function
Paul Moore446fda42006-08-03 16:48:06 -0700638 *
639 * Description:
Paul Moorefcd48282006-09-25 15:56:09 -0700640 * Iterate over the DOI definition list, skipping the first @skip_cnt entries.
641 * For each entry call @callback, if @callback returns a negative value stop
642 * 'walking' through the list and return. Updates the value in @skip_cnt upon
643 * return. Returns zero on success, negative values on failure.
Paul Moore446fda42006-08-03 16:48:06 -0700644 *
645 */
Paul Moorefcd48282006-09-25 15:56:09 -0700646int cipso_v4_doi_walk(u32 *skip_cnt,
647 int (*callback) (struct cipso_v4_doi *doi_def, void *arg),
648 void *cb_arg)
Paul Moore446fda42006-08-03 16:48:06 -0700649{
Paul Moorefcd48282006-09-25 15:56:09 -0700650 int ret_val = -ENOENT;
Paul Moore446fda42006-08-03 16:48:06 -0700651 u32 doi_cnt = 0;
Paul Moorefcd48282006-09-25 15:56:09 -0700652 struct cipso_v4_doi *iter_doi;
Paul Moore446fda42006-08-03 16:48:06 -0700653
654 rcu_read_lock();
Paul Moorefcd48282006-09-25 15:56:09 -0700655 list_for_each_entry_rcu(iter_doi, &cipso_v4_doi_list, list)
Paul Mooreb1edeb12008-10-10 10:16:31 -0400656 if (atomic_read(&iter_doi->refcount) > 0) {
Paul Moorefcd48282006-09-25 15:56:09 -0700657 if (doi_cnt++ < *skip_cnt)
658 continue;
659 ret_val = callback(iter_doi, cb_arg);
660 if (ret_val < 0) {
661 doi_cnt--;
662 goto doi_walk_return;
Paul Moore446fda42006-08-03 16:48:06 -0700663 }
Paul Moorefcd48282006-09-25 15:56:09 -0700664 }
Paul Moore446fda42006-08-03 16:48:06 -0700665
Paul Moorefcd48282006-09-25 15:56:09 -0700666doi_walk_return:
Paul Moore446fda42006-08-03 16:48:06 -0700667 rcu_read_unlock();
Paul Moorefcd48282006-09-25 15:56:09 -0700668 *skip_cnt = doi_cnt;
669 return ret_val;
Paul Moore446fda42006-08-03 16:48:06 -0700670}
671
Paul Moore446fda42006-08-03 16:48:06 -0700672/*
673 * Label Mapping Functions
674 */
675
676/**
677 * cipso_v4_map_lvl_valid - Checks to see if the given level is understood
678 * @doi_def: the DOI definition
679 * @level: the level to check
680 *
681 * Description:
682 * Checks the given level against the given DOI definition and returns a
683 * negative value if the level does not have a valid mapping and a zero value
684 * if the level is defined by the DOI.
685 *
686 */
687static int cipso_v4_map_lvl_valid(const struct cipso_v4_doi *doi_def, u8 level)
688{
689 switch (doi_def->type) {
690 case CIPSO_V4_MAP_PASS:
691 return 0;
Paul Moore15c45f72008-10-10 10:16:34 -0400692 case CIPSO_V4_MAP_TRANS:
Paul Moore446fda42006-08-03 16:48:06 -0700693 if (doi_def->map.std->lvl.cipso[level] < CIPSO_V4_INV_LVL)
694 return 0;
695 break;
696 }
697
698 return -EFAULT;
699}
700
701/**
702 * cipso_v4_map_lvl_hton - Perform a level mapping from the host to the network
703 * @doi_def: the DOI definition
704 * @host_lvl: the host MLS level
705 * @net_lvl: the network/CIPSO MLS level
706 *
707 * Description:
708 * Perform a label mapping to translate a local MLS level to the correct
709 * CIPSO level using the given DOI definition. Returns zero on success,
710 * negative values otherwise.
711 *
712 */
713static int cipso_v4_map_lvl_hton(const struct cipso_v4_doi *doi_def,
714 u32 host_lvl,
715 u32 *net_lvl)
716{
717 switch (doi_def->type) {
718 case CIPSO_V4_MAP_PASS:
719 *net_lvl = host_lvl;
720 return 0;
Paul Moore15c45f72008-10-10 10:16:34 -0400721 case CIPSO_V4_MAP_TRANS:
Paul Moorec6387a82007-03-02 13:19:02 -0800722 if (host_lvl < doi_def->map.std->lvl.local_size &&
723 doi_def->map.std->lvl.local[host_lvl] < CIPSO_V4_INV_LVL) {
Paul Moore446fda42006-08-03 16:48:06 -0700724 *net_lvl = doi_def->map.std->lvl.local[host_lvl];
725 return 0;
726 }
Paul Moorec6387a82007-03-02 13:19:02 -0800727 return -EPERM;
Paul Moore446fda42006-08-03 16:48:06 -0700728 }
729
730 return -EINVAL;
731}
732
733/**
734 * cipso_v4_map_lvl_ntoh - Perform a level mapping from the network to the host
735 * @doi_def: the DOI definition
736 * @net_lvl: the network/CIPSO MLS level
737 * @host_lvl: the host MLS level
738 *
739 * Description:
740 * Perform a label mapping to translate a CIPSO level to the correct local MLS
741 * level using the given DOI definition. Returns zero on success, negative
742 * values otherwise.
743 *
744 */
745static int cipso_v4_map_lvl_ntoh(const struct cipso_v4_doi *doi_def,
746 u32 net_lvl,
747 u32 *host_lvl)
748{
749 struct cipso_v4_std_map_tbl *map_tbl;
750
751 switch (doi_def->type) {
752 case CIPSO_V4_MAP_PASS:
753 *host_lvl = net_lvl;
754 return 0;
Paul Moore15c45f72008-10-10 10:16:34 -0400755 case CIPSO_V4_MAP_TRANS:
Paul Moore446fda42006-08-03 16:48:06 -0700756 map_tbl = doi_def->map.std;
757 if (net_lvl < map_tbl->lvl.cipso_size &&
758 map_tbl->lvl.cipso[net_lvl] < CIPSO_V4_INV_LVL) {
759 *host_lvl = doi_def->map.std->lvl.cipso[net_lvl];
760 return 0;
761 }
Paul Moorec6387a82007-03-02 13:19:02 -0800762 return -EPERM;
Paul Moore446fda42006-08-03 16:48:06 -0700763 }
764
765 return -EINVAL;
766}
767
768/**
769 * cipso_v4_map_cat_rbm_valid - Checks to see if the category bitmap is valid
770 * @doi_def: the DOI definition
771 * @bitmap: category bitmap
772 * @bitmap_len: bitmap length in bytes
773 *
774 * Description:
775 * Checks the given category bitmap against the given DOI definition and
776 * returns a negative value if any of the categories in the bitmap do not have
777 * a valid mapping and a zero value if all of the categories are valid.
778 *
779 */
780static int cipso_v4_map_cat_rbm_valid(const struct cipso_v4_doi *doi_def,
781 const unsigned char *bitmap,
782 u32 bitmap_len)
783{
784 int cat = -1;
785 u32 bitmap_len_bits = bitmap_len * 8;
Paul Moore044a68e2006-10-11 19:10:47 -0400786 u32 cipso_cat_size;
787 u32 *cipso_array;
Paul Moore446fda42006-08-03 16:48:06 -0700788
789 switch (doi_def->type) {
790 case CIPSO_V4_MAP_PASS:
791 return 0;
Paul Moore15c45f72008-10-10 10:16:34 -0400792 case CIPSO_V4_MAP_TRANS:
Paul Moore044a68e2006-10-11 19:10:47 -0400793 cipso_cat_size = doi_def->map.std->cat.cipso_size;
794 cipso_array = doi_def->map.std->cat.cipso;
Paul Moore446fda42006-08-03 16:48:06 -0700795 for (;;) {
796 cat = cipso_v4_bitmap_walk(bitmap,
797 bitmap_len_bits,
798 cat + 1,
799 1);
800 if (cat < 0)
801 break;
802 if (cat >= cipso_cat_size ||
803 cipso_array[cat] >= CIPSO_V4_INV_CAT)
804 return -EFAULT;
805 }
806
807 if (cat == -1)
808 return 0;
809 break;
810 }
811
812 return -EFAULT;
813}
814
815/**
816 * cipso_v4_map_cat_rbm_hton - Perform a category mapping from host to network
817 * @doi_def: the DOI definition
Paul Moore02752762006-11-29 13:18:18 -0500818 * @secattr: the security attributes
Paul Moore446fda42006-08-03 16:48:06 -0700819 * @net_cat: the zero'd out category bitmap in network/CIPSO format
820 * @net_cat_len: the length of the CIPSO bitmap in bytes
821 *
822 * Description:
823 * Perform a label mapping to translate a local MLS category bitmap to the
824 * correct CIPSO bitmap using the given DOI definition. Returns the minimum
825 * size in bytes of the network bitmap on success, negative values otherwise.
826 *
827 */
828static int cipso_v4_map_cat_rbm_hton(const struct cipso_v4_doi *doi_def,
Paul Moore02752762006-11-29 13:18:18 -0500829 const struct netlbl_lsm_secattr *secattr,
Paul Moore446fda42006-08-03 16:48:06 -0700830 unsigned char *net_cat,
831 u32 net_cat_len)
832{
833 int host_spot = -1;
Paul Moore02752762006-11-29 13:18:18 -0500834 u32 net_spot = CIPSO_V4_INV_CAT;
Paul Moore446fda42006-08-03 16:48:06 -0700835 u32 net_spot_max = 0;
Paul Moore446fda42006-08-03 16:48:06 -0700836 u32 net_clen_bits = net_cat_len * 8;
Paul Moore02752762006-11-29 13:18:18 -0500837 u32 host_cat_size = 0;
838 u32 *host_cat_array = NULL;
Paul Moore446fda42006-08-03 16:48:06 -0700839
Paul Moore15c45f72008-10-10 10:16:34 -0400840 if (doi_def->type == CIPSO_V4_MAP_TRANS) {
Paul Moore044a68e2006-10-11 19:10:47 -0400841 host_cat_size = doi_def->map.std->cat.local_size;
842 host_cat_array = doi_def->map.std->cat.local;
Paul Moore02752762006-11-29 13:18:18 -0500843 }
844
845 for (;;) {
Paul Moore16efd452008-01-29 08:37:59 -0500846 host_spot = netlbl_secattr_catmap_walk(secattr->attr.mls.cat,
Paul Moore02752762006-11-29 13:18:18 -0500847 host_spot + 1);
848 if (host_spot < 0)
849 break;
850
851 switch (doi_def->type) {
852 case CIPSO_V4_MAP_PASS:
853 net_spot = host_spot;
854 break;
Paul Moore15c45f72008-10-10 10:16:34 -0400855 case CIPSO_V4_MAP_TRANS:
Paul Moore446fda42006-08-03 16:48:06 -0700856 if (host_spot >= host_cat_size)
857 return -EPERM;
Paul Moore446fda42006-08-03 16:48:06 -0700858 net_spot = host_cat_array[host_spot];
Paul Moore9fade4b2006-11-17 17:38:50 -0500859 if (net_spot >= CIPSO_V4_INV_CAT)
860 return -EPERM;
Paul Moore02752762006-11-29 13:18:18 -0500861 break;
Paul Moore446fda42006-08-03 16:48:06 -0700862 }
Paul Moore02752762006-11-29 13:18:18 -0500863 if (net_spot >= net_clen_bits)
864 return -ENOSPC;
865 cipso_v4_bitmap_setbit(net_cat, net_spot, 1);
Paul Moore446fda42006-08-03 16:48:06 -0700866
Paul Moore02752762006-11-29 13:18:18 -0500867 if (net_spot > net_spot_max)
868 net_spot_max = net_spot;
Paul Moore446fda42006-08-03 16:48:06 -0700869 }
870
Paul Moore02752762006-11-29 13:18:18 -0500871 if (++net_spot_max % 8)
872 return net_spot_max / 8 + 1;
873 return net_spot_max / 8;
Paul Moore446fda42006-08-03 16:48:06 -0700874}
875
876/**
877 * cipso_v4_map_cat_rbm_ntoh - Perform a category mapping from network to host
878 * @doi_def: the DOI definition
879 * @net_cat: the category bitmap in network/CIPSO format
880 * @net_cat_len: the length of the CIPSO bitmap in bytes
Paul Moore02752762006-11-29 13:18:18 -0500881 * @secattr: the security attributes
Paul Moore446fda42006-08-03 16:48:06 -0700882 *
883 * Description:
884 * Perform a label mapping to translate a CIPSO bitmap to the correct local
Paul Moore02752762006-11-29 13:18:18 -0500885 * MLS category bitmap using the given DOI definition. Returns zero on
886 * success, negative values on failure.
Paul Moore446fda42006-08-03 16:48:06 -0700887 *
888 */
889static int cipso_v4_map_cat_rbm_ntoh(const struct cipso_v4_doi *doi_def,
890 const unsigned char *net_cat,
891 u32 net_cat_len,
Paul Moore02752762006-11-29 13:18:18 -0500892 struct netlbl_lsm_secattr *secattr)
Paul Moore446fda42006-08-03 16:48:06 -0700893{
Paul Moore02752762006-11-29 13:18:18 -0500894 int ret_val;
Paul Moore446fda42006-08-03 16:48:06 -0700895 int net_spot = -1;
Paul Moore02752762006-11-29 13:18:18 -0500896 u32 host_spot = CIPSO_V4_INV_CAT;
Paul Moore446fda42006-08-03 16:48:06 -0700897 u32 net_clen_bits = net_cat_len * 8;
Paul Moore02752762006-11-29 13:18:18 -0500898 u32 net_cat_size = 0;
899 u32 *net_cat_array = NULL;
Paul Moore446fda42006-08-03 16:48:06 -0700900
Paul Moore15c45f72008-10-10 10:16:34 -0400901 if (doi_def->type == CIPSO_V4_MAP_TRANS) {
Paul Moore044a68e2006-10-11 19:10:47 -0400902 net_cat_size = doi_def->map.std->cat.cipso_size;
903 net_cat_array = doi_def->map.std->cat.cipso;
Paul Moore02752762006-11-29 13:18:18 -0500904 }
Paul Moore446fda42006-08-03 16:48:06 -0700905
Paul Moore02752762006-11-29 13:18:18 -0500906 for (;;) {
907 net_spot = cipso_v4_bitmap_walk(net_cat,
908 net_clen_bits,
909 net_spot + 1,
910 1);
911 if (net_spot < 0) {
912 if (net_spot == -2)
913 return -EFAULT;
914 return 0;
915 }
916
917 switch (doi_def->type) {
918 case CIPSO_V4_MAP_PASS:
919 host_spot = net_spot;
920 break;
Paul Moore15c45f72008-10-10 10:16:34 -0400921 case CIPSO_V4_MAP_TRANS:
Paul Moore02752762006-11-29 13:18:18 -0500922 if (net_spot >= net_cat_size)
923 return -EPERM;
Paul Moore446fda42006-08-03 16:48:06 -0700924 host_spot = net_cat_array[net_spot];
Paul Moore9fade4b2006-11-17 17:38:50 -0500925 if (host_spot >= CIPSO_V4_INV_CAT)
926 return -EPERM;
Paul Moore02752762006-11-29 13:18:18 -0500927 break;
Paul Moore446fda42006-08-03 16:48:06 -0700928 }
Paul Moore16efd452008-01-29 08:37:59 -0500929 ret_val = netlbl_secattr_catmap_setbit(secattr->attr.mls.cat,
Paul Moore02752762006-11-29 13:18:18 -0500930 host_spot,
931 GFP_ATOMIC);
932 if (ret_val != 0)
933 return ret_val;
Paul Moore446fda42006-08-03 16:48:06 -0700934 }
935
936 return -EINVAL;
937}
938
Paul Moore654bbc22006-11-29 13:18:19 -0500939/**
940 * cipso_v4_map_cat_enum_valid - Checks to see if the categories are valid
941 * @doi_def: the DOI definition
942 * @enumcat: category list
943 * @enumcat_len: length of the category list in bytes
944 *
945 * Description:
946 * Checks the given categories against the given DOI definition and returns a
947 * negative value if any of the categories do not have a valid mapping and a
948 * zero value if all of the categories are valid.
949 *
950 */
951static int cipso_v4_map_cat_enum_valid(const struct cipso_v4_doi *doi_def,
952 const unsigned char *enumcat,
953 u32 enumcat_len)
954{
955 u16 cat;
956 int cat_prev = -1;
957 u32 iter;
958
959 if (doi_def->type != CIPSO_V4_MAP_PASS || enumcat_len & 0x01)
960 return -EFAULT;
961
962 for (iter = 0; iter < enumcat_len; iter += 2) {
Harvey Harrisond3e2ce32008-05-02 16:26:16 -0700963 cat = get_unaligned_be16(&enumcat[iter]);
Paul Moore654bbc22006-11-29 13:18:19 -0500964 if (cat <= cat_prev)
965 return -EFAULT;
966 cat_prev = cat;
967 }
968
969 return 0;
970}
971
972/**
973 * cipso_v4_map_cat_enum_hton - Perform a category mapping from host to network
974 * @doi_def: the DOI definition
975 * @secattr: the security attributes
976 * @net_cat: the zero'd out category list in network/CIPSO format
977 * @net_cat_len: the length of the CIPSO category list in bytes
978 *
979 * Description:
980 * Perform a label mapping to translate a local MLS category bitmap to the
981 * correct CIPSO category list using the given DOI definition. Returns the
982 * size in bytes of the network category bitmap on success, negative values
983 * otherwise.
984 *
985 */
986static int cipso_v4_map_cat_enum_hton(const struct cipso_v4_doi *doi_def,
987 const struct netlbl_lsm_secattr *secattr,
988 unsigned char *net_cat,
989 u32 net_cat_len)
990{
991 int cat = -1;
992 u32 cat_iter = 0;
993
994 for (;;) {
Paul Moore16efd452008-01-29 08:37:59 -0500995 cat = netlbl_secattr_catmap_walk(secattr->attr.mls.cat,
996 cat + 1);
Paul Moore654bbc22006-11-29 13:18:19 -0500997 if (cat < 0)
998 break;
999 if ((cat_iter + 2) > net_cat_len)
1000 return -ENOSPC;
1001
1002 *((__be16 *)&net_cat[cat_iter]) = htons(cat);
1003 cat_iter += 2;
1004 }
1005
1006 return cat_iter;
1007}
1008
1009/**
1010 * cipso_v4_map_cat_enum_ntoh - Perform a category mapping from network to host
1011 * @doi_def: the DOI definition
1012 * @net_cat: the category list in network/CIPSO format
1013 * @net_cat_len: the length of the CIPSO bitmap in bytes
1014 * @secattr: the security attributes
1015 *
1016 * Description:
1017 * Perform a label mapping to translate a CIPSO category list to the correct
1018 * local MLS category bitmap using the given DOI definition. Returns zero on
1019 * success, negative values on failure.
1020 *
1021 */
1022static int cipso_v4_map_cat_enum_ntoh(const struct cipso_v4_doi *doi_def,
1023 const unsigned char *net_cat,
1024 u32 net_cat_len,
1025 struct netlbl_lsm_secattr *secattr)
1026{
1027 int ret_val;
1028 u32 iter;
1029
1030 for (iter = 0; iter < net_cat_len; iter += 2) {
Paul Moore16efd452008-01-29 08:37:59 -05001031 ret_val = netlbl_secattr_catmap_setbit(secattr->attr.mls.cat,
Harvey Harrisond3e2ce32008-05-02 16:26:16 -07001032 get_unaligned_be16(&net_cat[iter]),
Paul Moore50e5d352007-06-07 18:38:14 -07001033 GFP_ATOMIC);
Paul Moore654bbc22006-11-29 13:18:19 -05001034 if (ret_val != 0)
1035 return ret_val;
1036 }
1037
1038 return 0;
1039}
1040
Paul Moore484b3662006-11-29 13:18:20 -05001041/**
1042 * cipso_v4_map_cat_rng_valid - Checks to see if the categories are valid
1043 * @doi_def: the DOI definition
1044 * @rngcat: category list
1045 * @rngcat_len: length of the category list in bytes
1046 *
1047 * Description:
1048 * Checks the given categories against the given DOI definition and returns a
1049 * negative value if any of the categories do not have a valid mapping and a
1050 * zero value if all of the categories are valid.
1051 *
1052 */
1053static int cipso_v4_map_cat_rng_valid(const struct cipso_v4_doi *doi_def,
1054 const unsigned char *rngcat,
1055 u32 rngcat_len)
1056{
1057 u16 cat_high;
1058 u16 cat_low;
1059 u32 cat_prev = CIPSO_V4_MAX_REM_CATS + 1;
1060 u32 iter;
1061
1062 if (doi_def->type != CIPSO_V4_MAP_PASS || rngcat_len & 0x01)
1063 return -EFAULT;
1064
1065 for (iter = 0; iter < rngcat_len; iter += 4) {
Harvey Harrisond3e2ce32008-05-02 16:26:16 -07001066 cat_high = get_unaligned_be16(&rngcat[iter]);
Paul Moore484b3662006-11-29 13:18:20 -05001067 if ((iter + 4) <= rngcat_len)
Harvey Harrisond3e2ce32008-05-02 16:26:16 -07001068 cat_low = get_unaligned_be16(&rngcat[iter + 2]);
Paul Moore484b3662006-11-29 13:18:20 -05001069 else
1070 cat_low = 0;
1071
1072 if (cat_high > cat_prev)
1073 return -EFAULT;
1074
1075 cat_prev = cat_low;
1076 }
1077
1078 return 0;
1079}
1080
1081/**
1082 * cipso_v4_map_cat_rng_hton - Perform a category mapping from host to network
1083 * @doi_def: the DOI definition
1084 * @secattr: the security attributes
1085 * @net_cat: the zero'd out category list in network/CIPSO format
1086 * @net_cat_len: the length of the CIPSO category list in bytes
1087 *
1088 * Description:
1089 * Perform a label mapping to translate a local MLS category bitmap to the
1090 * correct CIPSO category list using the given DOI definition. Returns the
1091 * size in bytes of the network category bitmap on success, negative values
1092 * otherwise.
1093 *
1094 */
1095static int cipso_v4_map_cat_rng_hton(const struct cipso_v4_doi *doi_def,
1096 const struct netlbl_lsm_secattr *secattr,
1097 unsigned char *net_cat,
1098 u32 net_cat_len)
1099{
Paul Moore484b3662006-11-29 13:18:20 -05001100 int iter = -1;
Paul Mooref998e8c2007-02-28 15:14:20 -05001101 u16 array[CIPSO_V4_TAG_RNG_CAT_MAX * 2];
Paul Moore484b3662006-11-29 13:18:20 -05001102 u32 array_cnt = 0;
1103 u32 cat_size = 0;
1104
Paul Mooref998e8c2007-02-28 15:14:20 -05001105 /* make sure we don't overflow the 'array[]' variable */
Paul Moore128c6b62007-02-28 15:14:21 -05001106 if (net_cat_len >
1107 (CIPSO_V4_OPT_LEN_MAX - CIPSO_V4_HDR_LEN - CIPSO_V4_TAG_RNG_BLEN))
1108 return -ENOSPC;
Paul Moore484b3662006-11-29 13:18:20 -05001109
1110 for (;;) {
Paul Moore16efd452008-01-29 08:37:59 -05001111 iter = netlbl_secattr_catmap_walk(secattr->attr.mls.cat,
1112 iter + 1);
Paul Moore484b3662006-11-29 13:18:20 -05001113 if (iter < 0)
1114 break;
1115 cat_size += (iter == 0 ? 0 : sizeof(u16));
1116 if (cat_size > net_cat_len)
1117 return -ENOSPC;
1118 array[array_cnt++] = iter;
1119
Paul Moore16efd452008-01-29 08:37:59 -05001120 iter = netlbl_secattr_catmap_walk_rng(secattr->attr.mls.cat,
1121 iter);
Paul Moore484b3662006-11-29 13:18:20 -05001122 if (iter < 0)
1123 return -EFAULT;
1124 cat_size += sizeof(u16);
1125 if (cat_size > net_cat_len)
1126 return -ENOSPC;
1127 array[array_cnt++] = iter;
1128 }
1129
1130 for (iter = 0; array_cnt > 0;) {
1131 *((__be16 *)&net_cat[iter]) = htons(array[--array_cnt]);
1132 iter += 2;
1133 array_cnt--;
1134 if (array[array_cnt] != 0) {
1135 *((__be16 *)&net_cat[iter]) = htons(array[array_cnt]);
1136 iter += 2;
1137 }
1138 }
1139
1140 return cat_size;
1141}
1142
1143/**
1144 * cipso_v4_map_cat_rng_ntoh - Perform a category mapping from network to host
1145 * @doi_def: the DOI definition
1146 * @net_cat: the category list in network/CIPSO format
1147 * @net_cat_len: the length of the CIPSO bitmap in bytes
1148 * @secattr: the security attributes
1149 *
1150 * Description:
1151 * Perform a label mapping to translate a CIPSO category list to the correct
1152 * local MLS category bitmap using the given DOI definition. Returns zero on
1153 * success, negative values on failure.
1154 *
1155 */
1156static int cipso_v4_map_cat_rng_ntoh(const struct cipso_v4_doi *doi_def,
1157 const unsigned char *net_cat,
1158 u32 net_cat_len,
1159 struct netlbl_lsm_secattr *secattr)
1160{
1161 int ret_val;
1162 u32 net_iter;
1163 u16 cat_low;
1164 u16 cat_high;
1165
Stephen Hemminger132adf52007-03-08 20:44:43 -08001166 for (net_iter = 0; net_iter < net_cat_len; net_iter += 4) {
Harvey Harrisond3e2ce32008-05-02 16:26:16 -07001167 cat_high = get_unaligned_be16(&net_cat[net_iter]);
Paul Moore484b3662006-11-29 13:18:20 -05001168 if ((net_iter + 4) <= net_cat_len)
Harvey Harrisond3e2ce32008-05-02 16:26:16 -07001169 cat_low = get_unaligned_be16(&net_cat[net_iter + 2]);
Paul Moore484b3662006-11-29 13:18:20 -05001170 else
1171 cat_low = 0;
1172
Paul Moore16efd452008-01-29 08:37:59 -05001173 ret_val = netlbl_secattr_catmap_setrng(secattr->attr.mls.cat,
Paul Moore484b3662006-11-29 13:18:20 -05001174 cat_low,
1175 cat_high,
1176 GFP_ATOMIC);
1177 if (ret_val != 0)
1178 return ret_val;
1179 }
1180
1181 return 0;
1182}
1183
Paul Moore446fda42006-08-03 16:48:06 -07001184/*
1185 * Protocol Handling Functions
1186 */
1187
Paul Moore446fda42006-08-03 16:48:06 -07001188/**
1189 * cipso_v4_gentag_hdr - Generate a CIPSO option header
1190 * @doi_def: the DOI definition
Paul Moore91b1ed02006-11-17 17:38:49 -05001191 * @len: the total tag length in bytes, not including this header
Paul Moore446fda42006-08-03 16:48:06 -07001192 * @buf: the CIPSO option buffer
1193 *
1194 * Description:
Paul Moore91b1ed02006-11-17 17:38:49 -05001195 * Write a CIPSO header into the beginning of @buffer.
Paul Moore446fda42006-08-03 16:48:06 -07001196 *
1197 */
Paul Moore91b1ed02006-11-17 17:38:49 -05001198static void cipso_v4_gentag_hdr(const struct cipso_v4_doi *doi_def,
1199 unsigned char *buf,
1200 u32 len)
Paul Moore446fda42006-08-03 16:48:06 -07001201{
Paul Moore446fda42006-08-03 16:48:06 -07001202 buf[0] = IPOPT_CIPSO;
1203 buf[1] = CIPSO_V4_HDR_LEN + len;
Al Viro714e85b2006-11-14 20:51:49 -08001204 *(__be32 *)&buf[2] = htonl(doi_def->doi);
Paul Moore446fda42006-08-03 16:48:06 -07001205}
1206
Paul Moore446fda42006-08-03 16:48:06 -07001207/**
1208 * cipso_v4_gentag_rbm - Generate a CIPSO restricted bitmap tag (type #1)
1209 * @doi_def: the DOI definition
1210 * @secattr: the security attributes
1211 * @buffer: the option buffer
1212 * @buffer_len: length of buffer in bytes
1213 *
1214 * Description:
1215 * Generate a CIPSO option using the restricted bitmap tag, tag type #1. The
1216 * actual buffer length may be larger than the indicated size due to
Paul Moore91b1ed02006-11-17 17:38:49 -05001217 * translation between host and network category bitmaps. Returns the size of
1218 * the tag on success, negative values on failure.
Paul Moore446fda42006-08-03 16:48:06 -07001219 *
1220 */
1221static int cipso_v4_gentag_rbm(const struct cipso_v4_doi *doi_def,
1222 const struct netlbl_lsm_secattr *secattr,
Paul Moore91b1ed02006-11-17 17:38:49 -05001223 unsigned char *buffer,
1224 u32 buffer_len)
Paul Moore446fda42006-08-03 16:48:06 -07001225{
Paul Moore701a90b2006-11-17 17:38:46 -05001226 int ret_val;
Paul Moore91b1ed02006-11-17 17:38:49 -05001227 u32 tag_len;
Paul Moore446fda42006-08-03 16:48:06 -07001228 u32 level;
1229
Paul Moore701a90b2006-11-17 17:38:46 -05001230 if ((secattr->flags & NETLBL_SECATTR_MLS_LVL) == 0)
1231 return -EPERM;
1232
Paul Moore16efd452008-01-29 08:37:59 -05001233 ret_val = cipso_v4_map_lvl_hton(doi_def,
1234 secattr->attr.mls.lvl,
1235 &level);
Paul Moore91b1ed02006-11-17 17:38:49 -05001236 if (ret_val != 0)
1237 return ret_val;
Paul Moore446fda42006-08-03 16:48:06 -07001238
Paul Moore91b1ed02006-11-17 17:38:49 -05001239 if (secattr->flags & NETLBL_SECATTR_MLS_CAT) {
Paul Moore446fda42006-08-03 16:48:06 -07001240 ret_val = cipso_v4_map_cat_rbm_hton(doi_def,
Paul Moore02752762006-11-29 13:18:18 -05001241 secattr,
Paul Moore91b1ed02006-11-17 17:38:49 -05001242 &buffer[4],
1243 buffer_len - 4);
Paul Moore446fda42006-08-03 16:48:06 -07001244 if (ret_val < 0)
Paul Moore91b1ed02006-11-17 17:38:49 -05001245 return ret_val;
Paul Moore446fda42006-08-03 16:48:06 -07001246
1247 /* This will send packets using the "optimized" format when
1248 * possibile as specified in section 3.4.2.6 of the
1249 * CIPSO draft. */
Paul Moore701a90b2006-11-17 17:38:46 -05001250 if (cipso_v4_rbm_optfmt && ret_val > 0 && ret_val <= 10)
Paul Moore91b1ed02006-11-17 17:38:49 -05001251 tag_len = 14;
Paul Moore701a90b2006-11-17 17:38:46 -05001252 else
Paul Moore91b1ed02006-11-17 17:38:49 -05001253 tag_len = 4 + ret_val;
1254 } else
1255 tag_len = 4;
Paul Moore446fda42006-08-03 16:48:06 -07001256
Paul Moore15c45f72008-10-10 10:16:34 -04001257 buffer[0] = CIPSO_V4_TAG_RBITMAP;
Paul Moore91b1ed02006-11-17 17:38:49 -05001258 buffer[1] = tag_len;
1259 buffer[3] = level;
Paul Moore446fda42006-08-03 16:48:06 -07001260
Paul Moore91b1ed02006-11-17 17:38:49 -05001261 return tag_len;
Paul Moore446fda42006-08-03 16:48:06 -07001262}
1263
1264/**
1265 * cipso_v4_parsetag_rbm - Parse a CIPSO restricted bitmap tag
1266 * @doi_def: the DOI definition
1267 * @tag: the CIPSO tag
1268 * @secattr: the security attributes
1269 *
1270 * Description:
1271 * Parse a CIPSO restricted bitmap tag (tag type #1) and return the security
1272 * attributes in @secattr. Return zero on success, negatives values on
1273 * failure.
1274 *
1275 */
1276static int cipso_v4_parsetag_rbm(const struct cipso_v4_doi *doi_def,
1277 const unsigned char *tag,
1278 struct netlbl_lsm_secattr *secattr)
1279{
1280 int ret_val;
1281 u8 tag_len = tag[1];
1282 u32 level;
1283
1284 ret_val = cipso_v4_map_lvl_ntoh(doi_def, tag[3], &level);
1285 if (ret_val != 0)
1286 return ret_val;
Paul Moore16efd452008-01-29 08:37:59 -05001287 secattr->attr.mls.lvl = level;
Paul Moore701a90b2006-11-17 17:38:46 -05001288 secattr->flags |= NETLBL_SECATTR_MLS_LVL;
Paul Moore446fda42006-08-03 16:48:06 -07001289
1290 if (tag_len > 4) {
Paul Moore16efd452008-01-29 08:37:59 -05001291 secattr->attr.mls.cat =
1292 netlbl_secattr_catmap_alloc(GFP_ATOMIC);
1293 if (secattr->attr.mls.cat == NULL)
Paul Moore446fda42006-08-03 16:48:06 -07001294 return -ENOMEM;
1295
1296 ret_val = cipso_v4_map_cat_rbm_ntoh(doi_def,
1297 &tag[4],
1298 tag_len - 4,
Paul Moore02752762006-11-29 13:18:18 -05001299 secattr);
1300 if (ret_val != 0) {
Paul Moore16efd452008-01-29 08:37:59 -05001301 netlbl_secattr_catmap_free(secattr->attr.mls.cat);
Paul Moore446fda42006-08-03 16:48:06 -07001302 return ret_val;
1303 }
Paul Moore02752762006-11-29 13:18:18 -05001304
1305 secattr->flags |= NETLBL_SECATTR_MLS_CAT;
Paul Moore446fda42006-08-03 16:48:06 -07001306 }
1307
1308 return 0;
1309}
1310
1311/**
Paul Moore654bbc22006-11-29 13:18:19 -05001312 * cipso_v4_gentag_enum - Generate a CIPSO enumerated tag (type #2)
1313 * @doi_def: the DOI definition
1314 * @secattr: the security attributes
1315 * @buffer: the option buffer
1316 * @buffer_len: length of buffer in bytes
1317 *
1318 * Description:
1319 * Generate a CIPSO option using the enumerated tag, tag type #2. Returns the
1320 * size of the tag on success, negative values on failure.
1321 *
1322 */
1323static int cipso_v4_gentag_enum(const struct cipso_v4_doi *doi_def,
1324 const struct netlbl_lsm_secattr *secattr,
1325 unsigned char *buffer,
1326 u32 buffer_len)
1327{
1328 int ret_val;
1329 u32 tag_len;
1330 u32 level;
1331
1332 if (!(secattr->flags & NETLBL_SECATTR_MLS_LVL))
1333 return -EPERM;
1334
Paul Moore16efd452008-01-29 08:37:59 -05001335 ret_val = cipso_v4_map_lvl_hton(doi_def,
1336 secattr->attr.mls.lvl,
1337 &level);
Paul Moore654bbc22006-11-29 13:18:19 -05001338 if (ret_val != 0)
1339 return ret_val;
1340
1341 if (secattr->flags & NETLBL_SECATTR_MLS_CAT) {
1342 ret_val = cipso_v4_map_cat_enum_hton(doi_def,
1343 secattr,
1344 &buffer[4],
1345 buffer_len - 4);
1346 if (ret_val < 0)
1347 return ret_val;
1348
1349 tag_len = 4 + ret_val;
1350 } else
1351 tag_len = 4;
1352
Paul Moore15c45f72008-10-10 10:16:34 -04001353 buffer[0] = CIPSO_V4_TAG_ENUM;
Paul Moore654bbc22006-11-29 13:18:19 -05001354 buffer[1] = tag_len;
1355 buffer[3] = level;
1356
1357 return tag_len;
1358}
1359
1360/**
1361 * cipso_v4_parsetag_enum - Parse a CIPSO enumerated tag
1362 * @doi_def: the DOI definition
1363 * @tag: the CIPSO tag
1364 * @secattr: the security attributes
1365 *
1366 * Description:
1367 * Parse a CIPSO enumerated tag (tag type #2) and return the security
1368 * attributes in @secattr. Return zero on success, negatives values on
1369 * failure.
1370 *
1371 */
1372static int cipso_v4_parsetag_enum(const struct cipso_v4_doi *doi_def,
1373 const unsigned char *tag,
1374 struct netlbl_lsm_secattr *secattr)
1375{
1376 int ret_val;
1377 u8 tag_len = tag[1];
1378 u32 level;
1379
1380 ret_val = cipso_v4_map_lvl_ntoh(doi_def, tag[3], &level);
1381 if (ret_val != 0)
1382 return ret_val;
Paul Moore16efd452008-01-29 08:37:59 -05001383 secattr->attr.mls.lvl = level;
Paul Moore654bbc22006-11-29 13:18:19 -05001384 secattr->flags |= NETLBL_SECATTR_MLS_LVL;
1385
1386 if (tag_len > 4) {
Paul Moore16efd452008-01-29 08:37:59 -05001387 secattr->attr.mls.cat =
1388 netlbl_secattr_catmap_alloc(GFP_ATOMIC);
1389 if (secattr->attr.mls.cat == NULL)
Paul Moore654bbc22006-11-29 13:18:19 -05001390 return -ENOMEM;
1391
1392 ret_val = cipso_v4_map_cat_enum_ntoh(doi_def,
1393 &tag[4],
1394 tag_len - 4,
1395 secattr);
1396 if (ret_val != 0) {
Paul Moore16efd452008-01-29 08:37:59 -05001397 netlbl_secattr_catmap_free(secattr->attr.mls.cat);
Paul Moore654bbc22006-11-29 13:18:19 -05001398 return ret_val;
1399 }
1400
1401 secattr->flags |= NETLBL_SECATTR_MLS_CAT;
1402 }
1403
1404 return 0;
1405}
1406
1407/**
Paul Moore484b3662006-11-29 13:18:20 -05001408 * cipso_v4_gentag_rng - Generate a CIPSO ranged tag (type #5)
1409 * @doi_def: the DOI definition
1410 * @secattr: the security attributes
1411 * @buffer: the option buffer
1412 * @buffer_len: length of buffer in bytes
1413 *
1414 * Description:
1415 * Generate a CIPSO option using the ranged tag, tag type #5. Returns the
1416 * size of the tag on success, negative values on failure.
1417 *
1418 */
1419static int cipso_v4_gentag_rng(const struct cipso_v4_doi *doi_def,
1420 const struct netlbl_lsm_secattr *secattr,
1421 unsigned char *buffer,
1422 u32 buffer_len)
1423{
1424 int ret_val;
1425 u32 tag_len;
1426 u32 level;
1427
1428 if (!(secattr->flags & NETLBL_SECATTR_MLS_LVL))
1429 return -EPERM;
1430
Paul Moore16efd452008-01-29 08:37:59 -05001431 ret_val = cipso_v4_map_lvl_hton(doi_def,
1432 secattr->attr.mls.lvl,
1433 &level);
Paul Moore484b3662006-11-29 13:18:20 -05001434 if (ret_val != 0)
1435 return ret_val;
1436
1437 if (secattr->flags & NETLBL_SECATTR_MLS_CAT) {
1438 ret_val = cipso_v4_map_cat_rng_hton(doi_def,
1439 secattr,
1440 &buffer[4],
1441 buffer_len - 4);
1442 if (ret_val < 0)
1443 return ret_val;
1444
1445 tag_len = 4 + ret_val;
1446 } else
1447 tag_len = 4;
1448
Paul Moore15c45f72008-10-10 10:16:34 -04001449 buffer[0] = CIPSO_V4_TAG_RANGE;
Paul Moore484b3662006-11-29 13:18:20 -05001450 buffer[1] = tag_len;
1451 buffer[3] = level;
1452
1453 return tag_len;
1454}
1455
1456/**
1457 * cipso_v4_parsetag_rng - Parse a CIPSO ranged tag
1458 * @doi_def: the DOI definition
1459 * @tag: the CIPSO tag
1460 * @secattr: the security attributes
1461 *
1462 * Description:
1463 * Parse a CIPSO ranged tag (tag type #5) and return the security attributes
1464 * in @secattr. Return zero on success, negatives values on failure.
1465 *
1466 */
1467static int cipso_v4_parsetag_rng(const struct cipso_v4_doi *doi_def,
1468 const unsigned char *tag,
1469 struct netlbl_lsm_secattr *secattr)
1470{
1471 int ret_val;
1472 u8 tag_len = tag[1];
1473 u32 level;
1474
1475 ret_val = cipso_v4_map_lvl_ntoh(doi_def, tag[3], &level);
1476 if (ret_val != 0)
1477 return ret_val;
Paul Moore16efd452008-01-29 08:37:59 -05001478 secattr->attr.mls.lvl = level;
Paul Moore484b3662006-11-29 13:18:20 -05001479 secattr->flags |= NETLBL_SECATTR_MLS_LVL;
1480
1481 if (tag_len > 4) {
Paul Moore16efd452008-01-29 08:37:59 -05001482 secattr->attr.mls.cat =
1483 netlbl_secattr_catmap_alloc(GFP_ATOMIC);
1484 if (secattr->attr.mls.cat == NULL)
Paul Moore484b3662006-11-29 13:18:20 -05001485 return -ENOMEM;
1486
1487 ret_val = cipso_v4_map_cat_rng_ntoh(doi_def,
1488 &tag[4],
1489 tag_len - 4,
1490 secattr);
1491 if (ret_val != 0) {
Paul Moore16efd452008-01-29 08:37:59 -05001492 netlbl_secattr_catmap_free(secattr->attr.mls.cat);
Paul Moore484b3662006-11-29 13:18:20 -05001493 return ret_val;
1494 }
1495
1496 secattr->flags |= NETLBL_SECATTR_MLS_CAT;
1497 }
1498
1499 return 0;
1500}
1501
1502/**
Paul Moore15c45f72008-10-10 10:16:34 -04001503 * cipso_v4_gentag_loc - Generate a CIPSO local tag (non-standard)
1504 * @doi_def: the DOI definition
1505 * @secattr: the security attributes
1506 * @buffer: the option buffer
1507 * @buffer_len: length of buffer in bytes
1508 *
1509 * Description:
1510 * Generate a CIPSO option using the local tag. Returns the size of the tag
1511 * on success, negative values on failure.
1512 *
1513 */
1514static int cipso_v4_gentag_loc(const struct cipso_v4_doi *doi_def,
1515 const struct netlbl_lsm_secattr *secattr,
1516 unsigned char *buffer,
1517 u32 buffer_len)
1518{
1519 if (!(secattr->flags & NETLBL_SECATTR_SECID))
1520 return -EPERM;
1521
1522 buffer[0] = CIPSO_V4_TAG_LOCAL;
1523 buffer[1] = CIPSO_V4_TAG_LOC_BLEN;
1524 *(u32 *)&buffer[2] = secattr->attr.secid;
1525
1526 return CIPSO_V4_TAG_LOC_BLEN;
1527}
1528
1529/**
1530 * cipso_v4_parsetag_loc - Parse a CIPSO local tag
1531 * @doi_def: the DOI definition
1532 * @tag: the CIPSO tag
1533 * @secattr: the security attributes
1534 *
1535 * Description:
1536 * Parse a CIPSO local tag and return the security attributes in @secattr.
1537 * Return zero on success, negatives values on failure.
1538 *
1539 */
1540static int cipso_v4_parsetag_loc(const struct cipso_v4_doi *doi_def,
1541 const unsigned char *tag,
1542 struct netlbl_lsm_secattr *secattr)
1543{
1544 secattr->attr.secid = *(u32 *)&tag[2];
1545 secattr->flags |= NETLBL_SECATTR_SECID;
1546
1547 return 0;
1548}
1549
1550/**
Paul Moore446fda42006-08-03 16:48:06 -07001551 * cipso_v4_validate - Validate a CIPSO option
1552 * @option: the start of the option, on error it is set to point to the error
1553 *
1554 * Description:
1555 * This routine is called to validate a CIPSO option, it checks all of the
1556 * fields to ensure that they are at least valid, see the draft snippet below
1557 * for details. If the option is valid then a zero value is returned and
1558 * the value of @option is unchanged. If the option is invalid then a
1559 * non-zero value is returned and @option is adjusted to point to the
1560 * offending portion of the option. From the IETF draft ...
1561 *
1562 * "If any field within the CIPSO options, such as the DOI identifier, is not
1563 * recognized the IP datagram is discarded and an ICMP 'parameter problem'
1564 * (type 12) is generated and returned. The ICMP code field is set to 'bad
1565 * parameter' (code 0) and the pointer is set to the start of the CIPSO field
1566 * that is unrecognized."
1567 *
1568 */
Paul Moore15c45f72008-10-10 10:16:34 -04001569int cipso_v4_validate(const struct sk_buff *skb, unsigned char **option)
Paul Moore446fda42006-08-03 16:48:06 -07001570{
1571 unsigned char *opt = *option;
1572 unsigned char *tag;
1573 unsigned char opt_iter;
1574 unsigned char err_offset = 0;
1575 u8 opt_len;
1576 u8 tag_len;
1577 struct cipso_v4_doi *doi_def = NULL;
1578 u32 tag_iter;
1579
1580 /* caller already checks for length values that are too large */
1581 opt_len = opt[1];
1582 if (opt_len < 8) {
1583 err_offset = 1;
1584 goto validate_return;
1585 }
1586
1587 rcu_read_lock();
Harvey Harrisond3e2ce32008-05-02 16:26:16 -07001588 doi_def = cipso_v4_doi_search(get_unaligned_be32(&opt[2]));
Paul Moore446fda42006-08-03 16:48:06 -07001589 if (doi_def == NULL) {
1590 err_offset = 2;
1591 goto validate_return_locked;
1592 }
1593
Paul Moore15c45f72008-10-10 10:16:34 -04001594 opt_iter = CIPSO_V4_HDR_LEN;
Paul Moore446fda42006-08-03 16:48:06 -07001595 tag = opt + opt_iter;
1596 while (opt_iter < opt_len) {
1597 for (tag_iter = 0; doi_def->tags[tag_iter] != tag[0];)
1598 if (doi_def->tags[tag_iter] == CIPSO_V4_TAG_INVALID ||
1599 ++tag_iter == CIPSO_V4_TAG_MAXCNT) {
1600 err_offset = opt_iter;
1601 goto validate_return_locked;
1602 }
1603
1604 tag_len = tag[1];
1605 if (tag_len > (opt_len - opt_iter)) {
1606 err_offset = opt_iter + 1;
1607 goto validate_return_locked;
1608 }
1609
1610 switch (tag[0]) {
1611 case CIPSO_V4_TAG_RBITMAP:
Paul Moore15c45f72008-10-10 10:16:34 -04001612 if (tag_len < CIPSO_V4_TAG_RBM_BLEN) {
Paul Moore446fda42006-08-03 16:48:06 -07001613 err_offset = opt_iter + 1;
1614 goto validate_return_locked;
1615 }
1616
1617 /* We are already going to do all the verification
1618 * necessary at the socket layer so from our point of
1619 * view it is safe to turn these checks off (and less
1620 * work), however, the CIPSO draft says we should do
1621 * all the CIPSO validations here but it doesn't
1622 * really specify _exactly_ what we need to validate
1623 * ... so, just make it a sysctl tunable. */
1624 if (cipso_v4_rbm_strictvalid) {
1625 if (cipso_v4_map_lvl_valid(doi_def,
1626 tag[3]) < 0) {
1627 err_offset = opt_iter + 3;
1628 goto validate_return_locked;
1629 }
Paul Moore15c45f72008-10-10 10:16:34 -04001630 if (tag_len > CIPSO_V4_TAG_RBM_BLEN &&
Paul Moore446fda42006-08-03 16:48:06 -07001631 cipso_v4_map_cat_rbm_valid(doi_def,
1632 &tag[4],
1633 tag_len - 4) < 0) {
1634 err_offset = opt_iter + 4;
1635 goto validate_return_locked;
1636 }
1637 }
1638 break;
Paul Moore654bbc22006-11-29 13:18:19 -05001639 case CIPSO_V4_TAG_ENUM:
Paul Moore15c45f72008-10-10 10:16:34 -04001640 if (tag_len < CIPSO_V4_TAG_ENUM_BLEN) {
Paul Moore654bbc22006-11-29 13:18:19 -05001641 err_offset = opt_iter + 1;
1642 goto validate_return_locked;
1643 }
1644
1645 if (cipso_v4_map_lvl_valid(doi_def,
1646 tag[3]) < 0) {
1647 err_offset = opt_iter + 3;
1648 goto validate_return_locked;
1649 }
Paul Moore15c45f72008-10-10 10:16:34 -04001650 if (tag_len > CIPSO_V4_TAG_ENUM_BLEN &&
Paul Moore654bbc22006-11-29 13:18:19 -05001651 cipso_v4_map_cat_enum_valid(doi_def,
1652 &tag[4],
1653 tag_len - 4) < 0) {
1654 err_offset = opt_iter + 4;
1655 goto validate_return_locked;
1656 }
1657 break;
Paul Moore484b3662006-11-29 13:18:20 -05001658 case CIPSO_V4_TAG_RANGE:
Paul Moore15c45f72008-10-10 10:16:34 -04001659 if (tag_len < CIPSO_V4_TAG_RNG_BLEN) {
Paul Moore484b3662006-11-29 13:18:20 -05001660 err_offset = opt_iter + 1;
1661 goto validate_return_locked;
1662 }
1663
1664 if (cipso_v4_map_lvl_valid(doi_def,
1665 tag[3]) < 0) {
1666 err_offset = opt_iter + 3;
1667 goto validate_return_locked;
1668 }
Paul Moore15c45f72008-10-10 10:16:34 -04001669 if (tag_len > CIPSO_V4_TAG_RNG_BLEN &&
Paul Moore484b3662006-11-29 13:18:20 -05001670 cipso_v4_map_cat_rng_valid(doi_def,
1671 &tag[4],
1672 tag_len - 4) < 0) {
1673 err_offset = opt_iter + 4;
1674 goto validate_return_locked;
1675 }
1676 break;
Paul Moore15c45f72008-10-10 10:16:34 -04001677 case CIPSO_V4_TAG_LOCAL:
1678 /* This is a non-standard tag that we only allow for
1679 * local connections, so if the incoming interface is
1680 * not the loopback device drop the packet. */
1681 if (!(skb->dev->flags & IFF_LOOPBACK)) {
1682 err_offset = opt_iter;
1683 goto validate_return_locked;
1684 }
1685 if (tag_len != CIPSO_V4_TAG_LOC_BLEN) {
1686 err_offset = opt_iter + 1;
1687 goto validate_return_locked;
1688 }
1689 break;
Paul Moore446fda42006-08-03 16:48:06 -07001690 default:
1691 err_offset = opt_iter;
1692 goto validate_return_locked;
1693 }
1694
1695 tag += tag_len;
1696 opt_iter += tag_len;
1697 }
1698
1699validate_return_locked:
1700 rcu_read_unlock();
1701validate_return:
1702 *option = opt + err_offset;
1703 return err_offset;
1704}
1705
1706/**
1707 * cipso_v4_error - Send the correct reponse for a bad packet
1708 * @skb: the packet
1709 * @error: the error code
1710 * @gateway: CIPSO gateway flag
1711 *
1712 * Description:
1713 * Based on the error code given in @error, send an ICMP error message back to
1714 * the originating host. From the IETF draft ...
1715 *
1716 * "If the contents of the CIPSO [option] are valid but the security label is
1717 * outside of the configured host or port label range, the datagram is
1718 * discarded and an ICMP 'destination unreachable' (type 3) is generated and
1719 * returned. The code field of the ICMP is set to 'communication with
1720 * destination network administratively prohibited' (code 9) or to
1721 * 'communication with destination host administratively prohibited'
1722 * (code 10). The value of the code is dependent on whether the originator
1723 * of the ICMP message is acting as a CIPSO host or a CIPSO gateway. The
1724 * recipient of the ICMP message MUST be able to handle either value. The
1725 * same procedure is performed if a CIPSO [option] can not be added to an
1726 * IP packet because it is too large to fit in the IP options area."
1727 *
1728 * "If the error is triggered by receipt of an ICMP message, the message is
1729 * discarded and no response is permitted (consistent with general ICMP
1730 * processing rules)."
1731 *
1732 */
1733void cipso_v4_error(struct sk_buff *skb, int error, u32 gateway)
1734{
Arnaldo Carvalho de Meloeddc9ec2007-04-20 22:47:35 -07001735 if (ip_hdr(skb)->protocol == IPPROTO_ICMP || error != -EACCES)
Paul Moore446fda42006-08-03 16:48:06 -07001736 return;
1737
1738 if (gateway)
1739 icmp_send(skb, ICMP_DEST_UNREACH, ICMP_NET_ANO, 0);
1740 else
1741 icmp_send(skb, ICMP_DEST_UNREACH, ICMP_HOST_ANO, 0);
1742}
1743
1744/**
Paul Moore948bf852008-10-10 10:16:32 -04001745 * cipso_v4_genopt - Generate a CIPSO option
1746 * @buf: the option buffer
1747 * @buf_len: the size of opt_buf
Paul Moore446fda42006-08-03 16:48:06 -07001748 * @doi_def: the CIPSO DOI to use
Paul Moore948bf852008-10-10 10:16:32 -04001749 * @secattr: the security attributes
Paul Moore446fda42006-08-03 16:48:06 -07001750 *
1751 * Description:
Paul Moore948bf852008-10-10 10:16:32 -04001752 * Generate a CIPSO option using the DOI definition and security attributes
1753 * passed to the function. Returns the length of the option on success and
1754 * negative values on failure.
Paul Moore446fda42006-08-03 16:48:06 -07001755 *
1756 */
Paul Moore948bf852008-10-10 10:16:32 -04001757static int cipso_v4_genopt(unsigned char *buf, u32 buf_len,
1758 const struct cipso_v4_doi *doi_def,
1759 const struct netlbl_lsm_secattr *secattr)
Paul Moore446fda42006-08-03 16:48:06 -07001760{
Paul Moore948bf852008-10-10 10:16:32 -04001761 int ret_val;
Paul Moore446fda42006-08-03 16:48:06 -07001762 u32 iter;
Paul Moore446fda42006-08-03 16:48:06 -07001763
Paul Moore948bf852008-10-10 10:16:32 -04001764 if (buf_len <= CIPSO_V4_HDR_LEN)
1765 return -ENOSPC;
Paul Moore91b1ed02006-11-17 17:38:49 -05001766
Paul Moore446fda42006-08-03 16:48:06 -07001767 /* XXX - This code assumes only one tag per CIPSO option which isn't
1768 * really a good assumption to make but since we only support the MAC
1769 * tags right now it is a safe assumption. */
1770 iter = 0;
1771 do {
Paul Moore91b1ed02006-11-17 17:38:49 -05001772 memset(buf, 0, buf_len);
Paul Moore446fda42006-08-03 16:48:06 -07001773 switch (doi_def->tags[iter]) {
1774 case CIPSO_V4_TAG_RBITMAP:
1775 ret_val = cipso_v4_gentag_rbm(doi_def,
Paul Moore91b1ed02006-11-17 17:38:49 -05001776 secattr,
1777 &buf[CIPSO_V4_HDR_LEN],
1778 buf_len - CIPSO_V4_HDR_LEN);
Paul Moore446fda42006-08-03 16:48:06 -07001779 break;
Paul Moore654bbc22006-11-29 13:18:19 -05001780 case CIPSO_V4_TAG_ENUM:
1781 ret_val = cipso_v4_gentag_enum(doi_def,
1782 secattr,
1783 &buf[CIPSO_V4_HDR_LEN],
1784 buf_len - CIPSO_V4_HDR_LEN);
1785 break;
Paul Moore484b3662006-11-29 13:18:20 -05001786 case CIPSO_V4_TAG_RANGE:
1787 ret_val = cipso_v4_gentag_rng(doi_def,
1788 secattr,
1789 &buf[CIPSO_V4_HDR_LEN],
1790 buf_len - CIPSO_V4_HDR_LEN);
1791 break;
Paul Moore15c45f72008-10-10 10:16:34 -04001792 case CIPSO_V4_TAG_LOCAL:
1793 ret_val = cipso_v4_gentag_loc(doi_def,
1794 secattr,
1795 &buf[CIPSO_V4_HDR_LEN],
1796 buf_len - CIPSO_V4_HDR_LEN);
1797 break;
Paul Moore446fda42006-08-03 16:48:06 -07001798 default:
Paul Moore948bf852008-10-10 10:16:32 -04001799 return -EPERM;
Paul Moore446fda42006-08-03 16:48:06 -07001800 }
1801
1802 iter++;
Paul Moore91b1ed02006-11-17 17:38:49 -05001803 } while (ret_val < 0 &&
Paul Moore446fda42006-08-03 16:48:06 -07001804 iter < CIPSO_V4_TAG_MAXCNT &&
1805 doi_def->tags[iter] != CIPSO_V4_TAG_INVALID);
Paul Moore91b1ed02006-11-17 17:38:49 -05001806 if (ret_val < 0)
Paul Moore948bf852008-10-10 10:16:32 -04001807 return ret_val;
Paul Moore91b1ed02006-11-17 17:38:49 -05001808 cipso_v4_gentag_hdr(doi_def, buf, ret_val);
Paul Moore948bf852008-10-10 10:16:32 -04001809 return CIPSO_V4_HDR_LEN + ret_val;
1810}
1811
1812/**
1813 * cipso_v4_sock_setattr - Add a CIPSO option to a socket
1814 * @sk: the socket
1815 * @doi_def: the CIPSO DOI to use
1816 * @secattr: the specific security attributes of the socket
1817 *
1818 * Description:
1819 * Set the CIPSO option on the given socket using the DOI definition and
1820 * security attributes passed to the function. This function requires
1821 * exclusive access to @sk, which means it either needs to be in the
1822 * process of being created or locked. Returns zero on success and negative
1823 * values on failure.
1824 *
1825 */
1826int cipso_v4_sock_setattr(struct sock *sk,
1827 const struct cipso_v4_doi *doi_def,
1828 const struct netlbl_lsm_secattr *secattr)
1829{
1830 int ret_val = -EPERM;
1831 unsigned char *buf = NULL;
1832 u32 buf_len;
1833 u32 opt_len;
1834 struct ip_options *opt = NULL;
1835 struct inet_sock *sk_inet;
1836 struct inet_connection_sock *sk_conn;
1837
1838 /* In the case of sock_create_lite(), the sock->sk field is not
1839 * defined yet but it is not a problem as the only users of these
1840 * "lite" PF_INET sockets are functions which do an accept() call
1841 * afterwards so we will label the socket as part of the accept(). */
1842 if (sk == NULL)
1843 return 0;
1844
1845 /* We allocate the maximum CIPSO option size here so we are probably
1846 * being a little wasteful, but it makes our life _much_ easier later
1847 * on and after all we are only talking about 40 bytes. */
1848 buf_len = CIPSO_V4_OPT_LEN_MAX;
1849 buf = kmalloc(buf_len, GFP_ATOMIC);
1850 if (buf == NULL) {
1851 ret_val = -ENOMEM;
1852 goto socket_setattr_failure;
1853 }
1854
1855 ret_val = cipso_v4_genopt(buf, buf_len, doi_def, secattr);
1856 if (ret_val < 0)
1857 goto socket_setattr_failure;
1858 buf_len = ret_val;
Paul Moore446fda42006-08-03 16:48:06 -07001859
1860 /* We can't use ip_options_get() directly because it makes a call to
1861 * ip_options_get_alloc() which allocates memory with GFP_KERNEL and
Paul Mooref8687af2006-10-30 15:22:15 -08001862 * we won't always have CAP_NET_RAW even though we _always_ want to
1863 * set the IPOPT_CIPSO option. */
Paul Moore446fda42006-08-03 16:48:06 -07001864 opt_len = (buf_len + 3) & ~3;
1865 opt = kzalloc(sizeof(*opt) + opt_len, GFP_ATOMIC);
1866 if (opt == NULL) {
1867 ret_val = -ENOMEM;
1868 goto socket_setattr_failure;
1869 }
1870 memcpy(opt->__data, buf, buf_len);
1871 opt->optlen = opt_len;
Paul Mooref8687af2006-10-30 15:22:15 -08001872 opt->cipso = sizeof(struct iphdr);
Paul Moore446fda42006-08-03 16:48:06 -07001873 kfree(buf);
1874 buf = NULL;
Paul Moore446fda42006-08-03 16:48:06 -07001875
1876 sk_inet = inet_sk(sk);
1877 if (sk_inet->is_icsk) {
1878 sk_conn = inet_csk(sk);
1879 if (sk_inet->opt)
1880 sk_conn->icsk_ext_hdr_len -= sk_inet->opt->optlen;
1881 sk_conn->icsk_ext_hdr_len += opt->optlen;
1882 sk_conn->icsk_sync_mss(sk, sk_conn->icsk_pmtu_cookie);
1883 }
1884 opt = xchg(&sk_inet->opt, opt);
1885 kfree(opt);
1886
1887 return 0;
1888
1889socket_setattr_failure:
1890 kfree(buf);
1891 kfree(opt);
1892 return ret_val;
1893}
1894
1895/**
Paul Moore014ab192008-10-10 10:16:33 -04001896 * cipso_v4_sock_delattr - Delete the CIPSO option from a socket
1897 * @sk: the socket
1898 *
1899 * Description:
1900 * Removes the CIPSO option from a socket, if present.
1901 *
1902 */
1903void cipso_v4_sock_delattr(struct sock *sk)
1904{
1905 u8 hdr_delta;
1906 struct ip_options *opt;
1907 struct inet_sock *sk_inet;
1908
1909 sk_inet = inet_sk(sk);
1910 opt = sk_inet->opt;
1911 if (opt == NULL || opt->cipso == 0)
1912 return;
1913
1914 if (opt->srr || opt->rr || opt->ts || opt->router_alert) {
1915 u8 cipso_len;
1916 u8 cipso_off;
1917 unsigned char *cipso_ptr;
1918 int iter;
1919 int optlen_new;
1920
1921 cipso_off = opt->cipso - sizeof(struct iphdr);
1922 cipso_ptr = &opt->__data[cipso_off];
1923 cipso_len = cipso_ptr[1];
1924
1925 if (opt->srr > opt->cipso)
1926 opt->srr -= cipso_len;
1927 if (opt->rr > opt->cipso)
1928 opt->rr -= cipso_len;
1929 if (opt->ts > opt->cipso)
1930 opt->ts -= cipso_len;
1931 if (opt->router_alert > opt->cipso)
1932 opt->router_alert -= cipso_len;
1933 opt->cipso = 0;
1934
1935 memmove(cipso_ptr, cipso_ptr + cipso_len,
1936 opt->optlen - cipso_off - cipso_len);
1937
1938 /* determining the new total option length is tricky because of
1939 * the padding necessary, the only thing i can think to do at
1940 * this point is walk the options one-by-one, skipping the
1941 * padding at the end to determine the actual option size and
1942 * from there we can determine the new total option length */
1943 iter = 0;
1944 optlen_new = 0;
1945 while (iter < opt->optlen)
1946 if (opt->__data[iter] != IPOPT_NOP) {
1947 iter += opt->__data[iter + 1];
1948 optlen_new = iter;
1949 } else
1950 iter++;
1951 hdr_delta = opt->optlen;
1952 opt->optlen = (optlen_new + 3) & ~3;
1953 hdr_delta -= opt->optlen;
1954 } else {
1955 /* only the cipso option was present on the socket so we can
1956 * remove the entire option struct */
1957 sk_inet->opt = NULL;
1958 hdr_delta = opt->optlen;
1959 kfree(opt);
1960 }
1961
1962 if (sk_inet->is_icsk && hdr_delta > 0) {
1963 struct inet_connection_sock *sk_conn = inet_csk(sk);
1964 sk_conn->icsk_ext_hdr_len -= hdr_delta;
1965 sk_conn->icsk_sync_mss(sk, sk_conn->icsk_pmtu_cookie);
1966 }
1967}
1968
1969/**
Paul Moore63d804ea2007-09-15 21:45:13 -07001970 * cipso_v4_getattr - Helper function for the cipso_v4_*_getattr functions
1971 * @cipso: the CIPSO v4 option
1972 * @secattr: the security attributes
1973 *
1974 * Description:
1975 * Inspect @cipso and return the security attributes in @secattr. Returns zero
1976 * on success and negative values on failure.
1977 *
1978 */
1979static int cipso_v4_getattr(const unsigned char *cipso,
1980 struct netlbl_lsm_secattr *secattr)
1981{
1982 int ret_val = -ENOMSG;
1983 u32 doi;
1984 struct cipso_v4_doi *doi_def;
1985
1986 if (cipso_v4_cache_check(cipso, cipso[1], secattr) == 0)
1987 return 0;
1988
Harvey Harrisond3e2ce32008-05-02 16:26:16 -07001989 doi = get_unaligned_be32(&cipso[2]);
Paul Moore63d804ea2007-09-15 21:45:13 -07001990 rcu_read_lock();
1991 doi_def = cipso_v4_doi_search(doi);
1992 if (doi_def == NULL)
1993 goto getattr_return;
1994 /* XXX - This code assumes only one tag per CIPSO option which isn't
1995 * really a good assumption to make but since we only support the MAC
1996 * tags right now it is a safe assumption. */
1997 switch (cipso[6]) {
1998 case CIPSO_V4_TAG_RBITMAP:
1999 ret_val = cipso_v4_parsetag_rbm(doi_def, &cipso[6], secattr);
2000 break;
2001 case CIPSO_V4_TAG_ENUM:
2002 ret_val = cipso_v4_parsetag_enum(doi_def, &cipso[6], secattr);
2003 break;
2004 case CIPSO_V4_TAG_RANGE:
2005 ret_val = cipso_v4_parsetag_rng(doi_def, &cipso[6], secattr);
2006 break;
Paul Moore15c45f72008-10-10 10:16:34 -04002007 case CIPSO_V4_TAG_LOCAL:
2008 ret_val = cipso_v4_parsetag_loc(doi_def, &cipso[6], secattr);
2009 break;
Paul Moore63d804ea2007-09-15 21:45:13 -07002010 }
Paul Moore16efd452008-01-29 08:37:59 -05002011 if (ret_val == 0)
2012 secattr->type = NETLBL_NLTYPE_CIPSOV4;
Paul Moore63d804ea2007-09-15 21:45:13 -07002013
2014getattr_return:
2015 rcu_read_unlock();
2016 return ret_val;
2017}
2018
2019/**
Paul Moore14a72f52006-09-25 15:52:01 -07002020 * cipso_v4_sock_getattr - Get the security attributes from a sock
2021 * @sk: the sock
2022 * @secattr: the security attributes
2023 *
2024 * Description:
2025 * Query @sk to see if there is a CIPSO option attached to the sock and if
2026 * there is return the CIPSO security attributes in @secattr. This function
2027 * requires that @sk be locked, or privately held, but it does not do any
2028 * locking itself. Returns zero on success and negative values on failure.
2029 *
2030 */
2031int cipso_v4_sock_getattr(struct sock *sk, struct netlbl_lsm_secattr *secattr)
2032{
Paul Moore63d804ea2007-09-15 21:45:13 -07002033 struct ip_options *opt;
Paul Moore14a72f52006-09-25 15:52:01 -07002034
Paul Moore63d804ea2007-09-15 21:45:13 -07002035 opt = inet_sk(sk)->opt;
2036 if (opt == NULL || opt->cipso == 0)
Paul Moore14a72f52006-09-25 15:52:01 -07002037 return -ENOMSG;
Paul Moore14a72f52006-09-25 15:52:01 -07002038
Paul Moore63d804ea2007-09-15 21:45:13 -07002039 return cipso_v4_getattr(opt->__data + opt->cipso - sizeof(struct iphdr),
2040 secattr);
Paul Moore14a72f52006-09-25 15:52:01 -07002041}
2042
2043/**
Paul Moore948bf852008-10-10 10:16:32 -04002044 * cipso_v4_skbuff_setattr - Set the CIPSO option on a packet
2045 * @skb: the packet
2046 * @secattr: the security attributes
2047 *
2048 * Description:
2049 * Set the CIPSO option on the given packet based on the security attributes.
2050 * Returns a pointer to the IP header on success and NULL on failure.
2051 *
2052 */
2053int cipso_v4_skbuff_setattr(struct sk_buff *skb,
2054 const struct cipso_v4_doi *doi_def,
2055 const struct netlbl_lsm_secattr *secattr)
2056{
2057 int ret_val;
2058 struct iphdr *iph;
2059 struct ip_options *opt = &IPCB(skb)->opt;
2060 unsigned char buf[CIPSO_V4_OPT_LEN_MAX];
2061 u32 buf_len = CIPSO_V4_OPT_LEN_MAX;
2062 u32 opt_len;
2063 int len_delta;
2064
roel kluin00af5c62008-10-29 15:55:53 -04002065 ret_val = cipso_v4_genopt(buf, buf_len, doi_def, secattr);
2066 if (ret_val < 0)
2067 return ret_val;
2068 buf_len = ret_val;
Paul Moore948bf852008-10-10 10:16:32 -04002069 opt_len = (buf_len + 3) & ~3;
2070
2071 /* we overwrite any existing options to ensure that we have enough
2072 * room for the CIPSO option, the reason is that we _need_ to guarantee
2073 * that the security label is applied to the packet - we do the same
2074 * thing when using the socket options and it hasn't caused a problem,
2075 * if we need to we can always revisit this choice later */
2076
2077 len_delta = opt_len - opt->optlen;
2078 /* if we don't ensure enough headroom we could panic on the skb_push()
2079 * call below so make sure we have enough, we are also "mangling" the
2080 * packet so we should probably do a copy-on-write call anyway */
2081 ret_val = skb_cow(skb, skb_headroom(skb) + len_delta);
2082 if (ret_val < 0)
2083 return ret_val;
2084
2085 if (len_delta > 0) {
2086 /* we assume that the header + opt->optlen have already been
2087 * "pushed" in ip_options_build() or similar */
2088 iph = ip_hdr(skb);
2089 skb_push(skb, len_delta);
2090 memmove((char *)iph - len_delta, iph, iph->ihl << 2);
2091 skb_reset_network_header(skb);
2092 iph = ip_hdr(skb);
2093 } else if (len_delta < 0) {
2094 iph = ip_hdr(skb);
2095 memset(iph + 1, IPOPT_NOP, opt->optlen);
2096 } else
2097 iph = ip_hdr(skb);
2098
2099 if (opt->optlen > 0)
2100 memset(opt, 0, sizeof(*opt));
2101 opt->optlen = opt_len;
2102 opt->cipso = sizeof(struct iphdr);
2103 opt->is_changed = 1;
2104
2105 /* we have to do the following because we are being called from a
2106 * netfilter hook which means the packet already has had the header
2107 * fields populated and the checksum calculated - yes this means we
2108 * are doing more work than needed but we do it to keep the core
2109 * stack clean and tidy */
2110 memcpy(iph + 1, buf, buf_len);
2111 if (opt_len > buf_len)
2112 memset((char *)(iph + 1) + buf_len, 0, opt_len - buf_len);
2113 if (len_delta != 0) {
2114 iph->ihl = 5 + (opt_len >> 2);
2115 iph->tot_len = htons(skb->len);
2116 }
2117 ip_send_check(iph);
2118
2119 return 0;
2120}
2121
2122/**
2123 * cipso_v4_skbuff_delattr - Delete any CIPSO options from a packet
2124 * @skb: the packet
2125 *
2126 * Description:
2127 * Removes any and all CIPSO options from the given packet. Returns zero on
2128 * success, negative values on failure.
2129 *
2130 */
2131int cipso_v4_skbuff_delattr(struct sk_buff *skb)
2132{
2133 int ret_val;
2134 struct iphdr *iph;
2135 struct ip_options *opt = &IPCB(skb)->opt;
2136 unsigned char *cipso_ptr;
2137
2138 if (opt->cipso == 0)
2139 return 0;
2140
2141 /* since we are changing the packet we should make a copy */
2142 ret_val = skb_cow(skb, skb_headroom(skb));
2143 if (ret_val < 0)
2144 return ret_val;
2145
2146 /* the easiest thing to do is just replace the cipso option with noop
2147 * options since we don't change the size of the packet, although we
2148 * still need to recalculate the checksum */
2149
2150 iph = ip_hdr(skb);
2151 cipso_ptr = (unsigned char *)iph + opt->cipso;
2152 memset(cipso_ptr, IPOPT_NOOP, cipso_ptr[1]);
2153 opt->cipso = 0;
2154 opt->is_changed = 1;
2155
2156 ip_send_check(iph);
2157
2158 return 0;
2159}
2160
2161/**
Paul Moore446fda42006-08-03 16:48:06 -07002162 * cipso_v4_skbuff_getattr - Get the security attributes from the CIPSO option
2163 * @skb: the packet
2164 * @secattr: the security attributes
2165 *
2166 * Description:
2167 * Parse the given packet's CIPSO option and return the security attributes.
2168 * Returns zero on success and negative values on failure.
2169 *
2170 */
2171int cipso_v4_skbuff_getattr(const struct sk_buff *skb,
2172 struct netlbl_lsm_secattr *secattr)
2173{
Paul Moore63d804ea2007-09-15 21:45:13 -07002174 return cipso_v4_getattr(CIPSO_V4_OPTPTR(skb), secattr);
Paul Moore446fda42006-08-03 16:48:06 -07002175}
2176
2177/*
2178 * Setup Functions
2179 */
2180
2181/**
2182 * cipso_v4_init - Initialize the CIPSO module
2183 *
2184 * Description:
2185 * Initialize the CIPSO module and prepare it for use. Returns zero on success
2186 * and negative values on failure.
2187 *
2188 */
2189static int __init cipso_v4_init(void)
2190{
2191 int ret_val;
2192
2193 ret_val = cipso_v4_cache_init();
2194 if (ret_val != 0)
2195 panic("Failed to initialize the CIPSO/IPv4 cache (%d)\n",
2196 ret_val);
2197
2198 return 0;
2199}
2200
2201subsys_initcall(cipso_v4_init);