blob: 5160c710f2eb4d4c3506ce41039f58cf50f5521c [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
Paul Moore586c2502009-02-20 16:32:55 -05006 * FIPS-188. While CIPSO never became a full IETF RFC standard many vendors
Paul Moore446fda42006-08-03 16:48:06 -07007 * have chosen to adopt the protocol and over the years it has become a
8 * de-facto standard for labeled networking.
9 *
Paul Moore586c2502009-02-20 16:32:55 -050010 * The CIPSO draft specification can be found in the kernel's Documentation
11 * directory as well as the following URL:
Justin P. Mattock631dd1a2010-10-18 11:03:14 +020012 * http://tools.ietf.org/id/draft-ietf-cipso-ipsecurity-01.txt
Paul Moore586c2502009-02-20 16:32:55 -050013 * The FIPS-188 specification can be found at the following URL:
14 * http://www.itl.nist.gov/fipspubs/fip188.htm
15 *
Paul Moore446fda42006-08-03 16:48:06 -070016 * Author: Paul Moore <paul.moore@hp.com>
17 *
18 */
19
20/*
Paul Moore948bf852008-10-10 10:16:32 -040021 * (c) Copyright Hewlett-Packard Development Company, L.P., 2006, 2008
Paul Moore446fda42006-08-03 16:48:06 -070022 *
23 * This program is free software; you can redistribute it and/or modify
24 * it under the terms of the GNU General Public License as published by
25 * the Free Software Foundation; either version 2 of the License, or
26 * (at your option) any later version.
27 *
28 * This program is distributed in the hope that it will be useful,
29 * but WITHOUT ANY WARRANTY; without even the implied warranty of
30 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See
31 * the GNU General Public License for more details.
32 *
33 * You should have received a copy of the GNU General Public License
Jeff Kirshera99421d2013-12-06 09:13:39 -080034 * along with this program; if not, see <http://www.gnu.org/licenses/>.
Paul Moore446fda42006-08-03 16:48:06 -070035 *
36 */
37
38#include <linux/init.h>
39#include <linux/types.h>
40#include <linux/rcupdate.h>
41#include <linux/list.h>
42#include <linux/spinlock.h>
43#include <linux/string.h>
44#include <linux/jhash.h>
Paul Moore6c2e8ac2008-12-31 12:54:11 -050045#include <linux/audit.h>
Tejun Heo5a0e3ad2010-03-24 17:04:11 +090046#include <linux/slab.h>
Paul Moore446fda42006-08-03 16:48:06 -070047#include <net/ip.h>
48#include <net/icmp.h>
49#include <net/tcp.h>
50#include <net/netlabel.h>
51#include <net/cipso_ipv4.h>
Arun Sharma600634972011-07-26 16:09:06 -070052#include <linux/atomic.h>
Fabian Frederick4c787b12014-11-04 20:13:50 +010053#include <linux/bug.h>
Paul Moore50e5d352007-06-07 18:38:14 -070054#include <asm/unaligned.h>
Paul Moore446fda42006-08-03 16:48:06 -070055
Paul Moore446fda42006-08-03 16:48:06 -070056/* List of available DOI definitions */
Paul Moore446fda42006-08-03 16:48:06 -070057/* XXX - This currently assumes a minimal number of different DOIs in use,
58 * if in practice there are a lot of different DOIs this list should
59 * probably be turned into a hash table or something similar so we
60 * can do quick lookups. */
Adrian Bunk8ce11e62006-08-07 21:50:48 -070061static DEFINE_SPINLOCK(cipso_v4_doi_list_lock);
Denis Cheng1596c972007-12-07 00:49:47 -080062static LIST_HEAD(cipso_v4_doi_list);
Paul Moore446fda42006-08-03 16:48:06 -070063
64/* Label mapping cache */
65int cipso_v4_cache_enabled = 1;
66int cipso_v4_cache_bucketsize = 10;
67#define CIPSO_V4_CACHE_BUCKETBITS 7
68#define CIPSO_V4_CACHE_BUCKETS (1 << CIPSO_V4_CACHE_BUCKETBITS)
69#define CIPSO_V4_CACHE_REORDERLIMIT 10
70struct cipso_v4_map_cache_bkt {
71 spinlock_t lock;
72 u32 size;
73 struct list_head list;
74};
Fabian Frederick988b1342014-11-04 20:19:19 +010075
Paul Moore446fda42006-08-03 16:48:06 -070076struct cipso_v4_map_cache_entry {
77 u32 hash;
78 unsigned char *key;
79 size_t key_len;
80
paul.moore@hp.comffb733c2006-10-04 11:46:31 -040081 struct netlbl_lsm_cache *lsm_data;
Paul Moore446fda42006-08-03 16:48:06 -070082
83 u32 activity;
84 struct list_head list;
85};
Fabian Frederick988b1342014-11-04 20:19:19 +010086
87static struct cipso_v4_map_cache_bkt *cipso_v4_cache;
Paul Moore446fda42006-08-03 16:48:06 -070088
89/* Restricted bitmap (tag #1) flags */
90int cipso_v4_rbm_optfmt = 0;
91int cipso_v4_rbm_strictvalid = 1;
92
93/*
Paul Mooref998e8c2007-02-28 15:14:20 -050094 * Protocol Constants
95 */
96
97/* Maximum size of the CIPSO IP option, derived from the fact that the maximum
98 * IPv4 header size is 60 bytes and the base IPv4 header is 20 bytes long. */
99#define CIPSO_V4_OPT_LEN_MAX 40
100
101/* Length of the base CIPSO option, this includes the option type (1 byte), the
102 * option length (1 byte), and the DOI (4 bytes). */
103#define CIPSO_V4_HDR_LEN 6
104
105/* Base length of the restrictive category bitmap tag (tag #1). */
106#define CIPSO_V4_TAG_RBM_BLEN 4
107
108/* Base length of the enumerated category tag (tag #2). */
109#define CIPSO_V4_TAG_ENUM_BLEN 4
110
111/* Base length of the ranged categories bitmap tag (tag #5). */
112#define CIPSO_V4_TAG_RNG_BLEN 4
113/* The maximum number of category ranges permitted in the ranged category tag
114 * (tag #5). You may note that the IETF draft states that the maximum number
115 * of category ranges is 7, but if the low end of the last category range is
Lucas De Marchi25985ed2011-03-30 22:57:33 -0300116 * zero then it is possible to fit 8 category ranges because the zero should
Paul Mooref998e8c2007-02-28 15:14:20 -0500117 * be omitted. */
118#define CIPSO_V4_TAG_RNG_CAT_MAX 8
119
Paul Moore15c45f72008-10-10 10:16:34 -0400120/* Base length of the local tag (non-standard tag).
121 * Tag definition (may change between kernel versions)
122 *
123 * 0 8 16 24 32
124 * +----------+----------+----------+----------+
125 * | 10000000 | 00000110 | 32-bit secid value |
126 * +----------+----------+----------+----------+
127 * | in (host byte order)|
128 * +----------+----------+
129 *
130 */
131#define CIPSO_V4_TAG_LOC_BLEN 6
132
Paul Mooref998e8c2007-02-28 15:14:20 -0500133/*
Paul Moore446fda42006-08-03 16:48:06 -0700134 * Helper Functions
135 */
136
137/**
138 * cipso_v4_bitmap_walk - Walk a bitmap looking for a bit
139 * @bitmap: the bitmap
140 * @bitmap_len: length in bits
141 * @offset: starting offset
142 * @state: if non-zero, look for a set (1) bit else look for a cleared (0) bit
143 *
144 * Description:
145 * Starting at @offset, walk the bitmap from left to right until either the
146 * desired bit is found or we reach the end. Return the bit offset, -1 if
147 * not found, or -2 if error.
148 */
149static int cipso_v4_bitmap_walk(const unsigned char *bitmap,
150 u32 bitmap_len,
151 u32 offset,
152 u8 state)
153{
154 u32 bit_spot;
155 u32 byte_offset;
156 unsigned char bitmask;
157 unsigned char byte;
158
159 /* gcc always rounds to zero when doing integer division */
160 byte_offset = offset / 8;
161 byte = bitmap[byte_offset];
162 bit_spot = offset;
163 bitmask = 0x80 >> (offset % 8);
164
165 while (bit_spot < bitmap_len) {
166 if ((state && (byte & bitmask) == bitmask) ||
167 (state == 0 && (byte & bitmask) == 0))
168 return bit_spot;
169
170 bit_spot++;
171 bitmask >>= 1;
172 if (bitmask == 0) {
173 byte = bitmap[++byte_offset];
174 bitmask = 0x80;
175 }
176 }
177
178 return -1;
179}
180
181/**
182 * cipso_v4_bitmap_setbit - Sets a single bit in a bitmap
183 * @bitmap: the bitmap
184 * @bit: the bit
185 * @state: if non-zero, set the bit (1) else clear the bit (0)
186 *
187 * Description:
188 * Set a single bit in the bitmask. Returns zero on success, negative values
189 * on error.
190 */
191static void cipso_v4_bitmap_setbit(unsigned char *bitmap,
192 u32 bit,
193 u8 state)
194{
195 u32 byte_spot;
196 u8 bitmask;
197
198 /* gcc always rounds to zero when doing integer division */
199 byte_spot = bit / 8;
200 bitmask = 0x80 >> (bit % 8);
201 if (state)
202 bitmap[byte_spot] |= bitmask;
203 else
204 bitmap[byte_spot] &= ~bitmask;
205}
206
207/**
Paul Moore446fda42006-08-03 16:48:06 -0700208 * cipso_v4_cache_entry_free - Frees a cache entry
209 * @entry: the entry to free
210 *
211 * Description:
paul.moore@hp.comffb733c2006-10-04 11:46:31 -0400212 * This function frees the memory associated with a cache entry including the
213 * LSM cache data if there are no longer any users, i.e. reference count == 0.
Paul Moore446fda42006-08-03 16:48:06 -0700214 *
215 */
216static void cipso_v4_cache_entry_free(struct cipso_v4_map_cache_entry *entry)
217{
paul.moore@hp.comffb733c2006-10-04 11:46:31 -0400218 if (entry->lsm_data)
219 netlbl_secattr_cache_free(entry->lsm_data);
Paul Moore446fda42006-08-03 16:48:06 -0700220 kfree(entry->key);
221 kfree(entry);
222}
223
224/**
225 * cipso_v4_map_cache_hash - Hashing function for the CIPSO cache
226 * @key: the hash key
227 * @key_len: the length of the key in bytes
228 *
229 * Description:
230 * The CIPSO tag hashing function. Returns a 32-bit hash value.
231 *
232 */
233static u32 cipso_v4_map_cache_hash(const unsigned char *key, u32 key_len)
234{
235 return jhash(key, key_len, 0);
236}
237
238/*
239 * Label Mapping Cache Functions
240 */
241
242/**
243 * cipso_v4_cache_init - Initialize the CIPSO cache
244 *
245 * Description:
246 * Initializes the CIPSO label mapping cache, this function should be called
247 * before any of the other functions defined in this file. Returns zero on
248 * success, negative values on error.
249 *
250 */
Fabian Frederickcb576592014-10-01 19:30:03 +0200251static int __init cipso_v4_cache_init(void)
Paul Moore446fda42006-08-03 16:48:06 -0700252{
253 u32 iter;
254
255 cipso_v4_cache = kcalloc(CIPSO_V4_CACHE_BUCKETS,
256 sizeof(struct cipso_v4_map_cache_bkt),
257 GFP_KERNEL);
258 if (cipso_v4_cache == NULL)
259 return -ENOMEM;
260
261 for (iter = 0; iter < CIPSO_V4_CACHE_BUCKETS; iter++) {
262 spin_lock_init(&cipso_v4_cache[iter].lock);
263 cipso_v4_cache[iter].size = 0;
264 INIT_LIST_HEAD(&cipso_v4_cache[iter].list);
265 }
266
267 return 0;
268}
269
270/**
271 * cipso_v4_cache_invalidate - Invalidates the current CIPSO cache
272 *
273 * Description:
274 * Invalidates and frees any entries in the CIPSO cache. Returns zero on
275 * success and negative values on failure.
276 *
277 */
278void cipso_v4_cache_invalidate(void)
279{
280 struct cipso_v4_map_cache_entry *entry, *tmp_entry;
281 u32 iter;
282
283 for (iter = 0; iter < CIPSO_V4_CACHE_BUCKETS; iter++) {
Paul Moore609c92f2006-09-25 15:52:37 -0700284 spin_lock_bh(&cipso_v4_cache[iter].lock);
Paul Moore446fda42006-08-03 16:48:06 -0700285 list_for_each_entry_safe(entry,
286 tmp_entry,
287 &cipso_v4_cache[iter].list, list) {
288 list_del(&entry->list);
289 cipso_v4_cache_entry_free(entry);
290 }
291 cipso_v4_cache[iter].size = 0;
Paul Moore609c92f2006-09-25 15:52:37 -0700292 spin_unlock_bh(&cipso_v4_cache[iter].lock);
Paul Moore446fda42006-08-03 16:48:06 -0700293 }
Paul Moore446fda42006-08-03 16:48:06 -0700294}
295
296/**
297 * cipso_v4_cache_check - Check the CIPSO cache for a label mapping
298 * @key: the buffer to check
299 * @key_len: buffer length in bytes
300 * @secattr: the security attribute struct to use
301 *
302 * Description:
303 * This function checks the cache to see if a label mapping already exists for
304 * the given key. If there is a match then the cache is adjusted and the
305 * @secattr struct is populated with the correct LSM security attributes. The
306 * cache is adjusted in the following manner if the entry is not already the
307 * first in the cache bucket:
308 *
309 * 1. The cache entry's activity counter is incremented
310 * 2. The previous (higher ranking) entry's activity counter is decremented
311 * 3. If the difference between the two activity counters is geater than
312 * CIPSO_V4_CACHE_REORDERLIMIT the two entries are swapped
313 *
314 * Returns zero on success, -ENOENT for a cache miss, and other negative values
315 * on error.
316 *
317 */
318static int cipso_v4_cache_check(const unsigned char *key,
319 u32 key_len,
320 struct netlbl_lsm_secattr *secattr)
321{
322 u32 bkt;
323 struct cipso_v4_map_cache_entry *entry;
324 struct cipso_v4_map_cache_entry *prev_entry = NULL;
325 u32 hash;
326
327 if (!cipso_v4_cache_enabled)
328 return -ENOENT;
329
330 hash = cipso_v4_map_cache_hash(key, key_len);
Pavel Emelyanov5e0f8922008-05-13 23:23:55 -0700331 bkt = hash & (CIPSO_V4_CACHE_BUCKETS - 1);
Paul Moore609c92f2006-09-25 15:52:37 -0700332 spin_lock_bh(&cipso_v4_cache[bkt].lock);
Paul Moore446fda42006-08-03 16:48:06 -0700333 list_for_each_entry(entry, &cipso_v4_cache[bkt].list, list) {
334 if (entry->hash == hash &&
335 entry->key_len == key_len &&
336 memcmp(entry->key, key, key_len) == 0) {
337 entry->activity += 1;
paul.moore@hp.comffb733c2006-10-04 11:46:31 -0400338 atomic_inc(&entry->lsm_data->refcount);
339 secattr->cache = entry->lsm_data;
Paul Moore701a90b2006-11-17 17:38:46 -0500340 secattr->flags |= NETLBL_SECATTR_CACHE;
Paul Moore16efd452008-01-29 08:37:59 -0500341 secattr->type = NETLBL_NLTYPE_CIPSOV4;
Paul Moore446fda42006-08-03 16:48:06 -0700342 if (prev_entry == NULL) {
Paul Moore609c92f2006-09-25 15:52:37 -0700343 spin_unlock_bh(&cipso_v4_cache[bkt].lock);
Paul Moore446fda42006-08-03 16:48:06 -0700344 return 0;
345 }
346
347 if (prev_entry->activity > 0)
348 prev_entry->activity -= 1;
349 if (entry->activity > prev_entry->activity &&
350 entry->activity - prev_entry->activity >
351 CIPSO_V4_CACHE_REORDERLIMIT) {
352 __list_del(entry->list.prev, entry->list.next);
353 __list_add(&entry->list,
354 prev_entry->list.prev,
355 &prev_entry->list);
356 }
357
Paul Moore609c92f2006-09-25 15:52:37 -0700358 spin_unlock_bh(&cipso_v4_cache[bkt].lock);
Paul Moore446fda42006-08-03 16:48:06 -0700359 return 0;
360 }
361 prev_entry = entry;
362 }
Paul Moore609c92f2006-09-25 15:52:37 -0700363 spin_unlock_bh(&cipso_v4_cache[bkt].lock);
Paul Moore446fda42006-08-03 16:48:06 -0700364
365 return -ENOENT;
366}
367
368/**
369 * cipso_v4_cache_add - Add an entry to the CIPSO cache
370 * @skb: the packet
371 * @secattr: the packet's security attributes
372 *
373 * Description:
374 * Add a new entry into the CIPSO label mapping cache. Add the new entry to
375 * head of the cache bucket's list, if the cache bucket is out of room remove
376 * the last entry in the list first. It is important to note that there is
377 * currently no checking for duplicate keys. Returns zero on success,
378 * negative values on failure.
379 *
380 */
381int cipso_v4_cache_add(const struct sk_buff *skb,
382 const struct netlbl_lsm_secattr *secattr)
383{
384 int ret_val = -EPERM;
385 u32 bkt;
386 struct cipso_v4_map_cache_entry *entry = NULL;
387 struct cipso_v4_map_cache_entry *old_entry = NULL;
388 unsigned char *cipso_ptr;
389 u32 cipso_ptr_len;
390
391 if (!cipso_v4_cache_enabled || cipso_v4_cache_bucketsize <= 0)
392 return 0;
393
394 cipso_ptr = CIPSO_V4_OPTPTR(skb);
395 cipso_ptr_len = cipso_ptr[1];
396
397 entry = kzalloc(sizeof(*entry), GFP_ATOMIC);
398 if (entry == NULL)
399 return -ENOMEM;
Arnaldo Carvalho de Melofac5d732006-11-17 11:14:16 -0200400 entry->key = kmemdup(cipso_ptr, cipso_ptr_len, GFP_ATOMIC);
Paul Moore446fda42006-08-03 16:48:06 -0700401 if (entry->key == NULL) {
402 ret_val = -ENOMEM;
403 goto cache_add_failure;
404 }
Paul Moore446fda42006-08-03 16:48:06 -0700405 entry->key_len = cipso_ptr_len;
406 entry->hash = cipso_v4_map_cache_hash(cipso_ptr, cipso_ptr_len);
paul.moore@hp.comffb733c2006-10-04 11:46:31 -0400407 atomic_inc(&secattr->cache->refcount);
408 entry->lsm_data = secattr->cache;
Paul Moore446fda42006-08-03 16:48:06 -0700409
Pavel Emelyanov5e0f8922008-05-13 23:23:55 -0700410 bkt = entry->hash & (CIPSO_V4_CACHE_BUCKETS - 1);
Paul Moore609c92f2006-09-25 15:52:37 -0700411 spin_lock_bh(&cipso_v4_cache[bkt].lock);
Paul Moore446fda42006-08-03 16:48:06 -0700412 if (cipso_v4_cache[bkt].size < cipso_v4_cache_bucketsize) {
413 list_add(&entry->list, &cipso_v4_cache[bkt].list);
414 cipso_v4_cache[bkt].size += 1;
415 } else {
416 old_entry = list_entry(cipso_v4_cache[bkt].list.prev,
417 struct cipso_v4_map_cache_entry, list);
418 list_del(&old_entry->list);
419 list_add(&entry->list, &cipso_v4_cache[bkt].list);
420 cipso_v4_cache_entry_free(old_entry);
421 }
Paul Moore609c92f2006-09-25 15:52:37 -0700422 spin_unlock_bh(&cipso_v4_cache[bkt].lock);
Paul Moore446fda42006-08-03 16:48:06 -0700423
424 return 0;
425
426cache_add_failure:
427 if (entry)
428 cipso_v4_cache_entry_free(entry);
429 return ret_val;
430}
431
432/*
433 * DOI List Functions
434 */
435
436/**
437 * cipso_v4_doi_search - Searches for a DOI definition
438 * @doi: the DOI to search for
439 *
440 * Description:
441 * Search the DOI definition list for a DOI definition with a DOI value that
Lucas De Marchi25985ed2011-03-30 22:57:33 -0300442 * matches @doi. The caller is responsible for calling rcu_read_[un]lock().
Paul Moore446fda42006-08-03 16:48:06 -0700443 * Returns a pointer to the DOI definition on success and NULL on failure.
444 */
445static struct cipso_v4_doi *cipso_v4_doi_search(u32 doi)
446{
447 struct cipso_v4_doi *iter;
448
449 list_for_each_entry_rcu(iter, &cipso_v4_doi_list, list)
Paul Mooreb1edeb12008-10-10 10:16:31 -0400450 if (iter->doi == doi && atomic_read(&iter->refcount))
Paul Moore446fda42006-08-03 16:48:06 -0700451 return iter;
452 return NULL;
453}
454
455/**
456 * cipso_v4_doi_add - Add a new DOI to the CIPSO protocol engine
457 * @doi_def: the DOI structure
Paul Moore6c2e8ac2008-12-31 12:54:11 -0500458 * @audit_info: NetLabel audit information
Paul Moore446fda42006-08-03 16:48:06 -0700459 *
460 * Description:
461 * The caller defines a new DOI for use by the CIPSO engine and calls this
462 * function to add it to the list of acceptable domains. The caller must
463 * ensure that the mapping table specified in @doi_def->map meets all of the
464 * requirements of the mapping type (see cipso_ipv4.h for details). Returns
465 * zero on success and non-zero on failure.
466 *
467 */
Paul Moore6c2e8ac2008-12-31 12:54:11 -0500468int cipso_v4_doi_add(struct cipso_v4_doi *doi_def,
469 struct netlbl_audit *audit_info)
Paul Moore446fda42006-08-03 16:48:06 -0700470{
Paul Moore6c2e8ac2008-12-31 12:54:11 -0500471 int ret_val = -EINVAL;
Paul Moore6ce61a72006-11-17 17:38:48 -0500472 u32 iter;
Paul Moore6c2e8ac2008-12-31 12:54:11 -0500473 u32 doi;
474 u32 doi_type;
475 struct audit_buffer *audit_buf;
476
477 doi = doi_def->doi;
478 doi_type = doi_def->type;
Paul Moore6ce61a72006-11-17 17:38:48 -0500479
Dan Carpenter56755922011-10-11 18:43:53 -0400480 if (doi_def->doi == CIPSO_V4_DOI_UNKNOWN)
Paul Moore6c2e8ac2008-12-31 12:54:11 -0500481 goto doi_add_return;
Paul Moore6ce61a72006-11-17 17:38:48 -0500482 for (iter = 0; iter < CIPSO_V4_TAG_MAXCNT; iter++) {
483 switch (doi_def->tags[iter]) {
484 case CIPSO_V4_TAG_RBITMAP:
485 break;
Paul Moore484b3662006-11-29 13:18:20 -0500486 case CIPSO_V4_TAG_RANGE:
Paul Moore654bbc22006-11-29 13:18:19 -0500487 case CIPSO_V4_TAG_ENUM:
488 if (doi_def->type != CIPSO_V4_MAP_PASS)
Paul Moore6c2e8ac2008-12-31 12:54:11 -0500489 goto doi_add_return;
Paul Moore654bbc22006-11-29 13:18:19 -0500490 break;
Paul Moore15c45f72008-10-10 10:16:34 -0400491 case CIPSO_V4_TAG_LOCAL:
492 if (doi_def->type != CIPSO_V4_MAP_LOCAL)
Paul Moore6c2e8ac2008-12-31 12:54:11 -0500493 goto doi_add_return;
494 break;
495 case CIPSO_V4_TAG_INVALID:
496 if (iter == 0)
497 goto doi_add_return;
Paul Moore15c45f72008-10-10 10:16:34 -0400498 break;
Paul Moore6ce61a72006-11-17 17:38:48 -0500499 default:
Paul Moore6c2e8ac2008-12-31 12:54:11 -0500500 goto doi_add_return;
Paul Moore6ce61a72006-11-17 17:38:48 -0500501 }
502 }
Paul Moore446fda42006-08-03 16:48:06 -0700503
Paul Mooreb1edeb12008-10-10 10:16:31 -0400504 atomic_set(&doi_def->refcount, 1);
Paul Moore446fda42006-08-03 16:48:06 -0700505
Paul Moore446fda42006-08-03 16:48:06 -0700506 spin_lock(&cipso_v4_doi_list_lock);
Paul Moore6c2e8ac2008-12-31 12:54:11 -0500507 if (cipso_v4_doi_search(doi_def->doi) != NULL) {
508 spin_unlock(&cipso_v4_doi_list_lock);
509 ret_val = -EEXIST;
510 goto doi_add_return;
511 }
Paul Moore446fda42006-08-03 16:48:06 -0700512 list_add_tail_rcu(&doi_def->list, &cipso_v4_doi_list);
513 spin_unlock(&cipso_v4_doi_list_lock);
Paul Moore6c2e8ac2008-12-31 12:54:11 -0500514 ret_val = 0;
Paul Moore446fda42006-08-03 16:48:06 -0700515
Paul Moore6c2e8ac2008-12-31 12:54:11 -0500516doi_add_return:
517 audit_buf = netlbl_audit_start(AUDIT_MAC_CIPSOV4_ADD, audit_info);
518 if (audit_buf != NULL) {
519 const char *type_str;
520 switch (doi_type) {
521 case CIPSO_V4_MAP_TRANS:
522 type_str = "trans";
523 break;
524 case CIPSO_V4_MAP_PASS:
525 type_str = "pass";
526 break;
527 case CIPSO_V4_MAP_LOCAL:
528 type_str = "local";
529 break;
530 default:
531 type_str = "(unknown)";
532 }
533 audit_log_format(audit_buf,
534 " cipso_doi=%u cipso_type=%s res=%u",
535 doi, type_str, ret_val == 0 ? 1 : 0);
536 audit_log_end(audit_buf);
537 }
Paul Moore446fda42006-08-03 16:48:06 -0700538
Paul Moore6c2e8ac2008-12-31 12:54:11 -0500539 return ret_val;
Paul Moore446fda42006-08-03 16:48:06 -0700540}
541
542/**
Paul Mooreb1edeb12008-10-10 10:16:31 -0400543 * cipso_v4_doi_free - Frees a DOI definition
Fabian Frederick49734042014-11-04 20:10:01 +0100544 * @doi_def: the DOI definition
Paul Moore446fda42006-08-03 16:48:06 -0700545 *
546 * Description:
Paul Mooreb1edeb12008-10-10 10:16:31 -0400547 * This function frees all of the memory associated with a DOI definition.
Paul Moore446fda42006-08-03 16:48:06 -0700548 *
549 */
Paul Mooreb1edeb12008-10-10 10:16:31 -0400550void cipso_v4_doi_free(struct cipso_v4_doi *doi_def)
Paul Moore446fda42006-08-03 16:48:06 -0700551{
Paul Mooreb1edeb12008-10-10 10:16:31 -0400552 if (doi_def == NULL)
553 return;
Paul Moore446fda42006-08-03 16:48:06 -0700554
Paul Mooreb1edeb12008-10-10 10:16:31 -0400555 switch (doi_def->type) {
Paul Moore15c45f72008-10-10 10:16:34 -0400556 case CIPSO_V4_MAP_TRANS:
Paul Mooreb1edeb12008-10-10 10:16:31 -0400557 kfree(doi_def->map.std->lvl.cipso);
558 kfree(doi_def->map.std->lvl.local);
559 kfree(doi_def->map.std->cat.cipso);
560 kfree(doi_def->map.std->cat.local);
561 break;
Paul Moore446fda42006-08-03 16:48:06 -0700562 }
Paul Mooreb1edeb12008-10-10 10:16:31 -0400563 kfree(doi_def);
Paul Moore446fda42006-08-03 16:48:06 -0700564}
565
566/**
Paul Mooreb1edeb12008-10-10 10:16:31 -0400567 * cipso_v4_doi_free_rcu - Frees a DOI definition via the RCU pointer
568 * @entry: the entry's RCU field
569 *
570 * Description:
571 * This function is designed to be used as a callback to the call_rcu()
572 * function so that the memory allocated to the DOI definition can be released
573 * safely.
574 *
575 */
576static void cipso_v4_doi_free_rcu(struct rcu_head *entry)
577{
578 struct cipso_v4_doi *doi_def;
579
580 doi_def = container_of(entry, struct cipso_v4_doi, rcu);
581 cipso_v4_doi_free(doi_def);
582}
583
584/**
585 * cipso_v4_doi_remove - Remove an existing DOI from the CIPSO protocol engine
586 * @doi: the DOI value
587 * @audit_secid: the LSM secid to use in the audit message
588 *
589 * Description:
590 * Removes a DOI definition from the CIPSO engine. The NetLabel routines will
591 * be called to release their own LSM domain mappings as well as our own
592 * domain list. Returns zero on success and negative values on failure.
593 *
594 */
595int cipso_v4_doi_remove(u32 doi, struct netlbl_audit *audit_info)
596{
Paul Moore6c2e8ac2008-12-31 12:54:11 -0500597 int ret_val;
Paul Mooreb1edeb12008-10-10 10:16:31 -0400598 struct cipso_v4_doi *doi_def;
Paul Moore6c2e8ac2008-12-31 12:54:11 -0500599 struct audit_buffer *audit_buf;
Paul Mooreb1edeb12008-10-10 10:16:31 -0400600
601 spin_lock(&cipso_v4_doi_list_lock);
602 doi_def = cipso_v4_doi_search(doi);
603 if (doi_def == NULL) {
604 spin_unlock(&cipso_v4_doi_list_lock);
Paul Moore6c2e8ac2008-12-31 12:54:11 -0500605 ret_val = -ENOENT;
606 goto doi_remove_return;
Paul Mooreb1edeb12008-10-10 10:16:31 -0400607 }
608 if (!atomic_dec_and_test(&doi_def->refcount)) {
609 spin_unlock(&cipso_v4_doi_list_lock);
Paul Moore6c2e8ac2008-12-31 12:54:11 -0500610 ret_val = -EBUSY;
611 goto doi_remove_return;
Paul Mooreb1edeb12008-10-10 10:16:31 -0400612 }
613 list_del_rcu(&doi_def->list);
614 spin_unlock(&cipso_v4_doi_list_lock);
615
616 cipso_v4_cache_invalidate();
617 call_rcu(&doi_def->rcu, cipso_v4_doi_free_rcu);
Paul Moore6c2e8ac2008-12-31 12:54:11 -0500618 ret_val = 0;
Paul Mooreb1edeb12008-10-10 10:16:31 -0400619
Paul Moore6c2e8ac2008-12-31 12:54:11 -0500620doi_remove_return:
621 audit_buf = netlbl_audit_start(AUDIT_MAC_CIPSOV4_DEL, audit_info);
622 if (audit_buf != NULL) {
623 audit_log_format(audit_buf,
624 " cipso_doi=%u res=%u",
625 doi, ret_val == 0 ? 1 : 0);
626 audit_log_end(audit_buf);
627 }
628
629 return ret_val;
Paul Mooreb1edeb12008-10-10 10:16:31 -0400630}
631
632/**
633 * cipso_v4_doi_getdef - Returns a reference to a valid DOI definition
Paul Moore446fda42006-08-03 16:48:06 -0700634 * @doi: the DOI value
635 *
636 * Description:
637 * Searches for a valid DOI definition and if one is found it is returned to
638 * the caller. Otherwise NULL is returned. The caller must ensure that
Paul Mooreb1edeb12008-10-10 10:16:31 -0400639 * rcu_read_lock() is held while accessing the returned definition and the DOI
640 * definition reference count is decremented when the caller is done.
Paul Moore446fda42006-08-03 16:48:06 -0700641 *
642 */
643struct cipso_v4_doi *cipso_v4_doi_getdef(u32 doi)
644{
Paul Mooreb1edeb12008-10-10 10:16:31 -0400645 struct cipso_v4_doi *doi_def;
646
647 rcu_read_lock();
648 doi_def = cipso_v4_doi_search(doi);
649 if (doi_def == NULL)
650 goto doi_getdef_return;
651 if (!atomic_inc_not_zero(&doi_def->refcount))
652 doi_def = NULL;
653
654doi_getdef_return:
655 rcu_read_unlock();
656 return doi_def;
657}
658
659/**
660 * cipso_v4_doi_putdef - Releases a reference for the given DOI definition
661 * @doi_def: the DOI definition
662 *
663 * Description:
664 * Releases a DOI definition reference obtained from cipso_v4_doi_getdef().
665 *
666 */
667void cipso_v4_doi_putdef(struct cipso_v4_doi *doi_def)
668{
669 if (doi_def == NULL)
670 return;
671
672 if (!atomic_dec_and_test(&doi_def->refcount))
673 return;
674 spin_lock(&cipso_v4_doi_list_lock);
675 list_del_rcu(&doi_def->list);
676 spin_unlock(&cipso_v4_doi_list_lock);
677
678 cipso_v4_cache_invalidate();
679 call_rcu(&doi_def->rcu, cipso_v4_doi_free_rcu);
Paul Moore446fda42006-08-03 16:48:06 -0700680}
681
682/**
Paul Moorefcd48282006-09-25 15:56:09 -0700683 * cipso_v4_doi_walk - Iterate through the DOI definitions
684 * @skip_cnt: skip past this number of DOI definitions, updated
685 * @callback: callback for each DOI definition
686 * @cb_arg: argument for the callback function
Paul Moore446fda42006-08-03 16:48:06 -0700687 *
688 * Description:
Paul Moorefcd48282006-09-25 15:56:09 -0700689 * Iterate over the DOI definition list, skipping the first @skip_cnt entries.
690 * For each entry call @callback, if @callback returns a negative value stop
691 * 'walking' through the list and return. Updates the value in @skip_cnt upon
692 * return. Returns zero on success, negative values on failure.
Paul Moore446fda42006-08-03 16:48:06 -0700693 *
694 */
Paul Moorefcd48282006-09-25 15:56:09 -0700695int cipso_v4_doi_walk(u32 *skip_cnt,
696 int (*callback) (struct cipso_v4_doi *doi_def, void *arg),
697 void *cb_arg)
Paul Moore446fda42006-08-03 16:48:06 -0700698{
Paul Moorefcd48282006-09-25 15:56:09 -0700699 int ret_val = -ENOENT;
Paul Moore446fda42006-08-03 16:48:06 -0700700 u32 doi_cnt = 0;
Paul Moorefcd48282006-09-25 15:56:09 -0700701 struct cipso_v4_doi *iter_doi;
Paul Moore446fda42006-08-03 16:48:06 -0700702
703 rcu_read_lock();
Paul Moorefcd48282006-09-25 15:56:09 -0700704 list_for_each_entry_rcu(iter_doi, &cipso_v4_doi_list, list)
Paul Mooreb1edeb12008-10-10 10:16:31 -0400705 if (atomic_read(&iter_doi->refcount) > 0) {
Paul Moorefcd48282006-09-25 15:56:09 -0700706 if (doi_cnt++ < *skip_cnt)
707 continue;
708 ret_val = callback(iter_doi, cb_arg);
709 if (ret_val < 0) {
710 doi_cnt--;
711 goto doi_walk_return;
Paul Moore446fda42006-08-03 16:48:06 -0700712 }
Paul Moorefcd48282006-09-25 15:56:09 -0700713 }
Paul Moore446fda42006-08-03 16:48:06 -0700714
Paul Moorefcd48282006-09-25 15:56:09 -0700715doi_walk_return:
Paul Moore446fda42006-08-03 16:48:06 -0700716 rcu_read_unlock();
Paul Moorefcd48282006-09-25 15:56:09 -0700717 *skip_cnt = doi_cnt;
718 return ret_val;
Paul Moore446fda42006-08-03 16:48:06 -0700719}
720
Paul Moore446fda42006-08-03 16:48:06 -0700721/*
722 * Label Mapping Functions
723 */
724
725/**
726 * cipso_v4_map_lvl_valid - Checks to see if the given level is understood
727 * @doi_def: the DOI definition
728 * @level: the level to check
729 *
730 * Description:
731 * Checks the given level against the given DOI definition and returns a
732 * negative value if the level does not have a valid mapping and a zero value
733 * if the level is defined by the DOI.
734 *
735 */
736static int cipso_v4_map_lvl_valid(const struct cipso_v4_doi *doi_def, u8 level)
737{
738 switch (doi_def->type) {
739 case CIPSO_V4_MAP_PASS:
740 return 0;
Paul Moore15c45f72008-10-10 10:16:34 -0400741 case CIPSO_V4_MAP_TRANS:
Paul Moore446fda42006-08-03 16:48:06 -0700742 if (doi_def->map.std->lvl.cipso[level] < CIPSO_V4_INV_LVL)
743 return 0;
744 break;
745 }
746
747 return -EFAULT;
748}
749
750/**
751 * cipso_v4_map_lvl_hton - Perform a level mapping from the host to the network
752 * @doi_def: the DOI definition
753 * @host_lvl: the host MLS level
754 * @net_lvl: the network/CIPSO MLS level
755 *
756 * Description:
757 * Perform a label mapping to translate a local MLS level to the correct
758 * CIPSO level using the given DOI definition. Returns zero on success,
759 * negative values otherwise.
760 *
761 */
762static int cipso_v4_map_lvl_hton(const struct cipso_v4_doi *doi_def,
763 u32 host_lvl,
764 u32 *net_lvl)
765{
766 switch (doi_def->type) {
767 case CIPSO_V4_MAP_PASS:
768 *net_lvl = host_lvl;
769 return 0;
Paul Moore15c45f72008-10-10 10:16:34 -0400770 case CIPSO_V4_MAP_TRANS:
Paul Moorec6387a82007-03-02 13:19:02 -0800771 if (host_lvl < doi_def->map.std->lvl.local_size &&
772 doi_def->map.std->lvl.local[host_lvl] < CIPSO_V4_INV_LVL) {
Paul Moore446fda42006-08-03 16:48:06 -0700773 *net_lvl = doi_def->map.std->lvl.local[host_lvl];
774 return 0;
775 }
Paul Moorec6387a82007-03-02 13:19:02 -0800776 return -EPERM;
Paul Moore446fda42006-08-03 16:48:06 -0700777 }
778
779 return -EINVAL;
780}
781
782/**
783 * cipso_v4_map_lvl_ntoh - Perform a level mapping from the network to the host
784 * @doi_def: the DOI definition
785 * @net_lvl: the network/CIPSO MLS level
786 * @host_lvl: the host MLS level
787 *
788 * Description:
789 * Perform a label mapping to translate a CIPSO level to the correct local MLS
790 * level using the given DOI definition. Returns zero on success, negative
791 * values otherwise.
792 *
793 */
794static int cipso_v4_map_lvl_ntoh(const struct cipso_v4_doi *doi_def,
795 u32 net_lvl,
796 u32 *host_lvl)
797{
798 struct cipso_v4_std_map_tbl *map_tbl;
799
800 switch (doi_def->type) {
801 case CIPSO_V4_MAP_PASS:
802 *host_lvl = net_lvl;
803 return 0;
Paul Moore15c45f72008-10-10 10:16:34 -0400804 case CIPSO_V4_MAP_TRANS:
Paul Moore446fda42006-08-03 16:48:06 -0700805 map_tbl = doi_def->map.std;
806 if (net_lvl < map_tbl->lvl.cipso_size &&
807 map_tbl->lvl.cipso[net_lvl] < CIPSO_V4_INV_LVL) {
808 *host_lvl = doi_def->map.std->lvl.cipso[net_lvl];
809 return 0;
810 }
Paul Moorec6387a82007-03-02 13:19:02 -0800811 return -EPERM;
Paul Moore446fda42006-08-03 16:48:06 -0700812 }
813
814 return -EINVAL;
815}
816
817/**
818 * cipso_v4_map_cat_rbm_valid - Checks to see if the category bitmap is valid
819 * @doi_def: the DOI definition
820 * @bitmap: category bitmap
821 * @bitmap_len: bitmap length in bytes
822 *
823 * Description:
824 * Checks the given category bitmap against the given DOI definition and
825 * returns a negative value if any of the categories in the bitmap do not have
826 * a valid mapping and a zero value if all of the categories are valid.
827 *
828 */
829static int cipso_v4_map_cat_rbm_valid(const struct cipso_v4_doi *doi_def,
830 const unsigned char *bitmap,
831 u32 bitmap_len)
832{
833 int cat = -1;
834 u32 bitmap_len_bits = bitmap_len * 8;
Paul Moore044a68e2006-10-11 19:10:47 -0400835 u32 cipso_cat_size;
836 u32 *cipso_array;
Paul Moore446fda42006-08-03 16:48:06 -0700837
838 switch (doi_def->type) {
839 case CIPSO_V4_MAP_PASS:
840 return 0;
Paul Moore15c45f72008-10-10 10:16:34 -0400841 case CIPSO_V4_MAP_TRANS:
Paul Moore044a68e2006-10-11 19:10:47 -0400842 cipso_cat_size = doi_def->map.std->cat.cipso_size;
843 cipso_array = doi_def->map.std->cat.cipso;
Paul Moore446fda42006-08-03 16:48:06 -0700844 for (;;) {
845 cat = cipso_v4_bitmap_walk(bitmap,
846 bitmap_len_bits,
847 cat + 1,
848 1);
849 if (cat < 0)
850 break;
851 if (cat >= cipso_cat_size ||
852 cipso_array[cat] >= CIPSO_V4_INV_CAT)
853 return -EFAULT;
854 }
855
856 if (cat == -1)
857 return 0;
858 break;
859 }
860
861 return -EFAULT;
862}
863
864/**
865 * cipso_v4_map_cat_rbm_hton - Perform a category mapping from host to network
866 * @doi_def: the DOI definition
Paul Moore02752762006-11-29 13:18:18 -0500867 * @secattr: the security attributes
Paul Moore446fda42006-08-03 16:48:06 -0700868 * @net_cat: the zero'd out category bitmap in network/CIPSO format
869 * @net_cat_len: the length of the CIPSO bitmap in bytes
870 *
871 * Description:
872 * Perform a label mapping to translate a local MLS category bitmap to the
873 * correct CIPSO bitmap using the given DOI definition. Returns the minimum
874 * size in bytes of the network bitmap on success, negative values otherwise.
875 *
876 */
877static int cipso_v4_map_cat_rbm_hton(const struct cipso_v4_doi *doi_def,
Paul Moore02752762006-11-29 13:18:18 -0500878 const struct netlbl_lsm_secattr *secattr,
Paul Moore446fda42006-08-03 16:48:06 -0700879 unsigned char *net_cat,
880 u32 net_cat_len)
881{
882 int host_spot = -1;
Paul Moore02752762006-11-29 13:18:18 -0500883 u32 net_spot = CIPSO_V4_INV_CAT;
Paul Moore446fda42006-08-03 16:48:06 -0700884 u32 net_spot_max = 0;
Paul Moore446fda42006-08-03 16:48:06 -0700885 u32 net_clen_bits = net_cat_len * 8;
Paul Moore02752762006-11-29 13:18:18 -0500886 u32 host_cat_size = 0;
887 u32 *host_cat_array = NULL;
Paul Moore446fda42006-08-03 16:48:06 -0700888
Paul Moore15c45f72008-10-10 10:16:34 -0400889 if (doi_def->type == CIPSO_V4_MAP_TRANS) {
Paul Moore044a68e2006-10-11 19:10:47 -0400890 host_cat_size = doi_def->map.std->cat.local_size;
891 host_cat_array = doi_def->map.std->cat.local;
Paul Moore02752762006-11-29 13:18:18 -0500892 }
893
894 for (;;) {
Paul Moore4fbe63d2014-08-01 11:17:37 -0400895 host_spot = netlbl_catmap_walk(secattr->attr.mls.cat,
896 host_spot + 1);
Paul Moore02752762006-11-29 13:18:18 -0500897 if (host_spot < 0)
898 break;
899
900 switch (doi_def->type) {
901 case CIPSO_V4_MAP_PASS:
902 net_spot = host_spot;
903 break;
Paul Moore15c45f72008-10-10 10:16:34 -0400904 case CIPSO_V4_MAP_TRANS:
Paul Moore446fda42006-08-03 16:48:06 -0700905 if (host_spot >= host_cat_size)
906 return -EPERM;
Paul Moore446fda42006-08-03 16:48:06 -0700907 net_spot = host_cat_array[host_spot];
Paul Moore9fade4b2006-11-17 17:38:50 -0500908 if (net_spot >= CIPSO_V4_INV_CAT)
909 return -EPERM;
Paul Moore02752762006-11-29 13:18:18 -0500910 break;
Paul Moore446fda42006-08-03 16:48:06 -0700911 }
Paul Moore02752762006-11-29 13:18:18 -0500912 if (net_spot >= net_clen_bits)
913 return -ENOSPC;
914 cipso_v4_bitmap_setbit(net_cat, net_spot, 1);
Paul Moore446fda42006-08-03 16:48:06 -0700915
Paul Moore02752762006-11-29 13:18:18 -0500916 if (net_spot > net_spot_max)
917 net_spot_max = net_spot;
Paul Moore446fda42006-08-03 16:48:06 -0700918 }
919
Paul Moore02752762006-11-29 13:18:18 -0500920 if (++net_spot_max % 8)
921 return net_spot_max / 8 + 1;
922 return net_spot_max / 8;
Paul Moore446fda42006-08-03 16:48:06 -0700923}
924
925/**
926 * cipso_v4_map_cat_rbm_ntoh - Perform a category mapping from network to host
927 * @doi_def: the DOI definition
928 * @net_cat: the category bitmap in network/CIPSO format
929 * @net_cat_len: the length of the CIPSO bitmap in bytes
Paul Moore02752762006-11-29 13:18:18 -0500930 * @secattr: the security attributes
Paul Moore446fda42006-08-03 16:48:06 -0700931 *
932 * Description:
933 * Perform a label mapping to translate a CIPSO bitmap to the correct local
Paul Moore02752762006-11-29 13:18:18 -0500934 * MLS category bitmap using the given DOI definition. Returns zero on
935 * success, negative values on failure.
Paul Moore446fda42006-08-03 16:48:06 -0700936 *
937 */
938static int cipso_v4_map_cat_rbm_ntoh(const struct cipso_v4_doi *doi_def,
939 const unsigned char *net_cat,
940 u32 net_cat_len,
Paul Moore02752762006-11-29 13:18:18 -0500941 struct netlbl_lsm_secattr *secattr)
Paul Moore446fda42006-08-03 16:48:06 -0700942{
Paul Moore02752762006-11-29 13:18:18 -0500943 int ret_val;
Paul Moore446fda42006-08-03 16:48:06 -0700944 int net_spot = -1;
Paul Moore02752762006-11-29 13:18:18 -0500945 u32 host_spot = CIPSO_V4_INV_CAT;
Paul Moore446fda42006-08-03 16:48:06 -0700946 u32 net_clen_bits = net_cat_len * 8;
Paul Moore02752762006-11-29 13:18:18 -0500947 u32 net_cat_size = 0;
948 u32 *net_cat_array = NULL;
Paul Moore446fda42006-08-03 16:48:06 -0700949
Paul Moore15c45f72008-10-10 10:16:34 -0400950 if (doi_def->type == CIPSO_V4_MAP_TRANS) {
Paul Moore044a68e2006-10-11 19:10:47 -0400951 net_cat_size = doi_def->map.std->cat.cipso_size;
952 net_cat_array = doi_def->map.std->cat.cipso;
Paul Moore02752762006-11-29 13:18:18 -0500953 }
Paul Moore446fda42006-08-03 16:48:06 -0700954
Paul Moore02752762006-11-29 13:18:18 -0500955 for (;;) {
956 net_spot = cipso_v4_bitmap_walk(net_cat,
957 net_clen_bits,
958 net_spot + 1,
959 1);
960 if (net_spot < 0) {
961 if (net_spot == -2)
962 return -EFAULT;
963 return 0;
964 }
965
966 switch (doi_def->type) {
967 case CIPSO_V4_MAP_PASS:
968 host_spot = net_spot;
969 break;
Paul Moore15c45f72008-10-10 10:16:34 -0400970 case CIPSO_V4_MAP_TRANS:
Paul Moore02752762006-11-29 13:18:18 -0500971 if (net_spot >= net_cat_size)
972 return -EPERM;
Paul Moore446fda42006-08-03 16:48:06 -0700973 host_spot = net_cat_array[net_spot];
Paul Moore9fade4b2006-11-17 17:38:50 -0500974 if (host_spot >= CIPSO_V4_INV_CAT)
975 return -EPERM;
Paul Moore02752762006-11-29 13:18:18 -0500976 break;
Paul Moore446fda42006-08-03 16:48:06 -0700977 }
Paul Moore4fbe63d2014-08-01 11:17:37 -0400978 ret_val = netlbl_catmap_setbit(&secattr->attr.mls.cat,
Paul Moore02752762006-11-29 13:18:18 -0500979 host_spot,
980 GFP_ATOMIC);
981 if (ret_val != 0)
982 return ret_val;
Paul Moore446fda42006-08-03 16:48:06 -0700983 }
984
985 return -EINVAL;
986}
987
Paul Moore654bbc22006-11-29 13:18:19 -0500988/**
989 * cipso_v4_map_cat_enum_valid - Checks to see if the categories are valid
990 * @doi_def: the DOI definition
991 * @enumcat: category list
992 * @enumcat_len: length of the category list in bytes
993 *
994 * Description:
995 * Checks the given categories against the given DOI definition and returns a
996 * negative value if any of the categories do not have a valid mapping and a
997 * zero value if all of the categories are valid.
998 *
999 */
1000static int cipso_v4_map_cat_enum_valid(const struct cipso_v4_doi *doi_def,
1001 const unsigned char *enumcat,
1002 u32 enumcat_len)
1003{
1004 u16 cat;
1005 int cat_prev = -1;
1006 u32 iter;
1007
1008 if (doi_def->type != CIPSO_V4_MAP_PASS || enumcat_len & 0x01)
1009 return -EFAULT;
1010
1011 for (iter = 0; iter < enumcat_len; iter += 2) {
Harvey Harrisond3e2ce32008-05-02 16:26:16 -07001012 cat = get_unaligned_be16(&enumcat[iter]);
Paul Moore654bbc22006-11-29 13:18:19 -05001013 if (cat <= cat_prev)
1014 return -EFAULT;
1015 cat_prev = cat;
1016 }
1017
1018 return 0;
1019}
1020
1021/**
1022 * cipso_v4_map_cat_enum_hton - Perform a category mapping from host to network
1023 * @doi_def: the DOI definition
1024 * @secattr: the security attributes
1025 * @net_cat: the zero'd out category list in network/CIPSO format
1026 * @net_cat_len: the length of the CIPSO category list in bytes
1027 *
1028 * Description:
1029 * Perform a label mapping to translate a local MLS category bitmap to the
1030 * correct CIPSO category list using the given DOI definition. Returns the
1031 * size in bytes of the network category bitmap on success, negative values
1032 * otherwise.
1033 *
1034 */
1035static int cipso_v4_map_cat_enum_hton(const struct cipso_v4_doi *doi_def,
1036 const struct netlbl_lsm_secattr *secattr,
1037 unsigned char *net_cat,
1038 u32 net_cat_len)
1039{
1040 int cat = -1;
1041 u32 cat_iter = 0;
1042
1043 for (;;) {
Paul Moore4fbe63d2014-08-01 11:17:37 -04001044 cat = netlbl_catmap_walk(secattr->attr.mls.cat, cat + 1);
Paul Moore654bbc22006-11-29 13:18:19 -05001045 if (cat < 0)
1046 break;
1047 if ((cat_iter + 2) > net_cat_len)
1048 return -ENOSPC;
1049
1050 *((__be16 *)&net_cat[cat_iter]) = htons(cat);
1051 cat_iter += 2;
1052 }
1053
1054 return cat_iter;
1055}
1056
1057/**
1058 * cipso_v4_map_cat_enum_ntoh - Perform a category mapping from network to host
1059 * @doi_def: the DOI definition
1060 * @net_cat: the category list in network/CIPSO format
1061 * @net_cat_len: the length of the CIPSO bitmap in bytes
1062 * @secattr: the security attributes
1063 *
1064 * Description:
1065 * Perform a label mapping to translate a CIPSO category list to the correct
1066 * local MLS category bitmap using the given DOI definition. Returns zero on
1067 * success, negative values on failure.
1068 *
1069 */
1070static int cipso_v4_map_cat_enum_ntoh(const struct cipso_v4_doi *doi_def,
1071 const unsigned char *net_cat,
1072 u32 net_cat_len,
1073 struct netlbl_lsm_secattr *secattr)
1074{
1075 int ret_val;
1076 u32 iter;
1077
1078 for (iter = 0; iter < net_cat_len; iter += 2) {
Paul Moore4fbe63d2014-08-01 11:17:37 -04001079 ret_val = netlbl_catmap_setbit(&secattr->attr.mls.cat,
1080 get_unaligned_be16(&net_cat[iter]),
1081 GFP_ATOMIC);
Paul Moore654bbc22006-11-29 13:18:19 -05001082 if (ret_val != 0)
1083 return ret_val;
1084 }
1085
1086 return 0;
1087}
1088
Paul Moore484b3662006-11-29 13:18:20 -05001089/**
1090 * cipso_v4_map_cat_rng_valid - Checks to see if the categories are valid
1091 * @doi_def: the DOI definition
1092 * @rngcat: category list
1093 * @rngcat_len: length of the category list in bytes
1094 *
1095 * Description:
1096 * Checks the given categories against the given DOI definition and returns a
1097 * negative value if any of the categories do not have a valid mapping and a
1098 * zero value if all of the categories are valid.
1099 *
1100 */
1101static int cipso_v4_map_cat_rng_valid(const struct cipso_v4_doi *doi_def,
1102 const unsigned char *rngcat,
1103 u32 rngcat_len)
1104{
1105 u16 cat_high;
1106 u16 cat_low;
1107 u32 cat_prev = CIPSO_V4_MAX_REM_CATS + 1;
1108 u32 iter;
1109
1110 if (doi_def->type != CIPSO_V4_MAP_PASS || rngcat_len & 0x01)
1111 return -EFAULT;
1112
1113 for (iter = 0; iter < rngcat_len; iter += 4) {
Harvey Harrisond3e2ce32008-05-02 16:26:16 -07001114 cat_high = get_unaligned_be16(&rngcat[iter]);
Paul Moore484b3662006-11-29 13:18:20 -05001115 if ((iter + 4) <= rngcat_len)
Harvey Harrisond3e2ce32008-05-02 16:26:16 -07001116 cat_low = get_unaligned_be16(&rngcat[iter + 2]);
Paul Moore484b3662006-11-29 13:18:20 -05001117 else
1118 cat_low = 0;
1119
1120 if (cat_high > cat_prev)
1121 return -EFAULT;
1122
1123 cat_prev = cat_low;
1124 }
1125
1126 return 0;
1127}
1128
1129/**
1130 * cipso_v4_map_cat_rng_hton - Perform a category mapping from host to network
1131 * @doi_def: the DOI definition
1132 * @secattr: the security attributes
1133 * @net_cat: the zero'd out category list in network/CIPSO format
1134 * @net_cat_len: the length of the CIPSO category list in bytes
1135 *
1136 * Description:
1137 * Perform a label mapping to translate a local MLS category bitmap to the
1138 * correct CIPSO category list using the given DOI definition. Returns the
1139 * size in bytes of the network category bitmap on success, negative values
1140 * otherwise.
1141 *
1142 */
1143static int cipso_v4_map_cat_rng_hton(const struct cipso_v4_doi *doi_def,
1144 const struct netlbl_lsm_secattr *secattr,
1145 unsigned char *net_cat,
1146 u32 net_cat_len)
1147{
Paul Moore484b3662006-11-29 13:18:20 -05001148 int iter = -1;
Paul Mooref998e8c2007-02-28 15:14:20 -05001149 u16 array[CIPSO_V4_TAG_RNG_CAT_MAX * 2];
Paul Moore484b3662006-11-29 13:18:20 -05001150 u32 array_cnt = 0;
1151 u32 cat_size = 0;
1152
Paul Mooref998e8c2007-02-28 15:14:20 -05001153 /* make sure we don't overflow the 'array[]' variable */
Paul Moore128c6b62007-02-28 15:14:21 -05001154 if (net_cat_len >
1155 (CIPSO_V4_OPT_LEN_MAX - CIPSO_V4_HDR_LEN - CIPSO_V4_TAG_RNG_BLEN))
1156 return -ENOSPC;
Paul Moore484b3662006-11-29 13:18:20 -05001157
1158 for (;;) {
Paul Moore4fbe63d2014-08-01 11:17:37 -04001159 iter = netlbl_catmap_walk(secattr->attr.mls.cat, iter + 1);
Paul Moore484b3662006-11-29 13:18:20 -05001160 if (iter < 0)
1161 break;
1162 cat_size += (iter == 0 ? 0 : sizeof(u16));
1163 if (cat_size > net_cat_len)
1164 return -ENOSPC;
1165 array[array_cnt++] = iter;
1166
Paul Moore4fbe63d2014-08-01 11:17:37 -04001167 iter = netlbl_catmap_walkrng(secattr->attr.mls.cat, iter);
Paul Moore484b3662006-11-29 13:18:20 -05001168 if (iter < 0)
1169 return -EFAULT;
1170 cat_size += sizeof(u16);
1171 if (cat_size > net_cat_len)
1172 return -ENOSPC;
1173 array[array_cnt++] = iter;
1174 }
1175
1176 for (iter = 0; array_cnt > 0;) {
1177 *((__be16 *)&net_cat[iter]) = htons(array[--array_cnt]);
1178 iter += 2;
1179 array_cnt--;
1180 if (array[array_cnt] != 0) {
1181 *((__be16 *)&net_cat[iter]) = htons(array[array_cnt]);
1182 iter += 2;
1183 }
1184 }
1185
1186 return cat_size;
1187}
1188
1189/**
1190 * cipso_v4_map_cat_rng_ntoh - Perform a category mapping from network to host
1191 * @doi_def: the DOI definition
1192 * @net_cat: the category list in network/CIPSO format
1193 * @net_cat_len: the length of the CIPSO bitmap in bytes
1194 * @secattr: the security attributes
1195 *
1196 * Description:
1197 * Perform a label mapping to translate a CIPSO category list to the correct
1198 * local MLS category bitmap using the given DOI definition. Returns zero on
1199 * success, negative values on failure.
1200 *
1201 */
1202static int cipso_v4_map_cat_rng_ntoh(const struct cipso_v4_doi *doi_def,
1203 const unsigned char *net_cat,
1204 u32 net_cat_len,
1205 struct netlbl_lsm_secattr *secattr)
1206{
1207 int ret_val;
1208 u32 net_iter;
1209 u16 cat_low;
1210 u16 cat_high;
1211
Stephen Hemminger132adf52007-03-08 20:44:43 -08001212 for (net_iter = 0; net_iter < net_cat_len; net_iter += 4) {
Harvey Harrisond3e2ce32008-05-02 16:26:16 -07001213 cat_high = get_unaligned_be16(&net_cat[net_iter]);
Paul Moore484b3662006-11-29 13:18:20 -05001214 if ((net_iter + 4) <= net_cat_len)
Harvey Harrisond3e2ce32008-05-02 16:26:16 -07001215 cat_low = get_unaligned_be16(&net_cat[net_iter + 2]);
Paul Moore484b3662006-11-29 13:18:20 -05001216 else
1217 cat_low = 0;
1218
Paul Moore4fbe63d2014-08-01 11:17:37 -04001219 ret_val = netlbl_catmap_setrng(&secattr->attr.mls.cat,
1220 cat_low,
1221 cat_high,
1222 GFP_ATOMIC);
Paul Moore484b3662006-11-29 13:18:20 -05001223 if (ret_val != 0)
1224 return ret_val;
1225 }
1226
1227 return 0;
1228}
1229
Paul Moore446fda42006-08-03 16:48:06 -07001230/*
1231 * Protocol Handling Functions
1232 */
1233
Paul Moore446fda42006-08-03 16:48:06 -07001234/**
1235 * cipso_v4_gentag_hdr - Generate a CIPSO option header
1236 * @doi_def: the DOI definition
Paul Moore91b1ed02006-11-17 17:38:49 -05001237 * @len: the total tag length in bytes, not including this header
Paul Moore446fda42006-08-03 16:48:06 -07001238 * @buf: the CIPSO option buffer
1239 *
1240 * Description:
Paul Moore91b1ed02006-11-17 17:38:49 -05001241 * Write a CIPSO header into the beginning of @buffer.
Paul Moore446fda42006-08-03 16:48:06 -07001242 *
1243 */
Paul Moore91b1ed02006-11-17 17:38:49 -05001244static void cipso_v4_gentag_hdr(const struct cipso_v4_doi *doi_def,
1245 unsigned char *buf,
1246 u32 len)
Paul Moore446fda42006-08-03 16:48:06 -07001247{
Paul Moore446fda42006-08-03 16:48:06 -07001248 buf[0] = IPOPT_CIPSO;
1249 buf[1] = CIPSO_V4_HDR_LEN + len;
Al Viro714e85b2006-11-14 20:51:49 -08001250 *(__be32 *)&buf[2] = htonl(doi_def->doi);
Paul Moore446fda42006-08-03 16:48:06 -07001251}
1252
Paul Moore446fda42006-08-03 16:48:06 -07001253/**
1254 * cipso_v4_gentag_rbm - Generate a CIPSO restricted bitmap tag (type #1)
1255 * @doi_def: the DOI definition
1256 * @secattr: the security attributes
1257 * @buffer: the option buffer
1258 * @buffer_len: length of buffer in bytes
1259 *
1260 * Description:
1261 * Generate a CIPSO option using the restricted bitmap tag, tag type #1. The
1262 * actual buffer length may be larger than the indicated size due to
Paul Moore91b1ed02006-11-17 17:38:49 -05001263 * translation between host and network category bitmaps. Returns the size of
1264 * the tag on success, negative values on failure.
Paul Moore446fda42006-08-03 16:48:06 -07001265 *
1266 */
1267static int cipso_v4_gentag_rbm(const struct cipso_v4_doi *doi_def,
1268 const struct netlbl_lsm_secattr *secattr,
Paul Moore91b1ed02006-11-17 17:38:49 -05001269 unsigned char *buffer,
1270 u32 buffer_len)
Paul Moore446fda42006-08-03 16:48:06 -07001271{
Paul Moore701a90b2006-11-17 17:38:46 -05001272 int ret_val;
Paul Moore91b1ed02006-11-17 17:38:49 -05001273 u32 tag_len;
Paul Moore446fda42006-08-03 16:48:06 -07001274 u32 level;
1275
Paul Moore701a90b2006-11-17 17:38:46 -05001276 if ((secattr->flags & NETLBL_SECATTR_MLS_LVL) == 0)
1277 return -EPERM;
1278
Paul Moore16efd452008-01-29 08:37:59 -05001279 ret_val = cipso_v4_map_lvl_hton(doi_def,
1280 secattr->attr.mls.lvl,
1281 &level);
Paul Moore91b1ed02006-11-17 17:38:49 -05001282 if (ret_val != 0)
1283 return ret_val;
Paul Moore446fda42006-08-03 16:48:06 -07001284
Paul Moore91b1ed02006-11-17 17:38:49 -05001285 if (secattr->flags & NETLBL_SECATTR_MLS_CAT) {
Paul Moore446fda42006-08-03 16:48:06 -07001286 ret_val = cipso_v4_map_cat_rbm_hton(doi_def,
Paul Moore02752762006-11-29 13:18:18 -05001287 secattr,
Paul Moore91b1ed02006-11-17 17:38:49 -05001288 &buffer[4],
1289 buffer_len - 4);
Paul Moore446fda42006-08-03 16:48:06 -07001290 if (ret_val < 0)
Paul Moore91b1ed02006-11-17 17:38:49 -05001291 return ret_val;
Paul Moore446fda42006-08-03 16:48:06 -07001292
1293 /* This will send packets using the "optimized" format when
Lucas De Marchi25985ed2011-03-30 22:57:33 -03001294 * possible as specified in section 3.4.2.6 of the
Paul Moore446fda42006-08-03 16:48:06 -07001295 * CIPSO draft. */
Paul Moore701a90b2006-11-17 17:38:46 -05001296 if (cipso_v4_rbm_optfmt && ret_val > 0 && ret_val <= 10)
Paul Moore91b1ed02006-11-17 17:38:49 -05001297 tag_len = 14;
Paul Moore701a90b2006-11-17 17:38:46 -05001298 else
Paul Moore91b1ed02006-11-17 17:38:49 -05001299 tag_len = 4 + ret_val;
1300 } else
1301 tag_len = 4;
Paul Moore446fda42006-08-03 16:48:06 -07001302
Paul Moore15c45f72008-10-10 10:16:34 -04001303 buffer[0] = CIPSO_V4_TAG_RBITMAP;
Paul Moore91b1ed02006-11-17 17:38:49 -05001304 buffer[1] = tag_len;
1305 buffer[3] = level;
Paul Moore446fda42006-08-03 16:48:06 -07001306
Paul Moore91b1ed02006-11-17 17:38:49 -05001307 return tag_len;
Paul Moore446fda42006-08-03 16:48:06 -07001308}
1309
1310/**
1311 * cipso_v4_parsetag_rbm - Parse a CIPSO restricted bitmap tag
1312 * @doi_def: the DOI definition
1313 * @tag: the CIPSO tag
1314 * @secattr: the security attributes
1315 *
1316 * Description:
1317 * Parse a CIPSO restricted bitmap tag (tag type #1) and return the security
1318 * attributes in @secattr. Return zero on success, negatives values on
1319 * failure.
1320 *
1321 */
1322static int cipso_v4_parsetag_rbm(const struct cipso_v4_doi *doi_def,
1323 const unsigned char *tag,
1324 struct netlbl_lsm_secattr *secattr)
1325{
1326 int ret_val;
1327 u8 tag_len = tag[1];
1328 u32 level;
1329
1330 ret_val = cipso_v4_map_lvl_ntoh(doi_def, tag[3], &level);
1331 if (ret_val != 0)
1332 return ret_val;
Paul Moore16efd452008-01-29 08:37:59 -05001333 secattr->attr.mls.lvl = level;
Paul Moore701a90b2006-11-17 17:38:46 -05001334 secattr->flags |= NETLBL_SECATTR_MLS_LVL;
Paul Moore446fda42006-08-03 16:48:06 -07001335
1336 if (tag_len > 4) {
Paul Moore446fda42006-08-03 16:48:06 -07001337 ret_val = cipso_v4_map_cat_rbm_ntoh(doi_def,
1338 &tag[4],
1339 tag_len - 4,
Paul Moore02752762006-11-29 13:18:18 -05001340 secattr);
1341 if (ret_val != 0) {
Paul Moore4fbe63d2014-08-01 11:17:37 -04001342 netlbl_catmap_free(secattr->attr.mls.cat);
Paul Moore446fda42006-08-03 16:48:06 -07001343 return ret_val;
1344 }
Paul Moore02752762006-11-29 13:18:18 -05001345
1346 secattr->flags |= NETLBL_SECATTR_MLS_CAT;
Paul Moore446fda42006-08-03 16:48:06 -07001347 }
1348
1349 return 0;
1350}
1351
1352/**
Paul Moore654bbc22006-11-29 13:18:19 -05001353 * cipso_v4_gentag_enum - Generate a CIPSO enumerated tag (type #2)
1354 * @doi_def: the DOI definition
1355 * @secattr: the security attributes
1356 * @buffer: the option buffer
1357 * @buffer_len: length of buffer in bytes
1358 *
1359 * Description:
1360 * Generate a CIPSO option using the enumerated tag, tag type #2. Returns the
1361 * size of the tag on success, negative values on failure.
1362 *
1363 */
1364static int cipso_v4_gentag_enum(const struct cipso_v4_doi *doi_def,
1365 const struct netlbl_lsm_secattr *secattr,
1366 unsigned char *buffer,
1367 u32 buffer_len)
1368{
1369 int ret_val;
1370 u32 tag_len;
1371 u32 level;
1372
1373 if (!(secattr->flags & NETLBL_SECATTR_MLS_LVL))
1374 return -EPERM;
1375
Paul Moore16efd452008-01-29 08:37:59 -05001376 ret_val = cipso_v4_map_lvl_hton(doi_def,
1377 secattr->attr.mls.lvl,
1378 &level);
Paul Moore654bbc22006-11-29 13:18:19 -05001379 if (ret_val != 0)
1380 return ret_val;
1381
1382 if (secattr->flags & NETLBL_SECATTR_MLS_CAT) {
1383 ret_val = cipso_v4_map_cat_enum_hton(doi_def,
1384 secattr,
1385 &buffer[4],
1386 buffer_len - 4);
1387 if (ret_val < 0)
1388 return ret_val;
1389
1390 tag_len = 4 + ret_val;
1391 } else
1392 tag_len = 4;
1393
Paul Moore15c45f72008-10-10 10:16:34 -04001394 buffer[0] = CIPSO_V4_TAG_ENUM;
Paul Moore654bbc22006-11-29 13:18:19 -05001395 buffer[1] = tag_len;
1396 buffer[3] = level;
1397
1398 return tag_len;
1399}
1400
1401/**
1402 * cipso_v4_parsetag_enum - Parse a CIPSO enumerated tag
1403 * @doi_def: the DOI definition
1404 * @tag: the CIPSO tag
1405 * @secattr: the security attributes
1406 *
1407 * Description:
1408 * Parse a CIPSO enumerated tag (tag type #2) and return the security
1409 * attributes in @secattr. Return zero on success, negatives values on
1410 * failure.
1411 *
1412 */
1413static int cipso_v4_parsetag_enum(const struct cipso_v4_doi *doi_def,
1414 const unsigned char *tag,
1415 struct netlbl_lsm_secattr *secattr)
1416{
1417 int ret_val;
1418 u8 tag_len = tag[1];
1419 u32 level;
1420
1421 ret_val = cipso_v4_map_lvl_ntoh(doi_def, tag[3], &level);
1422 if (ret_val != 0)
1423 return ret_val;
Paul Moore16efd452008-01-29 08:37:59 -05001424 secattr->attr.mls.lvl = level;
Paul Moore654bbc22006-11-29 13:18:19 -05001425 secattr->flags |= NETLBL_SECATTR_MLS_LVL;
1426
1427 if (tag_len > 4) {
Paul Moore654bbc22006-11-29 13:18:19 -05001428 ret_val = cipso_v4_map_cat_enum_ntoh(doi_def,
1429 &tag[4],
1430 tag_len - 4,
1431 secattr);
1432 if (ret_val != 0) {
Paul Moore4fbe63d2014-08-01 11:17:37 -04001433 netlbl_catmap_free(secattr->attr.mls.cat);
Paul Moore654bbc22006-11-29 13:18:19 -05001434 return ret_val;
1435 }
1436
1437 secattr->flags |= NETLBL_SECATTR_MLS_CAT;
1438 }
1439
1440 return 0;
1441}
1442
1443/**
Paul Moore484b3662006-11-29 13:18:20 -05001444 * cipso_v4_gentag_rng - Generate a CIPSO ranged tag (type #5)
1445 * @doi_def: the DOI definition
1446 * @secattr: the security attributes
1447 * @buffer: the option buffer
1448 * @buffer_len: length of buffer in bytes
1449 *
1450 * Description:
1451 * Generate a CIPSO option using the ranged tag, tag type #5. Returns the
1452 * size of the tag on success, negative values on failure.
1453 *
1454 */
1455static int cipso_v4_gentag_rng(const struct cipso_v4_doi *doi_def,
1456 const struct netlbl_lsm_secattr *secattr,
1457 unsigned char *buffer,
1458 u32 buffer_len)
1459{
1460 int ret_val;
1461 u32 tag_len;
1462 u32 level;
1463
1464 if (!(secattr->flags & NETLBL_SECATTR_MLS_LVL))
1465 return -EPERM;
1466
Paul Moore16efd452008-01-29 08:37:59 -05001467 ret_val = cipso_v4_map_lvl_hton(doi_def,
1468 secattr->attr.mls.lvl,
1469 &level);
Paul Moore484b3662006-11-29 13:18:20 -05001470 if (ret_val != 0)
1471 return ret_val;
1472
1473 if (secattr->flags & NETLBL_SECATTR_MLS_CAT) {
1474 ret_val = cipso_v4_map_cat_rng_hton(doi_def,
1475 secattr,
1476 &buffer[4],
1477 buffer_len - 4);
1478 if (ret_val < 0)
1479 return ret_val;
1480
1481 tag_len = 4 + ret_val;
1482 } else
1483 tag_len = 4;
1484
Paul Moore15c45f72008-10-10 10:16:34 -04001485 buffer[0] = CIPSO_V4_TAG_RANGE;
Paul Moore484b3662006-11-29 13:18:20 -05001486 buffer[1] = tag_len;
1487 buffer[3] = level;
1488
1489 return tag_len;
1490}
1491
1492/**
1493 * cipso_v4_parsetag_rng - Parse a CIPSO ranged tag
1494 * @doi_def: the DOI definition
1495 * @tag: the CIPSO tag
1496 * @secattr: the security attributes
1497 *
1498 * Description:
1499 * Parse a CIPSO ranged tag (tag type #5) and return the security attributes
1500 * in @secattr. Return zero on success, negatives values on failure.
1501 *
1502 */
1503static int cipso_v4_parsetag_rng(const struct cipso_v4_doi *doi_def,
1504 const unsigned char *tag,
1505 struct netlbl_lsm_secattr *secattr)
1506{
1507 int ret_val;
1508 u8 tag_len = tag[1];
1509 u32 level;
1510
1511 ret_val = cipso_v4_map_lvl_ntoh(doi_def, tag[3], &level);
1512 if (ret_val != 0)
1513 return ret_val;
Paul Moore16efd452008-01-29 08:37:59 -05001514 secattr->attr.mls.lvl = level;
Paul Moore484b3662006-11-29 13:18:20 -05001515 secattr->flags |= NETLBL_SECATTR_MLS_LVL;
1516
1517 if (tag_len > 4) {
Paul Moore484b3662006-11-29 13:18:20 -05001518 ret_val = cipso_v4_map_cat_rng_ntoh(doi_def,
1519 &tag[4],
1520 tag_len - 4,
1521 secattr);
1522 if (ret_val != 0) {
Paul Moore4fbe63d2014-08-01 11:17:37 -04001523 netlbl_catmap_free(secattr->attr.mls.cat);
Paul Moore484b3662006-11-29 13:18:20 -05001524 return ret_val;
1525 }
1526
1527 secattr->flags |= NETLBL_SECATTR_MLS_CAT;
1528 }
1529
1530 return 0;
1531}
1532
1533/**
Paul Moore15c45f72008-10-10 10:16:34 -04001534 * cipso_v4_gentag_loc - Generate a CIPSO local tag (non-standard)
1535 * @doi_def: the DOI definition
1536 * @secattr: the security attributes
1537 * @buffer: the option buffer
1538 * @buffer_len: length of buffer in bytes
1539 *
1540 * Description:
1541 * Generate a CIPSO option using the local tag. Returns the size of the tag
1542 * on success, negative values on failure.
1543 *
1544 */
1545static int cipso_v4_gentag_loc(const struct cipso_v4_doi *doi_def,
1546 const struct netlbl_lsm_secattr *secattr,
1547 unsigned char *buffer,
1548 u32 buffer_len)
1549{
1550 if (!(secattr->flags & NETLBL_SECATTR_SECID))
1551 return -EPERM;
1552
1553 buffer[0] = CIPSO_V4_TAG_LOCAL;
1554 buffer[1] = CIPSO_V4_TAG_LOC_BLEN;
1555 *(u32 *)&buffer[2] = secattr->attr.secid;
1556
1557 return CIPSO_V4_TAG_LOC_BLEN;
1558}
1559
1560/**
1561 * cipso_v4_parsetag_loc - Parse a CIPSO local tag
1562 * @doi_def: the DOI definition
1563 * @tag: the CIPSO tag
1564 * @secattr: the security attributes
1565 *
1566 * Description:
1567 * Parse a CIPSO local tag and return the security attributes in @secattr.
1568 * Return zero on success, negatives values on failure.
1569 *
1570 */
1571static int cipso_v4_parsetag_loc(const struct cipso_v4_doi *doi_def,
1572 const unsigned char *tag,
1573 struct netlbl_lsm_secattr *secattr)
1574{
1575 secattr->attr.secid = *(u32 *)&tag[2];
1576 secattr->flags |= NETLBL_SECATTR_SECID;
1577
1578 return 0;
1579}
1580
1581/**
Paul Moore446fda42006-08-03 16:48:06 -07001582 * cipso_v4_validate - Validate a CIPSO option
1583 * @option: the start of the option, on error it is set to point to the error
1584 *
1585 * Description:
1586 * This routine is called to validate a CIPSO option, it checks all of the
1587 * fields to ensure that they are at least valid, see the draft snippet below
1588 * for details. If the option is valid then a zero value is returned and
1589 * the value of @option is unchanged. If the option is invalid then a
1590 * non-zero value is returned and @option is adjusted to point to the
1591 * offending portion of the option. From the IETF draft ...
1592 *
1593 * "If any field within the CIPSO options, such as the DOI identifier, is not
1594 * recognized the IP datagram is discarded and an ICMP 'parameter problem'
1595 * (type 12) is generated and returned. The ICMP code field is set to 'bad
1596 * parameter' (code 0) and the pointer is set to the start of the CIPSO field
1597 * that is unrecognized."
1598 *
1599 */
Paul Moore15c45f72008-10-10 10:16:34 -04001600int cipso_v4_validate(const struct sk_buff *skb, unsigned char **option)
Paul Moore446fda42006-08-03 16:48:06 -07001601{
1602 unsigned char *opt = *option;
1603 unsigned char *tag;
1604 unsigned char opt_iter;
1605 unsigned char err_offset = 0;
1606 u8 opt_len;
1607 u8 tag_len;
1608 struct cipso_v4_doi *doi_def = NULL;
1609 u32 tag_iter;
1610
1611 /* caller already checks for length values that are too large */
1612 opt_len = opt[1];
1613 if (opt_len < 8) {
1614 err_offset = 1;
1615 goto validate_return;
1616 }
1617
1618 rcu_read_lock();
Harvey Harrisond3e2ce32008-05-02 16:26:16 -07001619 doi_def = cipso_v4_doi_search(get_unaligned_be32(&opt[2]));
Paul Moore446fda42006-08-03 16:48:06 -07001620 if (doi_def == NULL) {
1621 err_offset = 2;
1622 goto validate_return_locked;
1623 }
1624
Paul Moore15c45f72008-10-10 10:16:34 -04001625 opt_iter = CIPSO_V4_HDR_LEN;
Paul Moore446fda42006-08-03 16:48:06 -07001626 tag = opt + opt_iter;
1627 while (opt_iter < opt_len) {
1628 for (tag_iter = 0; doi_def->tags[tag_iter] != tag[0];)
1629 if (doi_def->tags[tag_iter] == CIPSO_V4_TAG_INVALID ||
1630 ++tag_iter == CIPSO_V4_TAG_MAXCNT) {
1631 err_offset = opt_iter;
1632 goto validate_return_locked;
1633 }
1634
1635 tag_len = tag[1];
1636 if (tag_len > (opt_len - opt_iter)) {
1637 err_offset = opt_iter + 1;
1638 goto validate_return_locked;
1639 }
1640
1641 switch (tag[0]) {
1642 case CIPSO_V4_TAG_RBITMAP:
Paul Moore15c45f72008-10-10 10:16:34 -04001643 if (tag_len < CIPSO_V4_TAG_RBM_BLEN) {
Paul Moore446fda42006-08-03 16:48:06 -07001644 err_offset = opt_iter + 1;
1645 goto validate_return_locked;
1646 }
1647
1648 /* We are already going to do all the verification
1649 * necessary at the socket layer so from our point of
1650 * view it is safe to turn these checks off (and less
1651 * work), however, the CIPSO draft says we should do
1652 * all the CIPSO validations here but it doesn't
1653 * really specify _exactly_ what we need to validate
1654 * ... so, just make it a sysctl tunable. */
1655 if (cipso_v4_rbm_strictvalid) {
1656 if (cipso_v4_map_lvl_valid(doi_def,
1657 tag[3]) < 0) {
1658 err_offset = opt_iter + 3;
1659 goto validate_return_locked;
1660 }
Paul Moore15c45f72008-10-10 10:16:34 -04001661 if (tag_len > CIPSO_V4_TAG_RBM_BLEN &&
Paul Moore446fda42006-08-03 16:48:06 -07001662 cipso_v4_map_cat_rbm_valid(doi_def,
1663 &tag[4],
1664 tag_len - 4) < 0) {
1665 err_offset = opt_iter + 4;
1666 goto validate_return_locked;
1667 }
1668 }
1669 break;
Paul Moore654bbc22006-11-29 13:18:19 -05001670 case CIPSO_V4_TAG_ENUM:
Paul Moore15c45f72008-10-10 10:16:34 -04001671 if (tag_len < CIPSO_V4_TAG_ENUM_BLEN) {
Paul Moore654bbc22006-11-29 13:18:19 -05001672 err_offset = opt_iter + 1;
1673 goto validate_return_locked;
1674 }
1675
1676 if (cipso_v4_map_lvl_valid(doi_def,
1677 tag[3]) < 0) {
1678 err_offset = opt_iter + 3;
1679 goto validate_return_locked;
1680 }
Paul Moore15c45f72008-10-10 10:16:34 -04001681 if (tag_len > CIPSO_V4_TAG_ENUM_BLEN &&
Paul Moore654bbc22006-11-29 13:18:19 -05001682 cipso_v4_map_cat_enum_valid(doi_def,
1683 &tag[4],
1684 tag_len - 4) < 0) {
1685 err_offset = opt_iter + 4;
1686 goto validate_return_locked;
1687 }
1688 break;
Paul Moore484b3662006-11-29 13:18:20 -05001689 case CIPSO_V4_TAG_RANGE:
Paul Moore15c45f72008-10-10 10:16:34 -04001690 if (tag_len < CIPSO_V4_TAG_RNG_BLEN) {
Paul Moore484b3662006-11-29 13:18:20 -05001691 err_offset = opt_iter + 1;
1692 goto validate_return_locked;
1693 }
1694
1695 if (cipso_v4_map_lvl_valid(doi_def,
1696 tag[3]) < 0) {
1697 err_offset = opt_iter + 3;
1698 goto validate_return_locked;
1699 }
Paul Moore15c45f72008-10-10 10:16:34 -04001700 if (tag_len > CIPSO_V4_TAG_RNG_BLEN &&
Paul Moore484b3662006-11-29 13:18:20 -05001701 cipso_v4_map_cat_rng_valid(doi_def,
1702 &tag[4],
1703 tag_len - 4) < 0) {
1704 err_offset = opt_iter + 4;
1705 goto validate_return_locked;
1706 }
1707 break;
Paul Moore15c45f72008-10-10 10:16:34 -04001708 case CIPSO_V4_TAG_LOCAL:
1709 /* This is a non-standard tag that we only allow for
1710 * local connections, so if the incoming interface is
Paul Moore89d7ae32012-07-17 11:07:47 +00001711 * not the loopback device drop the packet. Further,
1712 * there is no legitimate reason for setting this from
1713 * userspace so reject it if skb is NULL. */
1714 if (skb == NULL || !(skb->dev->flags & IFF_LOOPBACK)) {
Paul Moore15c45f72008-10-10 10:16:34 -04001715 err_offset = opt_iter;
1716 goto validate_return_locked;
1717 }
1718 if (tag_len != CIPSO_V4_TAG_LOC_BLEN) {
1719 err_offset = opt_iter + 1;
1720 goto validate_return_locked;
1721 }
1722 break;
Paul Moore446fda42006-08-03 16:48:06 -07001723 default:
1724 err_offset = opt_iter;
1725 goto validate_return_locked;
1726 }
1727
1728 tag += tag_len;
1729 opt_iter += tag_len;
1730 }
1731
1732validate_return_locked:
1733 rcu_read_unlock();
1734validate_return:
1735 *option = opt + err_offset;
1736 return err_offset;
1737}
1738
1739/**
Lucas De Marchi25985ed2011-03-30 22:57:33 -03001740 * cipso_v4_error - Send the correct response for a bad packet
Paul Moore446fda42006-08-03 16:48:06 -07001741 * @skb: the packet
1742 * @error: the error code
1743 * @gateway: CIPSO gateway flag
1744 *
1745 * Description:
1746 * Based on the error code given in @error, send an ICMP error message back to
1747 * the originating host. From the IETF draft ...
1748 *
1749 * "If the contents of the CIPSO [option] are valid but the security label is
1750 * outside of the configured host or port label range, the datagram is
1751 * discarded and an ICMP 'destination unreachable' (type 3) is generated and
1752 * returned. The code field of the ICMP is set to 'communication with
1753 * destination network administratively prohibited' (code 9) or to
1754 * 'communication with destination host administratively prohibited'
1755 * (code 10). The value of the code is dependent on whether the originator
1756 * of the ICMP message is acting as a CIPSO host or a CIPSO gateway. The
1757 * recipient of the ICMP message MUST be able to handle either value. The
1758 * same procedure is performed if a CIPSO [option] can not be added to an
1759 * IP packet because it is too large to fit in the IP options area."
1760 *
1761 * "If the error is triggered by receipt of an ICMP message, the message is
1762 * discarded and no response is permitted (consistent with general ICMP
1763 * processing rules)."
1764 *
1765 */
1766void cipso_v4_error(struct sk_buff *skb, int error, u32 gateway)
1767{
Arnaldo Carvalho de Meloeddc9ec2007-04-20 22:47:35 -07001768 if (ip_hdr(skb)->protocol == IPPROTO_ICMP || error != -EACCES)
Paul Moore446fda42006-08-03 16:48:06 -07001769 return;
1770
1771 if (gateway)
1772 icmp_send(skb, ICMP_DEST_UNREACH, ICMP_NET_ANO, 0);
1773 else
1774 icmp_send(skb, ICMP_DEST_UNREACH, ICMP_HOST_ANO, 0);
1775}
1776
1777/**
Paul Moore948bf852008-10-10 10:16:32 -04001778 * cipso_v4_genopt - Generate a CIPSO option
1779 * @buf: the option buffer
1780 * @buf_len: the size of opt_buf
Paul Moore446fda42006-08-03 16:48:06 -07001781 * @doi_def: the CIPSO DOI to use
Paul Moore948bf852008-10-10 10:16:32 -04001782 * @secattr: the security attributes
Paul Moore446fda42006-08-03 16:48:06 -07001783 *
1784 * Description:
Paul Moore948bf852008-10-10 10:16:32 -04001785 * Generate a CIPSO option using the DOI definition and security attributes
1786 * passed to the function. Returns the length of the option on success and
1787 * negative values on failure.
Paul Moore446fda42006-08-03 16:48:06 -07001788 *
1789 */
Paul Moore948bf852008-10-10 10:16:32 -04001790static int cipso_v4_genopt(unsigned char *buf, u32 buf_len,
1791 const struct cipso_v4_doi *doi_def,
1792 const struct netlbl_lsm_secattr *secattr)
Paul Moore446fda42006-08-03 16:48:06 -07001793{
Paul Moore948bf852008-10-10 10:16:32 -04001794 int ret_val;
Paul Moore446fda42006-08-03 16:48:06 -07001795 u32 iter;
Paul Moore446fda42006-08-03 16:48:06 -07001796
Paul Moore948bf852008-10-10 10:16:32 -04001797 if (buf_len <= CIPSO_V4_HDR_LEN)
1798 return -ENOSPC;
Paul Moore91b1ed02006-11-17 17:38:49 -05001799
Paul Moore446fda42006-08-03 16:48:06 -07001800 /* XXX - This code assumes only one tag per CIPSO option which isn't
1801 * really a good assumption to make but since we only support the MAC
1802 * tags right now it is a safe assumption. */
1803 iter = 0;
1804 do {
Paul Moore91b1ed02006-11-17 17:38:49 -05001805 memset(buf, 0, buf_len);
Paul Moore446fda42006-08-03 16:48:06 -07001806 switch (doi_def->tags[iter]) {
1807 case CIPSO_V4_TAG_RBITMAP:
1808 ret_val = cipso_v4_gentag_rbm(doi_def,
Paul Moore91b1ed02006-11-17 17:38:49 -05001809 secattr,
1810 &buf[CIPSO_V4_HDR_LEN],
1811 buf_len - CIPSO_V4_HDR_LEN);
Paul Moore446fda42006-08-03 16:48:06 -07001812 break;
Paul Moore654bbc22006-11-29 13:18:19 -05001813 case CIPSO_V4_TAG_ENUM:
1814 ret_val = cipso_v4_gentag_enum(doi_def,
1815 secattr,
1816 &buf[CIPSO_V4_HDR_LEN],
1817 buf_len - CIPSO_V4_HDR_LEN);
1818 break;
Paul Moore484b3662006-11-29 13:18:20 -05001819 case CIPSO_V4_TAG_RANGE:
1820 ret_val = cipso_v4_gentag_rng(doi_def,
1821 secattr,
1822 &buf[CIPSO_V4_HDR_LEN],
1823 buf_len - CIPSO_V4_HDR_LEN);
1824 break;
Paul Moore15c45f72008-10-10 10:16:34 -04001825 case CIPSO_V4_TAG_LOCAL:
1826 ret_val = cipso_v4_gentag_loc(doi_def,
1827 secattr,
1828 &buf[CIPSO_V4_HDR_LEN],
1829 buf_len - CIPSO_V4_HDR_LEN);
1830 break;
Paul Moore446fda42006-08-03 16:48:06 -07001831 default:
Paul Moore948bf852008-10-10 10:16:32 -04001832 return -EPERM;
Paul Moore446fda42006-08-03 16:48:06 -07001833 }
1834
1835 iter++;
Paul Moore91b1ed02006-11-17 17:38:49 -05001836 } while (ret_val < 0 &&
Paul Moore446fda42006-08-03 16:48:06 -07001837 iter < CIPSO_V4_TAG_MAXCNT &&
1838 doi_def->tags[iter] != CIPSO_V4_TAG_INVALID);
Paul Moore91b1ed02006-11-17 17:38:49 -05001839 if (ret_val < 0)
Paul Moore948bf852008-10-10 10:16:32 -04001840 return ret_val;
Paul Moore91b1ed02006-11-17 17:38:49 -05001841 cipso_v4_gentag_hdr(doi_def, buf, ret_val);
Paul Moore948bf852008-10-10 10:16:32 -04001842 return CIPSO_V4_HDR_LEN + ret_val;
1843}
1844
1845/**
1846 * cipso_v4_sock_setattr - Add a CIPSO option to a socket
1847 * @sk: the socket
1848 * @doi_def: the CIPSO DOI to use
1849 * @secattr: the specific security attributes of the socket
1850 *
1851 * Description:
1852 * Set the CIPSO option on the given socket using the DOI definition and
1853 * security attributes passed to the function. This function requires
1854 * exclusive access to @sk, which means it either needs to be in the
1855 * process of being created or locked. Returns zero on success and negative
1856 * values on failure.
1857 *
1858 */
1859int cipso_v4_sock_setattr(struct sock *sk,
1860 const struct cipso_v4_doi *doi_def,
1861 const struct netlbl_lsm_secattr *secattr)
1862{
1863 int ret_val = -EPERM;
1864 unsigned char *buf = NULL;
1865 u32 buf_len;
1866 u32 opt_len;
Eric Dumazetf6d8bd02011-04-21 09:45:37 +00001867 struct ip_options_rcu *old, *opt = NULL;
Paul Moore948bf852008-10-10 10:16:32 -04001868 struct inet_sock *sk_inet;
1869 struct inet_connection_sock *sk_conn;
1870
1871 /* In the case of sock_create_lite(), the sock->sk field is not
1872 * defined yet but it is not a problem as the only users of these
1873 * "lite" PF_INET sockets are functions which do an accept() call
1874 * afterwards so we will label the socket as part of the accept(). */
1875 if (sk == NULL)
1876 return 0;
1877
1878 /* We allocate the maximum CIPSO option size here so we are probably
1879 * being a little wasteful, but it makes our life _much_ easier later
1880 * on and after all we are only talking about 40 bytes. */
1881 buf_len = CIPSO_V4_OPT_LEN_MAX;
1882 buf = kmalloc(buf_len, GFP_ATOMIC);
1883 if (buf == NULL) {
1884 ret_val = -ENOMEM;
1885 goto socket_setattr_failure;
1886 }
1887
1888 ret_val = cipso_v4_genopt(buf, buf_len, doi_def, secattr);
1889 if (ret_val < 0)
1890 goto socket_setattr_failure;
1891 buf_len = ret_val;
Paul Moore446fda42006-08-03 16:48:06 -07001892
1893 /* We can't use ip_options_get() directly because it makes a call to
1894 * ip_options_get_alloc() which allocates memory with GFP_KERNEL and
Paul Mooref8687af2006-10-30 15:22:15 -08001895 * we won't always have CAP_NET_RAW even though we _always_ want to
1896 * set the IPOPT_CIPSO option. */
Paul Moore446fda42006-08-03 16:48:06 -07001897 opt_len = (buf_len + 3) & ~3;
1898 opt = kzalloc(sizeof(*opt) + opt_len, GFP_ATOMIC);
1899 if (opt == NULL) {
1900 ret_val = -ENOMEM;
1901 goto socket_setattr_failure;
1902 }
Eric Dumazetf6d8bd02011-04-21 09:45:37 +00001903 memcpy(opt->opt.__data, buf, buf_len);
1904 opt->opt.optlen = opt_len;
1905 opt->opt.cipso = sizeof(struct iphdr);
Paul Moore446fda42006-08-03 16:48:06 -07001906 kfree(buf);
1907 buf = NULL;
Paul Moore446fda42006-08-03 16:48:06 -07001908
1909 sk_inet = inet_sk(sk);
Eric Dumazetf6d8bd02011-04-21 09:45:37 +00001910
1911 old = rcu_dereference_protected(sk_inet->inet_opt, sock_owned_by_user(sk));
Paul Moore446fda42006-08-03 16:48:06 -07001912 if (sk_inet->is_icsk) {
1913 sk_conn = inet_csk(sk);
Eric Dumazetf6d8bd02011-04-21 09:45:37 +00001914 if (old)
1915 sk_conn->icsk_ext_hdr_len -= old->opt.optlen;
1916 sk_conn->icsk_ext_hdr_len += opt->opt.optlen;
Paul Moore446fda42006-08-03 16:48:06 -07001917 sk_conn->icsk_sync_mss(sk, sk_conn->icsk_pmtu_cookie);
1918 }
Eric Dumazetf6d8bd02011-04-21 09:45:37 +00001919 rcu_assign_pointer(sk_inet->inet_opt, opt);
1920 if (old)
Paul E. McKenney4f9c8c12012-01-06 17:06:11 -08001921 kfree_rcu(old, rcu);
Paul Moore446fda42006-08-03 16:48:06 -07001922
1923 return 0;
1924
1925socket_setattr_failure:
1926 kfree(buf);
1927 kfree(opt);
1928 return ret_val;
1929}
1930
1931/**
Paul Moore389fb8002009-03-27 17:10:34 -04001932 * cipso_v4_req_setattr - Add a CIPSO option to a connection request socket
1933 * @req: the connection request socket
1934 * @doi_def: the CIPSO DOI to use
1935 * @secattr: the specific security attributes of the socket
Paul Moore014ab192008-10-10 10:16:33 -04001936 *
1937 * Description:
Paul Moore389fb8002009-03-27 17:10:34 -04001938 * Set the CIPSO option on the given socket using the DOI definition and
1939 * security attributes passed to the function. Returns zero on success and
1940 * negative values on failure.
Paul Moore014ab192008-10-10 10:16:33 -04001941 *
1942 */
Paul Moore389fb8002009-03-27 17:10:34 -04001943int cipso_v4_req_setattr(struct request_sock *req,
1944 const struct cipso_v4_doi *doi_def,
1945 const struct netlbl_lsm_secattr *secattr)
Paul Moore014ab192008-10-10 10:16:33 -04001946{
Paul Moore389fb8002009-03-27 17:10:34 -04001947 int ret_val = -EPERM;
1948 unsigned char *buf = NULL;
1949 u32 buf_len;
1950 u32 opt_len;
Eric Dumazetf6d8bd02011-04-21 09:45:37 +00001951 struct ip_options_rcu *opt = NULL;
Paul Moore389fb8002009-03-27 17:10:34 -04001952 struct inet_request_sock *req_inet;
Paul Moore014ab192008-10-10 10:16:33 -04001953
Paul Moore389fb8002009-03-27 17:10:34 -04001954 /* We allocate the maximum CIPSO option size here so we are probably
1955 * being a little wasteful, but it makes our life _much_ easier later
1956 * on and after all we are only talking about 40 bytes. */
1957 buf_len = CIPSO_V4_OPT_LEN_MAX;
1958 buf = kmalloc(buf_len, GFP_ATOMIC);
1959 if (buf == NULL) {
1960 ret_val = -ENOMEM;
1961 goto req_setattr_failure;
1962 }
1963
1964 ret_val = cipso_v4_genopt(buf, buf_len, doi_def, secattr);
1965 if (ret_val < 0)
1966 goto req_setattr_failure;
1967 buf_len = ret_val;
1968
1969 /* We can't use ip_options_get() directly because it makes a call to
1970 * ip_options_get_alloc() which allocates memory with GFP_KERNEL and
1971 * we won't always have CAP_NET_RAW even though we _always_ want to
1972 * set the IPOPT_CIPSO option. */
1973 opt_len = (buf_len + 3) & ~3;
1974 opt = kzalloc(sizeof(*opt) + opt_len, GFP_ATOMIC);
1975 if (opt == NULL) {
1976 ret_val = -ENOMEM;
1977 goto req_setattr_failure;
1978 }
Eric Dumazetf6d8bd02011-04-21 09:45:37 +00001979 memcpy(opt->opt.__data, buf, buf_len);
1980 opt->opt.optlen = opt_len;
1981 opt->opt.cipso = sizeof(struct iphdr);
Paul Moore389fb8002009-03-27 17:10:34 -04001982 kfree(buf);
1983 buf = NULL;
1984
1985 req_inet = inet_rsk(req);
1986 opt = xchg(&req_inet->opt, opt);
Eric Dumazetf6d8bd02011-04-21 09:45:37 +00001987 if (opt)
Paul E. McKenney4f9c8c12012-01-06 17:06:11 -08001988 kfree_rcu(opt, rcu);
Paul Moore389fb8002009-03-27 17:10:34 -04001989
1990 return 0;
1991
1992req_setattr_failure:
1993 kfree(buf);
1994 kfree(opt);
1995 return ret_val;
1996}
1997
1998/**
1999 * cipso_v4_delopt - Delete the CIPSO option from a set of IP options
2000 * @opt_ptr: IP option pointer
2001 *
2002 * Description:
2003 * Deletes the CIPSO IP option from a set of IP options and makes the necessary
2004 * adjustments to the IP option structure. Returns zero on success, negative
2005 * values on failure.
2006 *
2007 */
Eric Dumazetf6d8bd02011-04-21 09:45:37 +00002008static int cipso_v4_delopt(struct ip_options_rcu **opt_ptr)
Paul Moore389fb8002009-03-27 17:10:34 -04002009{
2010 int hdr_delta = 0;
Eric Dumazetf6d8bd02011-04-21 09:45:37 +00002011 struct ip_options_rcu *opt = *opt_ptr;
Paul Moore014ab192008-10-10 10:16:33 -04002012
Eric Dumazetf6d8bd02011-04-21 09:45:37 +00002013 if (opt->opt.srr || opt->opt.rr || opt->opt.ts || opt->opt.router_alert) {
Paul Moore014ab192008-10-10 10:16:33 -04002014 u8 cipso_len;
2015 u8 cipso_off;
2016 unsigned char *cipso_ptr;
2017 int iter;
2018 int optlen_new;
2019
Eric Dumazetf6d8bd02011-04-21 09:45:37 +00002020 cipso_off = opt->opt.cipso - sizeof(struct iphdr);
2021 cipso_ptr = &opt->opt.__data[cipso_off];
Paul Moore014ab192008-10-10 10:16:33 -04002022 cipso_len = cipso_ptr[1];
2023
Eric Dumazetf6d8bd02011-04-21 09:45:37 +00002024 if (opt->opt.srr > opt->opt.cipso)
2025 opt->opt.srr -= cipso_len;
2026 if (opt->opt.rr > opt->opt.cipso)
2027 opt->opt.rr -= cipso_len;
2028 if (opt->opt.ts > opt->opt.cipso)
2029 opt->opt.ts -= cipso_len;
2030 if (opt->opt.router_alert > opt->opt.cipso)
2031 opt->opt.router_alert -= cipso_len;
2032 opt->opt.cipso = 0;
Paul Moore014ab192008-10-10 10:16:33 -04002033
2034 memmove(cipso_ptr, cipso_ptr + cipso_len,
Eric Dumazetf6d8bd02011-04-21 09:45:37 +00002035 opt->opt.optlen - cipso_off - cipso_len);
Paul Moore014ab192008-10-10 10:16:33 -04002036
2037 /* determining the new total option length is tricky because of
2038 * the padding necessary, the only thing i can think to do at
2039 * this point is walk the options one-by-one, skipping the
2040 * padding at the end to determine the actual option size and
2041 * from there we can determine the new total option length */
2042 iter = 0;
2043 optlen_new = 0;
Eric Dumazetf6d8bd02011-04-21 09:45:37 +00002044 while (iter < opt->opt.optlen)
2045 if (opt->opt.__data[iter] != IPOPT_NOP) {
2046 iter += opt->opt.__data[iter + 1];
Paul Moore014ab192008-10-10 10:16:33 -04002047 optlen_new = iter;
2048 } else
2049 iter++;
Eric Dumazetf6d8bd02011-04-21 09:45:37 +00002050 hdr_delta = opt->opt.optlen;
2051 opt->opt.optlen = (optlen_new + 3) & ~3;
2052 hdr_delta -= opt->opt.optlen;
Paul Moore014ab192008-10-10 10:16:33 -04002053 } else {
2054 /* only the cipso option was present on the socket so we can
2055 * remove the entire option struct */
Paul Moore389fb8002009-03-27 17:10:34 -04002056 *opt_ptr = NULL;
Eric Dumazetf6d8bd02011-04-21 09:45:37 +00002057 hdr_delta = opt->opt.optlen;
Paul E. McKenney4f9c8c12012-01-06 17:06:11 -08002058 kfree_rcu(opt, rcu);
Paul Moore014ab192008-10-10 10:16:33 -04002059 }
2060
Paul Moore389fb8002009-03-27 17:10:34 -04002061 return hdr_delta;
2062}
2063
2064/**
2065 * cipso_v4_sock_delattr - Delete the CIPSO option from a socket
2066 * @sk: the socket
2067 *
2068 * Description:
2069 * Removes the CIPSO option from a socket, if present.
2070 *
2071 */
2072void cipso_v4_sock_delattr(struct sock *sk)
2073{
2074 int hdr_delta;
Eric Dumazetf6d8bd02011-04-21 09:45:37 +00002075 struct ip_options_rcu *opt;
Paul Moore389fb8002009-03-27 17:10:34 -04002076 struct inet_sock *sk_inet;
2077
2078 sk_inet = inet_sk(sk);
Eric Dumazetf6d8bd02011-04-21 09:45:37 +00002079 opt = rcu_dereference_protected(sk_inet->inet_opt, 1);
2080 if (opt == NULL || opt->opt.cipso == 0)
Paul Moore389fb8002009-03-27 17:10:34 -04002081 return;
2082
Eric Dumazetf6d8bd02011-04-21 09:45:37 +00002083 hdr_delta = cipso_v4_delopt(&sk_inet->inet_opt);
Paul Moore014ab192008-10-10 10:16:33 -04002084 if (sk_inet->is_icsk && hdr_delta > 0) {
2085 struct inet_connection_sock *sk_conn = inet_csk(sk);
2086 sk_conn->icsk_ext_hdr_len -= hdr_delta;
2087 sk_conn->icsk_sync_mss(sk, sk_conn->icsk_pmtu_cookie);
2088 }
2089}
2090
2091/**
Paul Moore389fb8002009-03-27 17:10:34 -04002092 * cipso_v4_req_delattr - Delete the CIPSO option from a request socket
2093 * @reg: the request socket
2094 *
2095 * Description:
2096 * Removes the CIPSO option from a request socket, if present.
2097 *
2098 */
2099void cipso_v4_req_delattr(struct request_sock *req)
2100{
Eric Dumazetf6d8bd02011-04-21 09:45:37 +00002101 struct ip_options_rcu *opt;
Paul Moore389fb8002009-03-27 17:10:34 -04002102 struct inet_request_sock *req_inet;
2103
2104 req_inet = inet_rsk(req);
2105 opt = req_inet->opt;
Eric Dumazetf6d8bd02011-04-21 09:45:37 +00002106 if (opt == NULL || opt->opt.cipso == 0)
Paul Moore389fb8002009-03-27 17:10:34 -04002107 return;
2108
2109 cipso_v4_delopt(&req_inet->opt);
2110}
2111
2112/**
Paul Moore63d804ea2007-09-15 21:45:13 -07002113 * cipso_v4_getattr - Helper function for the cipso_v4_*_getattr functions
2114 * @cipso: the CIPSO v4 option
2115 * @secattr: the security attributes
2116 *
2117 * Description:
2118 * Inspect @cipso and return the security attributes in @secattr. Returns zero
2119 * on success and negative values on failure.
2120 *
2121 */
2122static int cipso_v4_getattr(const unsigned char *cipso,
2123 struct netlbl_lsm_secattr *secattr)
2124{
2125 int ret_val = -ENOMSG;
2126 u32 doi;
2127 struct cipso_v4_doi *doi_def;
2128
2129 if (cipso_v4_cache_check(cipso, cipso[1], secattr) == 0)
2130 return 0;
2131
Harvey Harrisond3e2ce32008-05-02 16:26:16 -07002132 doi = get_unaligned_be32(&cipso[2]);
Paul Moore63d804ea2007-09-15 21:45:13 -07002133 rcu_read_lock();
2134 doi_def = cipso_v4_doi_search(doi);
2135 if (doi_def == NULL)
2136 goto getattr_return;
2137 /* XXX - This code assumes only one tag per CIPSO option which isn't
2138 * really a good assumption to make but since we only support the MAC
2139 * tags right now it is a safe assumption. */
2140 switch (cipso[6]) {
2141 case CIPSO_V4_TAG_RBITMAP:
2142 ret_val = cipso_v4_parsetag_rbm(doi_def, &cipso[6], secattr);
2143 break;
2144 case CIPSO_V4_TAG_ENUM:
2145 ret_val = cipso_v4_parsetag_enum(doi_def, &cipso[6], secattr);
2146 break;
2147 case CIPSO_V4_TAG_RANGE:
2148 ret_val = cipso_v4_parsetag_rng(doi_def, &cipso[6], secattr);
2149 break;
Paul Moore15c45f72008-10-10 10:16:34 -04002150 case CIPSO_V4_TAG_LOCAL:
2151 ret_val = cipso_v4_parsetag_loc(doi_def, &cipso[6], secattr);
2152 break;
Paul Moore63d804ea2007-09-15 21:45:13 -07002153 }
Paul Moore16efd452008-01-29 08:37:59 -05002154 if (ret_val == 0)
2155 secattr->type = NETLBL_NLTYPE_CIPSOV4;
Paul Moore63d804ea2007-09-15 21:45:13 -07002156
2157getattr_return:
2158 rcu_read_unlock();
2159 return ret_val;
2160}
2161
2162/**
Paul Moore14a72f52006-09-25 15:52:01 -07002163 * cipso_v4_sock_getattr - Get the security attributes from a sock
2164 * @sk: the sock
2165 * @secattr: the security attributes
2166 *
2167 * Description:
2168 * Query @sk to see if there is a CIPSO option attached to the sock and if
2169 * there is return the CIPSO security attributes in @secattr. This function
2170 * requires that @sk be locked, or privately held, but it does not do any
2171 * locking itself. Returns zero on success and negative values on failure.
2172 *
2173 */
2174int cipso_v4_sock_getattr(struct sock *sk, struct netlbl_lsm_secattr *secattr)
2175{
Eric Dumazetf6d8bd02011-04-21 09:45:37 +00002176 struct ip_options_rcu *opt;
2177 int res = -ENOMSG;
Paul Moore14a72f52006-09-25 15:52:01 -07002178
Eric Dumazetf6d8bd02011-04-21 09:45:37 +00002179 rcu_read_lock();
2180 opt = rcu_dereference(inet_sk(sk)->inet_opt);
2181 if (opt && opt->opt.cipso)
2182 res = cipso_v4_getattr(opt->opt.__data +
2183 opt->opt.cipso -
2184 sizeof(struct iphdr),
2185 secattr);
2186 rcu_read_unlock();
2187 return res;
Paul Moore14a72f52006-09-25 15:52:01 -07002188}
2189
2190/**
Paul Moore948bf852008-10-10 10:16:32 -04002191 * cipso_v4_skbuff_setattr - Set the CIPSO option on a packet
2192 * @skb: the packet
2193 * @secattr: the security attributes
2194 *
2195 * Description:
2196 * Set the CIPSO option on the given packet based on the security attributes.
2197 * Returns a pointer to the IP header on success and NULL on failure.
2198 *
2199 */
2200int cipso_v4_skbuff_setattr(struct sk_buff *skb,
2201 const struct cipso_v4_doi *doi_def,
2202 const struct netlbl_lsm_secattr *secattr)
2203{
2204 int ret_val;
2205 struct iphdr *iph;
2206 struct ip_options *opt = &IPCB(skb)->opt;
2207 unsigned char buf[CIPSO_V4_OPT_LEN_MAX];
2208 u32 buf_len = CIPSO_V4_OPT_LEN_MAX;
2209 u32 opt_len;
2210 int len_delta;
2211
roel kluin00af5c62008-10-29 15:55:53 -04002212 ret_val = cipso_v4_genopt(buf, buf_len, doi_def, secattr);
2213 if (ret_val < 0)
2214 return ret_val;
2215 buf_len = ret_val;
Paul Moore948bf852008-10-10 10:16:32 -04002216 opt_len = (buf_len + 3) & ~3;
2217
2218 /* we overwrite any existing options to ensure that we have enough
2219 * room for the CIPSO option, the reason is that we _need_ to guarantee
2220 * that the security label is applied to the packet - we do the same
2221 * thing when using the socket options and it hasn't caused a problem,
2222 * if we need to we can always revisit this choice later */
2223
2224 len_delta = opt_len - opt->optlen;
2225 /* if we don't ensure enough headroom we could panic on the skb_push()
2226 * call below so make sure we have enough, we are also "mangling" the
2227 * packet so we should probably do a copy-on-write call anyway */
2228 ret_val = skb_cow(skb, skb_headroom(skb) + len_delta);
2229 if (ret_val < 0)
2230 return ret_val;
2231
2232 if (len_delta > 0) {
2233 /* we assume that the header + opt->optlen have already been
2234 * "pushed" in ip_options_build() or similar */
2235 iph = ip_hdr(skb);
2236 skb_push(skb, len_delta);
2237 memmove((char *)iph - len_delta, iph, iph->ihl << 2);
2238 skb_reset_network_header(skb);
2239 iph = ip_hdr(skb);
2240 } else if (len_delta < 0) {
2241 iph = ip_hdr(skb);
2242 memset(iph + 1, IPOPT_NOP, opt->optlen);
2243 } else
2244 iph = ip_hdr(skb);
2245
2246 if (opt->optlen > 0)
2247 memset(opt, 0, sizeof(*opt));
2248 opt->optlen = opt_len;
2249 opt->cipso = sizeof(struct iphdr);
2250 opt->is_changed = 1;
2251
2252 /* we have to do the following because we are being called from a
2253 * netfilter hook which means the packet already has had the header
2254 * fields populated and the checksum calculated - yes this means we
2255 * are doing more work than needed but we do it to keep the core
2256 * stack clean and tidy */
2257 memcpy(iph + 1, buf, buf_len);
2258 if (opt_len > buf_len)
2259 memset((char *)(iph + 1) + buf_len, 0, opt_len - buf_len);
2260 if (len_delta != 0) {
2261 iph->ihl = 5 + (opt_len >> 2);
2262 iph->tot_len = htons(skb->len);
2263 }
2264 ip_send_check(iph);
2265
2266 return 0;
2267}
2268
2269/**
2270 * cipso_v4_skbuff_delattr - Delete any CIPSO options from a packet
2271 * @skb: the packet
2272 *
2273 * Description:
2274 * Removes any and all CIPSO options from the given packet. Returns zero on
2275 * success, negative values on failure.
2276 *
2277 */
2278int cipso_v4_skbuff_delattr(struct sk_buff *skb)
2279{
2280 int ret_val;
2281 struct iphdr *iph;
2282 struct ip_options *opt = &IPCB(skb)->opt;
2283 unsigned char *cipso_ptr;
2284
2285 if (opt->cipso == 0)
2286 return 0;
2287
2288 /* since we are changing the packet we should make a copy */
2289 ret_val = skb_cow(skb, skb_headroom(skb));
2290 if (ret_val < 0)
2291 return ret_val;
2292
2293 /* the easiest thing to do is just replace the cipso option with noop
2294 * options since we don't change the size of the packet, although we
2295 * still need to recalculate the checksum */
2296
2297 iph = ip_hdr(skb);
2298 cipso_ptr = (unsigned char *)iph + opt->cipso;
2299 memset(cipso_ptr, IPOPT_NOOP, cipso_ptr[1]);
2300 opt->cipso = 0;
2301 opt->is_changed = 1;
2302
2303 ip_send_check(iph);
2304
2305 return 0;
2306}
2307
2308/**
Paul Moore446fda42006-08-03 16:48:06 -07002309 * cipso_v4_skbuff_getattr - Get the security attributes from the CIPSO option
2310 * @skb: the packet
2311 * @secattr: the security attributes
2312 *
2313 * Description:
2314 * Parse the given packet's CIPSO option and return the security attributes.
2315 * Returns zero on success and negative values on failure.
2316 *
2317 */
2318int cipso_v4_skbuff_getattr(const struct sk_buff *skb,
2319 struct netlbl_lsm_secattr *secattr)
2320{
Paul Moore63d804ea2007-09-15 21:45:13 -07002321 return cipso_v4_getattr(CIPSO_V4_OPTPTR(skb), secattr);
Paul Moore446fda42006-08-03 16:48:06 -07002322}
2323
2324/*
2325 * Setup Functions
2326 */
2327
2328/**
2329 * cipso_v4_init - Initialize the CIPSO module
2330 *
2331 * Description:
2332 * Initialize the CIPSO module and prepare it for use. Returns zero on success
2333 * and negative values on failure.
2334 *
2335 */
2336static int __init cipso_v4_init(void)
2337{
2338 int ret_val;
2339
2340 ret_val = cipso_v4_cache_init();
2341 if (ret_val != 0)
2342 panic("Failed to initialize the CIPSO/IPv4 cache (%d)\n",
2343 ret_val);
2344
2345 return 0;
2346}
2347
2348subsys_initcall(cipso_v4_init);