NetLabel: fixup the handling of CIPSOv4 tags to allow for multiple tag types
[linux-2.6-block.git] / net / ipv4 / cipso_ipv4.c
CommitLineData
446fda4f
PM
1/*
2 * CIPSO - Commercial IP Security Option
3 *
4 * This is an implementation of the CIPSO 2.2 protocol as specified in
5 * draft-ietf-cipso-ipsecurity-01.txt with additional tag types as found in
6 * FIPS-188, copies of both documents can be found in the Documentation
7 * directory. While CIPSO never became a full IETF RFC standard many vendors
8 * have chosen to adopt the protocol and over the years it has become a
9 * de-facto standard for labeled networking.
10 *
11 * Author: Paul Moore <paul.moore@hp.com>
12 *
13 */
14
15/*
16 * (c) Copyright Hewlett-Packard Development Company, L.P., 2006
17 *
18 * This program is free software; you can redistribute it and/or modify
19 * it under the terms of the GNU General Public License as published by
20 * the Free Software Foundation; either version 2 of the License, or
21 * (at your option) any later version.
22 *
23 * This program is distributed in the hope that it will be useful,
24 * but WITHOUT ANY WARRANTY; without even the implied warranty of
25 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See
26 * the GNU General Public License for more details.
27 *
28 * You should have received a copy of the GNU General Public License
29 * along with this program; if not, write to the Free Software
30 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
31 *
32 */
33
34#include <linux/init.h>
35#include <linux/types.h>
36#include <linux/rcupdate.h>
37#include <linux/list.h>
38#include <linux/spinlock.h>
39#include <linux/string.h>
40#include <linux/jhash.h>
41#include <net/ip.h>
42#include <net/icmp.h>
43#include <net/tcp.h>
44#include <net/netlabel.h>
45#include <net/cipso_ipv4.h>
ffb733c6 46#include <asm/atomic.h>
446fda4f
PM
47#include <asm/bug.h>
48
49struct cipso_v4_domhsh_entry {
50 char *domain;
51 u32 valid;
52 struct list_head list;
53 struct rcu_head rcu;
54};
55
56/* List of available DOI definitions */
57/* XXX - Updates should be minimal so having a single lock for the
58 * cipso_v4_doi_list and the cipso_v4_doi_list->dom_list should be
59 * okay. */
60/* XXX - This currently assumes a minimal number of different DOIs in use,
61 * if in practice there are a lot of different DOIs this list should
62 * probably be turned into a hash table or something similar so we
63 * can do quick lookups. */
8ce11e6a 64static DEFINE_SPINLOCK(cipso_v4_doi_list_lock);
446fda4f
PM
65static struct list_head cipso_v4_doi_list = LIST_HEAD_INIT(cipso_v4_doi_list);
66
67/* Label mapping cache */
68int cipso_v4_cache_enabled = 1;
69int cipso_v4_cache_bucketsize = 10;
70#define CIPSO_V4_CACHE_BUCKETBITS 7
71#define CIPSO_V4_CACHE_BUCKETS (1 << CIPSO_V4_CACHE_BUCKETBITS)
72#define CIPSO_V4_CACHE_REORDERLIMIT 10
73struct cipso_v4_map_cache_bkt {
74 spinlock_t lock;
75 u32 size;
76 struct list_head list;
77};
78struct cipso_v4_map_cache_entry {
79 u32 hash;
80 unsigned char *key;
81 size_t key_len;
82
ffb733c6 83 struct netlbl_lsm_cache *lsm_data;
446fda4f
PM
84
85 u32 activity;
86 struct list_head list;
87};
88static struct cipso_v4_map_cache_bkt *cipso_v4_cache = NULL;
89
90/* Restricted bitmap (tag #1) flags */
91int cipso_v4_rbm_optfmt = 0;
92int cipso_v4_rbm_strictvalid = 1;
93
94/*
95 * Helper Functions
96 */
97
98/**
99 * cipso_v4_bitmap_walk - Walk a bitmap looking for a bit
100 * @bitmap: the bitmap
101 * @bitmap_len: length in bits
102 * @offset: starting offset
103 * @state: if non-zero, look for a set (1) bit else look for a cleared (0) bit
104 *
105 * Description:
106 * Starting at @offset, walk the bitmap from left to right until either the
107 * desired bit is found or we reach the end. Return the bit offset, -1 if
108 * not found, or -2 if error.
109 */
110static int cipso_v4_bitmap_walk(const unsigned char *bitmap,
111 u32 bitmap_len,
112 u32 offset,
113 u8 state)
114{
115 u32 bit_spot;
116 u32 byte_offset;
117 unsigned char bitmask;
118 unsigned char byte;
119
120 /* gcc always rounds to zero when doing integer division */
121 byte_offset = offset / 8;
122 byte = bitmap[byte_offset];
123 bit_spot = offset;
124 bitmask = 0x80 >> (offset % 8);
125
126 while (bit_spot < bitmap_len) {
127 if ((state && (byte & bitmask) == bitmask) ||
128 (state == 0 && (byte & bitmask) == 0))
129 return bit_spot;
130
131 bit_spot++;
132 bitmask >>= 1;
133 if (bitmask == 0) {
134 byte = bitmap[++byte_offset];
135 bitmask = 0x80;
136 }
137 }
138
139 return -1;
140}
141
142/**
143 * cipso_v4_bitmap_setbit - Sets a single bit in a bitmap
144 * @bitmap: the bitmap
145 * @bit: the bit
146 * @state: if non-zero, set the bit (1) else clear the bit (0)
147 *
148 * Description:
149 * Set a single bit in the bitmask. Returns zero on success, negative values
150 * on error.
151 */
152static void cipso_v4_bitmap_setbit(unsigned char *bitmap,
153 u32 bit,
154 u8 state)
155{
156 u32 byte_spot;
157 u8 bitmask;
158
159 /* gcc always rounds to zero when doing integer division */
160 byte_spot = bit / 8;
161 bitmask = 0x80 >> (bit % 8);
162 if (state)
163 bitmap[byte_spot] |= bitmask;
164 else
165 bitmap[byte_spot] &= ~bitmask;
166}
167
168/**
169 * cipso_v4_doi_domhsh_free - Frees a domain list entry
170 * @entry: the entry's RCU field
171 *
172 * Description:
173 * This function is designed to be used as a callback to the call_rcu()
174 * function so that the memory allocated to a domain list entry can be released
175 * safely.
176 *
177 */
178static void cipso_v4_doi_domhsh_free(struct rcu_head *entry)
179{
180 struct cipso_v4_domhsh_entry *ptr;
181
182 ptr = container_of(entry, struct cipso_v4_domhsh_entry, rcu);
183 kfree(ptr->domain);
184 kfree(ptr);
185}
186
187/**
188 * cipso_v4_cache_entry_free - Frees a cache entry
189 * @entry: the entry to free
190 *
191 * Description:
ffb733c6 192 * This function frees the memory associated with a cache entry including the
193 * LSM cache data if there are no longer any users, i.e. reference count == 0.
446fda4f
PM
194 *
195 */
196static void cipso_v4_cache_entry_free(struct cipso_v4_map_cache_entry *entry)
197{
ffb733c6 198 if (entry->lsm_data)
199 netlbl_secattr_cache_free(entry->lsm_data);
446fda4f
PM
200 kfree(entry->key);
201 kfree(entry);
202}
203
204/**
205 * cipso_v4_map_cache_hash - Hashing function for the CIPSO cache
206 * @key: the hash key
207 * @key_len: the length of the key in bytes
208 *
209 * Description:
210 * The CIPSO tag hashing function. Returns a 32-bit hash value.
211 *
212 */
213static u32 cipso_v4_map_cache_hash(const unsigned char *key, u32 key_len)
214{
215 return jhash(key, key_len, 0);
216}
217
218/*
219 * Label Mapping Cache Functions
220 */
221
222/**
223 * cipso_v4_cache_init - Initialize the CIPSO cache
224 *
225 * Description:
226 * Initializes the CIPSO label mapping cache, this function should be called
227 * before any of the other functions defined in this file. Returns zero on
228 * success, negative values on error.
229 *
230 */
231static int cipso_v4_cache_init(void)
232{
233 u32 iter;
234
235 cipso_v4_cache = kcalloc(CIPSO_V4_CACHE_BUCKETS,
236 sizeof(struct cipso_v4_map_cache_bkt),
237 GFP_KERNEL);
238 if (cipso_v4_cache == NULL)
239 return -ENOMEM;
240
241 for (iter = 0; iter < CIPSO_V4_CACHE_BUCKETS; iter++) {
242 spin_lock_init(&cipso_v4_cache[iter].lock);
243 cipso_v4_cache[iter].size = 0;
244 INIT_LIST_HEAD(&cipso_v4_cache[iter].list);
245 }
246
247 return 0;
248}
249
250/**
251 * cipso_v4_cache_invalidate - Invalidates the current CIPSO cache
252 *
253 * Description:
254 * Invalidates and frees any entries in the CIPSO cache. Returns zero on
255 * success and negative values on failure.
256 *
257 */
258void cipso_v4_cache_invalidate(void)
259{
260 struct cipso_v4_map_cache_entry *entry, *tmp_entry;
261 u32 iter;
262
263 for (iter = 0; iter < CIPSO_V4_CACHE_BUCKETS; iter++) {
609c92fe 264 spin_lock_bh(&cipso_v4_cache[iter].lock);
446fda4f
PM
265 list_for_each_entry_safe(entry,
266 tmp_entry,
267 &cipso_v4_cache[iter].list, list) {
268 list_del(&entry->list);
269 cipso_v4_cache_entry_free(entry);
270 }
271 cipso_v4_cache[iter].size = 0;
609c92fe 272 spin_unlock_bh(&cipso_v4_cache[iter].lock);
446fda4f
PM
273 }
274
275 return;
276}
277
278/**
279 * cipso_v4_cache_check - Check the CIPSO cache for a label mapping
280 * @key: the buffer to check
281 * @key_len: buffer length in bytes
282 * @secattr: the security attribute struct to use
283 *
284 * Description:
285 * This function checks the cache to see if a label mapping already exists for
286 * the given key. If there is a match then the cache is adjusted and the
287 * @secattr struct is populated with the correct LSM security attributes. The
288 * cache is adjusted in the following manner if the entry is not already the
289 * first in the cache bucket:
290 *
291 * 1. The cache entry's activity counter is incremented
292 * 2. The previous (higher ranking) entry's activity counter is decremented
293 * 3. If the difference between the two activity counters is geater than
294 * CIPSO_V4_CACHE_REORDERLIMIT the two entries are swapped
295 *
296 * Returns zero on success, -ENOENT for a cache miss, and other negative values
297 * on error.
298 *
299 */
300static int cipso_v4_cache_check(const unsigned char *key,
301 u32 key_len,
302 struct netlbl_lsm_secattr *secattr)
303{
304 u32 bkt;
305 struct cipso_v4_map_cache_entry *entry;
306 struct cipso_v4_map_cache_entry *prev_entry = NULL;
307 u32 hash;
308
309 if (!cipso_v4_cache_enabled)
310 return -ENOENT;
311
312 hash = cipso_v4_map_cache_hash(key, key_len);
313 bkt = hash & (CIPSO_V4_CACHE_BUCKETBITS - 1);
609c92fe 314 spin_lock_bh(&cipso_v4_cache[bkt].lock);
446fda4f
PM
315 list_for_each_entry(entry, &cipso_v4_cache[bkt].list, list) {
316 if (entry->hash == hash &&
317 entry->key_len == key_len &&
318 memcmp(entry->key, key, key_len) == 0) {
319 entry->activity += 1;
ffb733c6 320 atomic_inc(&entry->lsm_data->refcount);
321 secattr->cache = entry->lsm_data;
701a90ba 322 secattr->flags |= NETLBL_SECATTR_CACHE;
446fda4f 323 if (prev_entry == NULL) {
609c92fe 324 spin_unlock_bh(&cipso_v4_cache[bkt].lock);
446fda4f
PM
325 return 0;
326 }
327
328 if (prev_entry->activity > 0)
329 prev_entry->activity -= 1;
330 if (entry->activity > prev_entry->activity &&
331 entry->activity - prev_entry->activity >
332 CIPSO_V4_CACHE_REORDERLIMIT) {
333 __list_del(entry->list.prev, entry->list.next);
334 __list_add(&entry->list,
335 prev_entry->list.prev,
336 &prev_entry->list);
337 }
338
609c92fe 339 spin_unlock_bh(&cipso_v4_cache[bkt].lock);
446fda4f
PM
340 return 0;
341 }
342 prev_entry = entry;
343 }
609c92fe 344 spin_unlock_bh(&cipso_v4_cache[bkt].lock);
446fda4f
PM
345
346 return -ENOENT;
347}
348
349/**
350 * cipso_v4_cache_add - Add an entry to the CIPSO cache
351 * @skb: the packet
352 * @secattr: the packet's security attributes
353 *
354 * Description:
355 * Add a new entry into the CIPSO label mapping cache. Add the new entry to
356 * head of the cache bucket's list, if the cache bucket is out of room remove
357 * the last entry in the list first. It is important to note that there is
358 * currently no checking for duplicate keys. Returns zero on success,
359 * negative values on failure.
360 *
361 */
362int cipso_v4_cache_add(const struct sk_buff *skb,
363 const struct netlbl_lsm_secattr *secattr)
364{
365 int ret_val = -EPERM;
366 u32 bkt;
367 struct cipso_v4_map_cache_entry *entry = NULL;
368 struct cipso_v4_map_cache_entry *old_entry = NULL;
369 unsigned char *cipso_ptr;
370 u32 cipso_ptr_len;
371
372 if (!cipso_v4_cache_enabled || cipso_v4_cache_bucketsize <= 0)
373 return 0;
374
375 cipso_ptr = CIPSO_V4_OPTPTR(skb);
376 cipso_ptr_len = cipso_ptr[1];
377
378 entry = kzalloc(sizeof(*entry), GFP_ATOMIC);
379 if (entry == NULL)
380 return -ENOMEM;
fac5d731 381 entry->key = kmemdup(cipso_ptr, cipso_ptr_len, GFP_ATOMIC);
446fda4f
PM
382 if (entry->key == NULL) {
383 ret_val = -ENOMEM;
384 goto cache_add_failure;
385 }
446fda4f
PM
386 entry->key_len = cipso_ptr_len;
387 entry->hash = cipso_v4_map_cache_hash(cipso_ptr, cipso_ptr_len);
ffb733c6 388 atomic_inc(&secattr->cache->refcount);
389 entry->lsm_data = secattr->cache;
446fda4f
PM
390
391 bkt = entry->hash & (CIPSO_V4_CACHE_BUCKETBITS - 1);
609c92fe 392 spin_lock_bh(&cipso_v4_cache[bkt].lock);
446fda4f
PM
393 if (cipso_v4_cache[bkt].size < cipso_v4_cache_bucketsize) {
394 list_add(&entry->list, &cipso_v4_cache[bkt].list);
395 cipso_v4_cache[bkt].size += 1;
396 } else {
397 old_entry = list_entry(cipso_v4_cache[bkt].list.prev,
398 struct cipso_v4_map_cache_entry, list);
399 list_del(&old_entry->list);
400 list_add(&entry->list, &cipso_v4_cache[bkt].list);
401 cipso_v4_cache_entry_free(old_entry);
402 }
609c92fe 403 spin_unlock_bh(&cipso_v4_cache[bkt].lock);
446fda4f
PM
404
405 return 0;
406
407cache_add_failure:
408 if (entry)
409 cipso_v4_cache_entry_free(entry);
410 return ret_val;
411}
412
413/*
414 * DOI List Functions
415 */
416
417/**
418 * cipso_v4_doi_search - Searches for a DOI definition
419 * @doi: the DOI to search for
420 *
421 * Description:
422 * Search the DOI definition list for a DOI definition with a DOI value that
423 * matches @doi. The caller is responsibile for calling rcu_read_[un]lock().
424 * Returns a pointer to the DOI definition on success and NULL on failure.
425 */
426static struct cipso_v4_doi *cipso_v4_doi_search(u32 doi)
427{
428 struct cipso_v4_doi *iter;
429
430 list_for_each_entry_rcu(iter, &cipso_v4_doi_list, list)
431 if (iter->doi == doi && iter->valid)
432 return iter;
433 return NULL;
434}
435
436/**
437 * cipso_v4_doi_add - Add a new DOI to the CIPSO protocol engine
438 * @doi_def: the DOI structure
439 *
440 * Description:
441 * The caller defines a new DOI for use by the CIPSO engine and calls this
442 * function to add it to the list of acceptable domains. The caller must
443 * ensure that the mapping table specified in @doi_def->map meets all of the
444 * requirements of the mapping type (see cipso_ipv4.h for details). Returns
445 * zero on success and non-zero on failure.
446 *
447 */
448int cipso_v4_doi_add(struct cipso_v4_doi *doi_def)
449{
6ce61a7c
PM
450 u32 iter;
451
446fda4f
PM
452 if (doi_def == NULL || doi_def->doi == CIPSO_V4_DOI_UNKNOWN)
453 return -EINVAL;
6ce61a7c
PM
454 for (iter = 0; iter < CIPSO_V4_TAG_MAXCNT; iter++) {
455 switch (doi_def->tags[iter]) {
456 case CIPSO_V4_TAG_RBITMAP:
457 break;
458 case CIPSO_V4_TAG_INVALID:
459 if (iter == 0)
460 return -EINVAL;
461 break;
462 default:
463 return -EINVAL;
464 }
465 }
446fda4f
PM
466
467 doi_def->valid = 1;
468 INIT_RCU_HEAD(&doi_def->rcu);
469 INIT_LIST_HEAD(&doi_def->dom_list);
470
471 rcu_read_lock();
472 if (cipso_v4_doi_search(doi_def->doi) != NULL)
473 goto doi_add_failure_rlock;
474 spin_lock(&cipso_v4_doi_list_lock);
475 if (cipso_v4_doi_search(doi_def->doi) != NULL)
476 goto doi_add_failure_slock;
477 list_add_tail_rcu(&doi_def->list, &cipso_v4_doi_list);
478 spin_unlock(&cipso_v4_doi_list_lock);
479 rcu_read_unlock();
480
481 return 0;
482
483doi_add_failure_slock:
484 spin_unlock(&cipso_v4_doi_list_lock);
485doi_add_failure_rlock:
486 rcu_read_unlock();
487 return -EEXIST;
488}
489
490/**
491 * cipso_v4_doi_remove - Remove an existing DOI from the CIPSO protocol engine
492 * @doi: the DOI value
32f50cde 493 * @audit_secid: the LSM secid to use in the audit message
446fda4f
PM
494 * @callback: the DOI cleanup/free callback
495 *
496 * Description:
497 * Removes a DOI definition from the CIPSO engine, @callback is called to
498 * free any memory. The NetLabel routines will be called to release their own
499 * LSM domain mappings as well as our own domain list. Returns zero on
500 * success and negative values on failure.
501 *
502 */
32f50cde 503int cipso_v4_doi_remove(u32 doi,
95d4e6be 504 struct netlbl_audit *audit_info,
32f50cde 505 void (*callback) (struct rcu_head * head))
446fda4f
PM
506{
507 struct cipso_v4_doi *doi_def;
508 struct cipso_v4_domhsh_entry *dom_iter;
509
510 rcu_read_lock();
511 if (cipso_v4_doi_search(doi) != NULL) {
512 spin_lock(&cipso_v4_doi_list_lock);
513 doi_def = cipso_v4_doi_search(doi);
514 if (doi_def == NULL) {
515 spin_unlock(&cipso_v4_doi_list_lock);
516 rcu_read_unlock();
517 return -ENOENT;
518 }
519 doi_def->valid = 0;
520 list_del_rcu(&doi_def->list);
521 spin_unlock(&cipso_v4_doi_list_lock);
522 list_for_each_entry_rcu(dom_iter, &doi_def->dom_list, list)
523 if (dom_iter->valid)
32f50cde 524 netlbl_domhsh_remove(dom_iter->domain,
95d4e6be 525 audit_info);
446fda4f
PM
526 cipso_v4_cache_invalidate();
527 rcu_read_unlock();
528
529 call_rcu(&doi_def->rcu, callback);
530 return 0;
531 }
532 rcu_read_unlock();
533
534 return -ENOENT;
535}
536
537/**
538 * cipso_v4_doi_getdef - Returns a pointer to a valid DOI definition
539 * @doi: the DOI value
540 *
541 * Description:
542 * Searches for a valid DOI definition and if one is found it is returned to
543 * the caller. Otherwise NULL is returned. The caller must ensure that
544 * rcu_read_lock() is held while accessing the returned definition.
545 *
546 */
547struct cipso_v4_doi *cipso_v4_doi_getdef(u32 doi)
548{
549 return cipso_v4_doi_search(doi);
550}
551
552/**
fcd48280
PM
553 * cipso_v4_doi_walk - Iterate through the DOI definitions
554 * @skip_cnt: skip past this number of DOI definitions, updated
555 * @callback: callback for each DOI definition
556 * @cb_arg: argument for the callback function
446fda4f
PM
557 *
558 * Description:
fcd48280
PM
559 * Iterate over the DOI definition list, skipping the first @skip_cnt entries.
560 * For each entry call @callback, if @callback returns a negative value stop
561 * 'walking' through the list and return. Updates the value in @skip_cnt upon
562 * return. Returns zero on success, negative values on failure.
446fda4f
PM
563 *
564 */
fcd48280
PM
565int cipso_v4_doi_walk(u32 *skip_cnt,
566 int (*callback) (struct cipso_v4_doi *doi_def, void *arg),
567 void *cb_arg)
446fda4f 568{
fcd48280 569 int ret_val = -ENOENT;
446fda4f 570 u32 doi_cnt = 0;
fcd48280 571 struct cipso_v4_doi *iter_doi;
446fda4f 572
446fda4f 573 rcu_read_lock();
fcd48280
PM
574 list_for_each_entry_rcu(iter_doi, &cipso_v4_doi_list, list)
575 if (iter_doi->valid) {
576 if (doi_cnt++ < *skip_cnt)
577 continue;
578 ret_val = callback(iter_doi, cb_arg);
579 if (ret_val < 0) {
580 doi_cnt--;
581 goto doi_walk_return;
446fda4f 582 }
446fda4f 583 }
446fda4f 584
fcd48280 585doi_walk_return:
446fda4f 586 rcu_read_unlock();
fcd48280
PM
587 *skip_cnt = doi_cnt;
588 return ret_val;
446fda4f
PM
589}
590
591/**
592 * cipso_v4_doi_domhsh_add - Adds a domain entry to a DOI definition
593 * @doi_def: the DOI definition
594 * @domain: the domain to add
595 *
596 * Description:
597 * Adds the @domain to the the DOI specified by @doi_def, this function
598 * should only be called by external functions (i.e. NetLabel). This function
599 * does allocate memory. Returns zero on success, negative values on failure.
600 *
601 */
602int cipso_v4_doi_domhsh_add(struct cipso_v4_doi *doi_def, const char *domain)
603{
604 struct cipso_v4_domhsh_entry *iter;
605 struct cipso_v4_domhsh_entry *new_dom;
606
607 new_dom = kzalloc(sizeof(*new_dom), GFP_KERNEL);
608 if (new_dom == NULL)
609 return -ENOMEM;
610 if (domain) {
611 new_dom->domain = kstrdup(domain, GFP_KERNEL);
612 if (new_dom->domain == NULL) {
613 kfree(new_dom);
614 return -ENOMEM;
615 }
616 }
617 new_dom->valid = 1;
618 INIT_RCU_HEAD(&new_dom->rcu);
619
620 rcu_read_lock();
621 spin_lock(&cipso_v4_doi_list_lock);
622 list_for_each_entry_rcu(iter, &doi_def->dom_list, list)
623 if (iter->valid &&
624 ((domain != NULL && iter->domain != NULL &&
625 strcmp(iter->domain, domain) == 0) ||
626 (domain == NULL && iter->domain == NULL))) {
627 spin_unlock(&cipso_v4_doi_list_lock);
628 rcu_read_unlock();
629 kfree(new_dom->domain);
630 kfree(new_dom);
631 return -EEXIST;
632 }
633 list_add_tail_rcu(&new_dom->list, &doi_def->dom_list);
634 spin_unlock(&cipso_v4_doi_list_lock);
635 rcu_read_unlock();
636
637 return 0;
638}
639
640/**
641 * cipso_v4_doi_domhsh_remove - Removes a domain entry from a DOI definition
642 * @doi_def: the DOI definition
643 * @domain: the domain to remove
644 *
645 * Description:
646 * Removes the @domain from the DOI specified by @doi_def, this function
647 * should only be called by external functions (i.e. NetLabel). Returns zero
648 * on success and negative values on error.
649 *
650 */
651int cipso_v4_doi_domhsh_remove(struct cipso_v4_doi *doi_def,
652 const char *domain)
653{
654 struct cipso_v4_domhsh_entry *iter;
655
656 rcu_read_lock();
657 spin_lock(&cipso_v4_doi_list_lock);
658 list_for_each_entry_rcu(iter, &doi_def->dom_list, list)
659 if (iter->valid &&
660 ((domain != NULL && iter->domain != NULL &&
661 strcmp(iter->domain, domain) == 0) ||
662 (domain == NULL && iter->domain == NULL))) {
663 iter->valid = 0;
664 list_del_rcu(&iter->list);
665 spin_unlock(&cipso_v4_doi_list_lock);
666 rcu_read_unlock();
667 call_rcu(&iter->rcu, cipso_v4_doi_domhsh_free);
668
669 return 0;
670 }
671 spin_unlock(&cipso_v4_doi_list_lock);
672 rcu_read_unlock();
673
674 return -ENOENT;
675}
676
677/*
678 * Label Mapping Functions
679 */
680
681/**
682 * cipso_v4_map_lvl_valid - Checks to see if the given level is understood
683 * @doi_def: the DOI definition
684 * @level: the level to check
685 *
686 * Description:
687 * Checks the given level against the given DOI definition and returns a
688 * negative value if the level does not have a valid mapping and a zero value
689 * if the level is defined by the DOI.
690 *
691 */
692static int cipso_v4_map_lvl_valid(const struct cipso_v4_doi *doi_def, u8 level)
693{
694 switch (doi_def->type) {
695 case CIPSO_V4_MAP_PASS:
696 return 0;
697 case CIPSO_V4_MAP_STD:
698 if (doi_def->map.std->lvl.cipso[level] < CIPSO_V4_INV_LVL)
699 return 0;
700 break;
701 }
702
703 return -EFAULT;
704}
705
706/**
707 * cipso_v4_map_lvl_hton - Perform a level mapping from the host to the network
708 * @doi_def: the DOI definition
709 * @host_lvl: the host MLS level
710 * @net_lvl: the network/CIPSO MLS level
711 *
712 * Description:
713 * Perform a label mapping to translate a local MLS level to the correct
714 * CIPSO level using the given DOI definition. Returns zero on success,
715 * negative values otherwise.
716 *
717 */
718static int cipso_v4_map_lvl_hton(const struct cipso_v4_doi *doi_def,
719 u32 host_lvl,
720 u32 *net_lvl)
721{
722 switch (doi_def->type) {
723 case CIPSO_V4_MAP_PASS:
724 *net_lvl = host_lvl;
725 return 0;
726 case CIPSO_V4_MAP_STD:
727 if (host_lvl < doi_def->map.std->lvl.local_size) {
728 *net_lvl = doi_def->map.std->lvl.local[host_lvl];
729 return 0;
730 }
731 break;
732 }
733
734 return -EINVAL;
735}
736
737/**
738 * cipso_v4_map_lvl_ntoh - Perform a level mapping from the network to the host
739 * @doi_def: the DOI definition
740 * @net_lvl: the network/CIPSO MLS level
741 * @host_lvl: the host MLS level
742 *
743 * Description:
744 * Perform a label mapping to translate a CIPSO level to the correct local MLS
745 * level using the given DOI definition. Returns zero on success, negative
746 * values otherwise.
747 *
748 */
749static int cipso_v4_map_lvl_ntoh(const struct cipso_v4_doi *doi_def,
750 u32 net_lvl,
751 u32 *host_lvl)
752{
753 struct cipso_v4_std_map_tbl *map_tbl;
754
755 switch (doi_def->type) {
756 case CIPSO_V4_MAP_PASS:
757 *host_lvl = net_lvl;
758 return 0;
759 case CIPSO_V4_MAP_STD:
760 map_tbl = doi_def->map.std;
761 if (net_lvl < map_tbl->lvl.cipso_size &&
762 map_tbl->lvl.cipso[net_lvl] < CIPSO_V4_INV_LVL) {
763 *host_lvl = doi_def->map.std->lvl.cipso[net_lvl];
764 return 0;
765 }
766 break;
767 }
768
769 return -EINVAL;
770}
771
772/**
773 * cipso_v4_map_cat_rbm_valid - Checks to see if the category bitmap is valid
774 * @doi_def: the DOI definition
775 * @bitmap: category bitmap
776 * @bitmap_len: bitmap length in bytes
777 *
778 * Description:
779 * Checks the given category bitmap against the given DOI definition and
780 * returns a negative value if any of the categories in the bitmap do not have
781 * a valid mapping and a zero value if all of the categories are valid.
782 *
783 */
784static int cipso_v4_map_cat_rbm_valid(const struct cipso_v4_doi *doi_def,
785 const unsigned char *bitmap,
786 u32 bitmap_len)
787{
788 int cat = -1;
789 u32 bitmap_len_bits = bitmap_len * 8;
044a68ed
PM
790 u32 cipso_cat_size;
791 u32 *cipso_array;
446fda4f
PM
792
793 switch (doi_def->type) {
794 case CIPSO_V4_MAP_PASS:
795 return 0;
796 case CIPSO_V4_MAP_STD:
044a68ed
PM
797 cipso_cat_size = doi_def->map.std->cat.cipso_size;
798 cipso_array = doi_def->map.std->cat.cipso;
446fda4f
PM
799 for (;;) {
800 cat = cipso_v4_bitmap_walk(bitmap,
801 bitmap_len_bits,
802 cat + 1,
803 1);
804 if (cat < 0)
805 break;
806 if (cat >= cipso_cat_size ||
807 cipso_array[cat] >= CIPSO_V4_INV_CAT)
808 return -EFAULT;
809 }
810
811 if (cat == -1)
812 return 0;
813 break;
814 }
815
816 return -EFAULT;
817}
818
819/**
820 * cipso_v4_map_cat_rbm_hton - Perform a category mapping from host to network
821 * @doi_def: the DOI definition
822 * @host_cat: the category bitmap in host format
823 * @host_cat_len: the length of the host's category bitmap in bytes
824 * @net_cat: the zero'd out category bitmap in network/CIPSO format
825 * @net_cat_len: the length of the CIPSO bitmap in bytes
826 *
827 * Description:
828 * Perform a label mapping to translate a local MLS category bitmap to the
829 * correct CIPSO bitmap using the given DOI definition. Returns the minimum
830 * size in bytes of the network bitmap on success, negative values otherwise.
831 *
832 */
833static int cipso_v4_map_cat_rbm_hton(const struct cipso_v4_doi *doi_def,
834 const unsigned char *host_cat,
835 u32 host_cat_len,
836 unsigned char *net_cat,
837 u32 net_cat_len)
838{
839 int host_spot = -1;
840 u32 net_spot;
841 u32 net_spot_max = 0;
842 u32 host_clen_bits = host_cat_len * 8;
843 u32 net_clen_bits = net_cat_len * 8;
044a68ed
PM
844 u32 host_cat_size;
845 u32 *host_cat_array;
446fda4f
PM
846
847 switch (doi_def->type) {
848 case CIPSO_V4_MAP_PASS:
ea614d7f
PM
849 net_spot_max = host_cat_len;
850 while (net_spot_max > 0 && host_cat[net_spot_max - 1] == 0)
446fda4f
PM
851 net_spot_max--;
852 if (net_spot_max > net_cat_len)
853 return -EINVAL;
854 memcpy(net_cat, host_cat, net_spot_max);
855 return net_spot_max;
856 case CIPSO_V4_MAP_STD:
044a68ed
PM
857 host_cat_size = doi_def->map.std->cat.local_size;
858 host_cat_array = doi_def->map.std->cat.local;
446fda4f
PM
859 for (;;) {
860 host_spot = cipso_v4_bitmap_walk(host_cat,
861 host_clen_bits,
862 host_spot + 1,
863 1);
864 if (host_spot < 0)
865 break;
866 if (host_spot >= host_cat_size)
867 return -EPERM;
868
869 net_spot = host_cat_array[host_spot];
870 if (net_spot >= net_clen_bits)
871 return -ENOSPC;
872 cipso_v4_bitmap_setbit(net_cat, net_spot, 1);
873
874 if (net_spot > net_spot_max)
875 net_spot_max = net_spot;
876 }
877
878 if (host_spot == -2)
879 return -EFAULT;
880
881 if (++net_spot_max % 8)
882 return net_spot_max / 8 + 1;
883 return net_spot_max / 8;
884 }
885
886 return -EINVAL;
887}
888
889/**
890 * cipso_v4_map_cat_rbm_ntoh - Perform a category mapping from network to host
891 * @doi_def: the DOI definition
892 * @net_cat: the category bitmap in network/CIPSO format
893 * @net_cat_len: the length of the CIPSO bitmap in bytes
894 * @host_cat: the zero'd out category bitmap in host format
895 * @host_cat_len: the length of the host's category bitmap in bytes
896 *
897 * Description:
898 * Perform a label mapping to translate a CIPSO bitmap to the correct local
899 * MLS category bitmap using the given DOI definition. Returns the minimum
900 * size in bytes of the host bitmap on success, negative values otherwise.
901 *
902 */
903static int cipso_v4_map_cat_rbm_ntoh(const struct cipso_v4_doi *doi_def,
904 const unsigned char *net_cat,
905 u32 net_cat_len,
906 unsigned char *host_cat,
907 u32 host_cat_len)
908{
909 u32 host_spot;
910 u32 host_spot_max = 0;
911 int net_spot = -1;
912 u32 net_clen_bits = net_cat_len * 8;
913 u32 host_clen_bits = host_cat_len * 8;
044a68ed
PM
914 u32 net_cat_size;
915 u32 *net_cat_array;
446fda4f
PM
916
917 switch (doi_def->type) {
918 case CIPSO_V4_MAP_PASS:
919 if (net_cat_len > host_cat_len)
920 return -EINVAL;
921 memcpy(host_cat, net_cat, net_cat_len);
922 return net_cat_len;
923 case CIPSO_V4_MAP_STD:
044a68ed
PM
924 net_cat_size = doi_def->map.std->cat.cipso_size;
925 net_cat_array = doi_def->map.std->cat.cipso;
446fda4f
PM
926 for (;;) {
927 net_spot = cipso_v4_bitmap_walk(net_cat,
928 net_clen_bits,
929 net_spot + 1,
930 1);
931 if (net_spot < 0)
932 break;
933 if (net_spot >= net_cat_size ||
934 net_cat_array[net_spot] >= CIPSO_V4_INV_CAT)
935 return -EPERM;
936
937 host_spot = net_cat_array[net_spot];
938 if (host_spot >= host_clen_bits)
939 return -ENOSPC;
940 cipso_v4_bitmap_setbit(host_cat, host_spot, 1);
941
942 if (host_spot > host_spot_max)
943 host_spot_max = host_spot;
944 }
945
946 if (net_spot == -2)
947 return -EFAULT;
948
949 if (++host_spot_max % 8)
950 return host_spot_max / 8 + 1;
951 return host_spot_max / 8;
952 }
953
954 return -EINVAL;
955}
956
957/*
958 * Protocol Handling Functions
959 */
960
91b1ed0a 961#define CIPSO_V4_OPT_LEN_MAX 40
446fda4f
PM
962#define CIPSO_V4_HDR_LEN 6
963
964/**
965 * cipso_v4_gentag_hdr - Generate a CIPSO option header
966 * @doi_def: the DOI definition
91b1ed0a 967 * @len: the total tag length in bytes, not including this header
446fda4f
PM
968 * @buf: the CIPSO option buffer
969 *
970 * Description:
91b1ed0a 971 * Write a CIPSO header into the beginning of @buffer.
446fda4f
PM
972 *
973 */
91b1ed0a
PM
974static void cipso_v4_gentag_hdr(const struct cipso_v4_doi *doi_def,
975 unsigned char *buf,
976 u32 len)
446fda4f 977{
446fda4f
PM
978 buf[0] = IPOPT_CIPSO;
979 buf[1] = CIPSO_V4_HDR_LEN + len;
714e85be 980 *(__be32 *)&buf[2] = htonl(doi_def->doi);
446fda4f
PM
981}
982
446fda4f
PM
983/**
984 * cipso_v4_gentag_rbm - Generate a CIPSO restricted bitmap tag (type #1)
985 * @doi_def: the DOI definition
986 * @secattr: the security attributes
987 * @buffer: the option buffer
988 * @buffer_len: length of buffer in bytes
989 *
990 * Description:
991 * Generate a CIPSO option using the restricted bitmap tag, tag type #1. The
992 * actual buffer length may be larger than the indicated size due to
91b1ed0a
PM
993 * translation between host and network category bitmaps. Returns the size of
994 * the tag on success, negative values on failure.
446fda4f
PM
995 *
996 */
997static int cipso_v4_gentag_rbm(const struct cipso_v4_doi *doi_def,
998 const struct netlbl_lsm_secattr *secattr,
91b1ed0a
PM
999 unsigned char *buffer,
1000 u32 buffer_len)
446fda4f 1001{
701a90ba 1002 int ret_val;
91b1ed0a 1003 u32 tag_len;
446fda4f
PM
1004 u32 level;
1005
701a90ba
PM
1006 if ((secattr->flags & NETLBL_SECATTR_MLS_LVL) == 0)
1007 return -EPERM;
1008
91b1ed0a
PM
1009 ret_val = cipso_v4_map_lvl_hton(doi_def, secattr->mls_lvl, &level);
1010 if (ret_val != 0)
1011 return ret_val;
446fda4f 1012
91b1ed0a 1013 if (secattr->flags & NETLBL_SECATTR_MLS_CAT) {
446fda4f
PM
1014 ret_val = cipso_v4_map_cat_rbm_hton(doi_def,
1015 secattr->mls_cat,
1016 secattr->mls_cat_len,
91b1ed0a
PM
1017 &buffer[4],
1018 buffer_len - 4);
446fda4f 1019 if (ret_val < 0)
91b1ed0a 1020 return ret_val;
446fda4f
PM
1021
1022 /* This will send packets using the "optimized" format when
1023 * possibile as specified in section 3.4.2.6 of the
1024 * CIPSO draft. */
701a90ba 1025 if (cipso_v4_rbm_optfmt && ret_val > 0 && ret_val <= 10)
91b1ed0a 1026 tag_len = 14;
701a90ba 1027 else
91b1ed0a
PM
1028 tag_len = 4 + ret_val;
1029 } else
1030 tag_len = 4;
446fda4f 1031
91b1ed0a
PM
1032 buffer[0] = 0x01;
1033 buffer[1] = tag_len;
1034 buffer[3] = level;
446fda4f 1035
91b1ed0a 1036 return tag_len;
446fda4f
PM
1037}
1038
1039/**
1040 * cipso_v4_parsetag_rbm - Parse a CIPSO restricted bitmap tag
1041 * @doi_def: the DOI definition
1042 * @tag: the CIPSO tag
1043 * @secattr: the security attributes
1044 *
1045 * Description:
1046 * Parse a CIPSO restricted bitmap tag (tag type #1) and return the security
1047 * attributes in @secattr. Return zero on success, negatives values on
1048 * failure.
1049 *
1050 */
1051static int cipso_v4_parsetag_rbm(const struct cipso_v4_doi *doi_def,
1052 const unsigned char *tag,
1053 struct netlbl_lsm_secattr *secattr)
1054{
1055 int ret_val;
1056 u8 tag_len = tag[1];
1057 u32 level;
1058
1059 ret_val = cipso_v4_map_lvl_ntoh(doi_def, tag[3], &level);
1060 if (ret_val != 0)
1061 return ret_val;
1062 secattr->mls_lvl = level;
701a90ba 1063 secattr->flags |= NETLBL_SECATTR_MLS_LVL;
446fda4f
PM
1064
1065 if (tag_len > 4) {
1066 switch (doi_def->type) {
1067 case CIPSO_V4_MAP_PASS:
1068 secattr->mls_cat_len = tag_len - 4;
1069 break;
1070 case CIPSO_V4_MAP_STD:
1071 secattr->mls_cat_len =
1072 doi_def->map.std->cat.local_size;
1073 break;
1074 }
1075 secattr->mls_cat = kzalloc(secattr->mls_cat_len, GFP_ATOMIC);
1076 if (secattr->mls_cat == NULL)
1077 return -ENOMEM;
1078
1079 ret_val = cipso_v4_map_cat_rbm_ntoh(doi_def,
1080 &tag[4],
1081 tag_len - 4,
1082 secattr->mls_cat,
1083 secattr->mls_cat_len);
1084 if (ret_val < 0) {
1085 kfree(secattr->mls_cat);
1086 return ret_val;
701a90ba
PM
1087 } else if (ret_val > 0) {
1088 secattr->mls_cat_len = ret_val;
1089 secattr->flags |= NETLBL_SECATTR_MLS_CAT;
446fda4f 1090 }
446fda4f
PM
1091 }
1092
1093 return 0;
1094}
1095
1096/**
1097 * cipso_v4_validate - Validate a CIPSO option
1098 * @option: the start of the option, on error it is set to point to the error
1099 *
1100 * Description:
1101 * This routine is called to validate a CIPSO option, it checks all of the
1102 * fields to ensure that they are at least valid, see the draft snippet below
1103 * for details. If the option is valid then a zero value is returned and
1104 * the value of @option is unchanged. If the option is invalid then a
1105 * non-zero value is returned and @option is adjusted to point to the
1106 * offending portion of the option. From the IETF draft ...
1107 *
1108 * "If any field within the CIPSO options, such as the DOI identifier, is not
1109 * recognized the IP datagram is discarded and an ICMP 'parameter problem'
1110 * (type 12) is generated and returned. The ICMP code field is set to 'bad
1111 * parameter' (code 0) and the pointer is set to the start of the CIPSO field
1112 * that is unrecognized."
1113 *
1114 */
1115int cipso_v4_validate(unsigned char **option)
1116{
1117 unsigned char *opt = *option;
1118 unsigned char *tag;
1119 unsigned char opt_iter;
1120 unsigned char err_offset = 0;
1121 u8 opt_len;
1122 u8 tag_len;
1123 struct cipso_v4_doi *doi_def = NULL;
1124 u32 tag_iter;
1125
1126 /* caller already checks for length values that are too large */
1127 opt_len = opt[1];
1128 if (opt_len < 8) {
1129 err_offset = 1;
1130 goto validate_return;
1131 }
1132
1133 rcu_read_lock();
714e85be 1134 doi_def = cipso_v4_doi_getdef(ntohl(*((__be32 *)&opt[2])));
446fda4f
PM
1135 if (doi_def == NULL) {
1136 err_offset = 2;
1137 goto validate_return_locked;
1138 }
1139
1140 opt_iter = 6;
1141 tag = opt + opt_iter;
1142 while (opt_iter < opt_len) {
1143 for (tag_iter = 0; doi_def->tags[tag_iter] != tag[0];)
1144 if (doi_def->tags[tag_iter] == CIPSO_V4_TAG_INVALID ||
1145 ++tag_iter == CIPSO_V4_TAG_MAXCNT) {
1146 err_offset = opt_iter;
1147 goto validate_return_locked;
1148 }
1149
1150 tag_len = tag[1];
1151 if (tag_len > (opt_len - opt_iter)) {
1152 err_offset = opt_iter + 1;
1153 goto validate_return_locked;
1154 }
1155
1156 switch (tag[0]) {
1157 case CIPSO_V4_TAG_RBITMAP:
1158 if (tag_len < 4) {
1159 err_offset = opt_iter + 1;
1160 goto validate_return_locked;
1161 }
1162
1163 /* We are already going to do all the verification
1164 * necessary at the socket layer so from our point of
1165 * view it is safe to turn these checks off (and less
1166 * work), however, the CIPSO draft says we should do
1167 * all the CIPSO validations here but it doesn't
1168 * really specify _exactly_ what we need to validate
1169 * ... so, just make it a sysctl tunable. */
1170 if (cipso_v4_rbm_strictvalid) {
1171 if (cipso_v4_map_lvl_valid(doi_def,
1172 tag[3]) < 0) {
1173 err_offset = opt_iter + 3;
1174 goto validate_return_locked;
1175 }
1176 if (tag_len > 4 &&
1177 cipso_v4_map_cat_rbm_valid(doi_def,
1178 &tag[4],
1179 tag_len - 4) < 0) {
1180 err_offset = opt_iter + 4;
1181 goto validate_return_locked;
1182 }
1183 }
1184 break;
1185 default:
1186 err_offset = opt_iter;
1187 goto validate_return_locked;
1188 }
1189
1190 tag += tag_len;
1191 opt_iter += tag_len;
1192 }
1193
1194validate_return_locked:
1195 rcu_read_unlock();
1196validate_return:
1197 *option = opt + err_offset;
1198 return err_offset;
1199}
1200
1201/**
1202 * cipso_v4_error - Send the correct reponse for a bad packet
1203 * @skb: the packet
1204 * @error: the error code
1205 * @gateway: CIPSO gateway flag
1206 *
1207 * Description:
1208 * Based on the error code given in @error, send an ICMP error message back to
1209 * the originating host. From the IETF draft ...
1210 *
1211 * "If the contents of the CIPSO [option] are valid but the security label is
1212 * outside of the configured host or port label range, the datagram is
1213 * discarded and an ICMP 'destination unreachable' (type 3) is generated and
1214 * returned. The code field of the ICMP is set to 'communication with
1215 * destination network administratively prohibited' (code 9) or to
1216 * 'communication with destination host administratively prohibited'
1217 * (code 10). The value of the code is dependent on whether the originator
1218 * of the ICMP message is acting as a CIPSO host or a CIPSO gateway. The
1219 * recipient of the ICMP message MUST be able to handle either value. The
1220 * same procedure is performed if a CIPSO [option] can not be added to an
1221 * IP packet because it is too large to fit in the IP options area."
1222 *
1223 * "If the error is triggered by receipt of an ICMP message, the message is
1224 * discarded and no response is permitted (consistent with general ICMP
1225 * processing rules)."
1226 *
1227 */
1228void cipso_v4_error(struct sk_buff *skb, int error, u32 gateway)
1229{
1230 if (skb->nh.iph->protocol == IPPROTO_ICMP || error != -EACCES)
1231 return;
1232
1233 if (gateway)
1234 icmp_send(skb, ICMP_DEST_UNREACH, ICMP_NET_ANO, 0);
1235 else
1236 icmp_send(skb, ICMP_DEST_UNREACH, ICMP_HOST_ANO, 0);
1237}
1238
1239/**
1240 * cipso_v4_socket_setattr - Add a CIPSO option to a socket
1241 * @sock: the socket
1242 * @doi_def: the CIPSO DOI to use
1243 * @secattr: the specific security attributes of the socket
1244 *
1245 * Description:
1246 * Set the CIPSO option on the given socket using the DOI definition and
1247 * security attributes passed to the function. This function requires
1248 * exclusive access to @sock->sk, which means it either needs to be in the
1249 * process of being created or locked via lock_sock(sock->sk). Returns zero on
1250 * success and negative values on failure.
1251 *
1252 */
1253int cipso_v4_socket_setattr(const struct socket *sock,
1254 const struct cipso_v4_doi *doi_def,
1255 const struct netlbl_lsm_secattr *secattr)
1256{
1257 int ret_val = -EPERM;
1258 u32 iter;
91b1ed0a 1259 unsigned char *buf;
446fda4f
PM
1260 u32 buf_len = 0;
1261 u32 opt_len;
1262 struct ip_options *opt = NULL;
1263 struct sock *sk;
1264 struct inet_sock *sk_inet;
1265 struct inet_connection_sock *sk_conn;
1266
1267 /* In the case of sock_create_lite(), the sock->sk field is not
1268 * defined yet but it is not a problem as the only users of these
1269 * "lite" PF_INET sockets are functions which do an accept() call
1270 * afterwards so we will label the socket as part of the accept(). */
1271 sk = sock->sk;
1272 if (sk == NULL)
1273 return 0;
1274
91b1ed0a
PM
1275 /* We allocate the maximum CIPSO option size here so we are probably
1276 * being a little wasteful, but it makes our life _much_ easier later
1277 * on and after all we are only talking about 40 bytes. */
1278 buf_len = CIPSO_V4_OPT_LEN_MAX;
1279 buf = kmalloc(buf_len, GFP_ATOMIC);
1280 if (buf == NULL) {
1281 ret_val = -ENOMEM;
1282 goto socket_setattr_failure;
1283 }
1284
446fda4f
PM
1285 /* XXX - This code assumes only one tag per CIPSO option which isn't
1286 * really a good assumption to make but since we only support the MAC
1287 * tags right now it is a safe assumption. */
1288 iter = 0;
1289 do {
91b1ed0a 1290 memset(buf, 0, buf_len);
446fda4f
PM
1291 switch (doi_def->tags[iter]) {
1292 case CIPSO_V4_TAG_RBITMAP:
1293 ret_val = cipso_v4_gentag_rbm(doi_def,
91b1ed0a
PM
1294 secattr,
1295 &buf[CIPSO_V4_HDR_LEN],
1296 buf_len - CIPSO_V4_HDR_LEN);
446fda4f
PM
1297 break;
1298 default:
1299 ret_val = -EPERM;
1300 goto socket_setattr_failure;
1301 }
1302
1303 iter++;
91b1ed0a 1304 } while (ret_val < 0 &&
446fda4f
PM
1305 iter < CIPSO_V4_TAG_MAXCNT &&
1306 doi_def->tags[iter] != CIPSO_V4_TAG_INVALID);
91b1ed0a 1307 if (ret_val < 0)
446fda4f 1308 goto socket_setattr_failure;
91b1ed0a
PM
1309 cipso_v4_gentag_hdr(doi_def, buf, ret_val);
1310 buf_len = CIPSO_V4_HDR_LEN + ret_val;
446fda4f
PM
1311
1312 /* We can't use ip_options_get() directly because it makes a call to
1313 * ip_options_get_alloc() which allocates memory with GFP_KERNEL and
f8687afe
PM
1314 * we won't always have CAP_NET_RAW even though we _always_ want to
1315 * set the IPOPT_CIPSO option. */
446fda4f
PM
1316 opt_len = (buf_len + 3) & ~3;
1317 opt = kzalloc(sizeof(*opt) + opt_len, GFP_ATOMIC);
1318 if (opt == NULL) {
1319 ret_val = -ENOMEM;
1320 goto socket_setattr_failure;
1321 }
1322 memcpy(opt->__data, buf, buf_len);
1323 opt->optlen = opt_len;
1324 opt->is_data = 1;
f8687afe 1325 opt->cipso = sizeof(struct iphdr);
446fda4f
PM
1326 kfree(buf);
1327 buf = NULL;
446fda4f
PM
1328
1329 sk_inet = inet_sk(sk);
1330 if (sk_inet->is_icsk) {
1331 sk_conn = inet_csk(sk);
1332 if (sk_inet->opt)
1333 sk_conn->icsk_ext_hdr_len -= sk_inet->opt->optlen;
1334 sk_conn->icsk_ext_hdr_len += opt->optlen;
1335 sk_conn->icsk_sync_mss(sk, sk_conn->icsk_pmtu_cookie);
1336 }
1337 opt = xchg(&sk_inet->opt, opt);
1338 kfree(opt);
1339
1340 return 0;
1341
1342socket_setattr_failure:
1343 kfree(buf);
1344 kfree(opt);
1345 return ret_val;
1346}
1347
1348/**
14a72f53
PM
1349 * cipso_v4_sock_getattr - Get the security attributes from a sock
1350 * @sk: the sock
446fda4f
PM
1351 * @secattr: the security attributes
1352 *
1353 * Description:
14a72f53
PM
1354 * Query @sk to see if there is a CIPSO option attached to the sock and if
1355 * there is return the CIPSO security attributes in @secattr. This function
1356 * requires that @sk be locked, or privately held, but it does not do any
1357 * locking itself. Returns zero on success and negative values on failure.
446fda4f
PM
1358 *
1359 */
14a72f53 1360int cipso_v4_sock_getattr(struct sock *sk, struct netlbl_lsm_secattr *secattr)
446fda4f
PM
1361{
1362 int ret_val = -ENOMSG;
446fda4f
PM
1363 struct inet_sock *sk_inet;
1364 unsigned char *cipso_ptr;
1365 u32 doi;
1366 struct cipso_v4_doi *doi_def;
1367
446fda4f
PM
1368 sk_inet = inet_sk(sk);
1369 if (sk_inet->opt == NULL || sk_inet->opt->cipso == 0)
14a72f53 1370 return -ENOMSG;
446fda4f
PM
1371 cipso_ptr = sk_inet->opt->__data + sk_inet->opt->cipso -
1372 sizeof(struct iphdr);
1373 ret_val = cipso_v4_cache_check(cipso_ptr, cipso_ptr[1], secattr);
1374 if (ret_val == 0)
14a72f53 1375 return ret_val;
446fda4f 1376
714e85be 1377 doi = ntohl(*(__be32 *)&cipso_ptr[2]);
446fda4f
PM
1378 rcu_read_lock();
1379 doi_def = cipso_v4_doi_getdef(doi);
1380 if (doi_def == NULL) {
1381 rcu_read_unlock();
14a72f53 1382 return -ENOMSG;
446fda4f 1383 }
91b1ed0a
PM
1384
1385 /* XXX - This code assumes only one tag per CIPSO option which isn't
1386 * really a good assumption to make but since we only support the MAC
1387 * tags right now it is a safe assumption. */
446fda4f
PM
1388 switch (cipso_ptr[6]) {
1389 case CIPSO_V4_TAG_RBITMAP:
1390 ret_val = cipso_v4_parsetag_rbm(doi_def,
1391 &cipso_ptr[6],
1392 secattr);
1393 break;
1394 }
1395 rcu_read_unlock();
1396
14a72f53
PM
1397 return ret_val;
1398}
1399
1400/**
1401 * cipso_v4_socket_getattr - Get the security attributes from a socket
1402 * @sock: the socket
1403 * @secattr: the security attributes
1404 *
1405 * Description:
1406 * Query @sock to see if there is a CIPSO option attached to the socket and if
1407 * there is return the CIPSO security attributes in @secattr. Returns zero on
1408 * success and negative values on failure.
1409 *
1410 */
1411int cipso_v4_socket_getattr(const struct socket *sock,
1412 struct netlbl_lsm_secattr *secattr)
1413{
1414 int ret_val;
1415
1416 lock_sock(sock->sk);
1417 ret_val = cipso_v4_sock_getattr(sock->sk, secattr);
1418 release_sock(sock->sk);
1419
446fda4f
PM
1420 return ret_val;
1421}
1422
1423/**
1424 * cipso_v4_skbuff_getattr - Get the security attributes from the CIPSO option
1425 * @skb: the packet
1426 * @secattr: the security attributes
1427 *
1428 * Description:
1429 * Parse the given packet's CIPSO option and return the security attributes.
1430 * Returns zero on success and negative values on failure.
1431 *
1432 */
1433int cipso_v4_skbuff_getattr(const struct sk_buff *skb,
1434 struct netlbl_lsm_secattr *secattr)
1435{
1436 int ret_val = -ENOMSG;
1437 unsigned char *cipso_ptr;
1438 u32 doi;
1439 struct cipso_v4_doi *doi_def;
1440
446fda4f
PM
1441 cipso_ptr = CIPSO_V4_OPTPTR(skb);
1442 if (cipso_v4_cache_check(cipso_ptr, cipso_ptr[1], secattr) == 0)
1443 return 0;
1444
714e85be 1445 doi = ntohl(*(__be32 *)&cipso_ptr[2]);
446fda4f
PM
1446 rcu_read_lock();
1447 doi_def = cipso_v4_doi_getdef(doi);
1448 if (doi_def == NULL)
1449 goto skbuff_getattr_return;
91b1ed0a
PM
1450
1451 /* XXX - This code assumes only one tag per CIPSO option which isn't
1452 * really a good assumption to make but since we only support the MAC
1453 * tags right now it is a safe assumption. */
446fda4f
PM
1454 switch (cipso_ptr[6]) {
1455 case CIPSO_V4_TAG_RBITMAP:
1456 ret_val = cipso_v4_parsetag_rbm(doi_def,
1457 &cipso_ptr[6],
1458 secattr);
1459 break;
1460 }
1461
1462skbuff_getattr_return:
1463 rcu_read_unlock();
1464 return ret_val;
1465}
1466
1467/*
1468 * Setup Functions
1469 */
1470
1471/**
1472 * cipso_v4_init - Initialize the CIPSO module
1473 *
1474 * Description:
1475 * Initialize the CIPSO module and prepare it for use. Returns zero on success
1476 * and negative values on failure.
1477 *
1478 */
1479static int __init cipso_v4_init(void)
1480{
1481 int ret_val;
1482
1483 ret_val = cipso_v4_cache_init();
1484 if (ret_val != 0)
1485 panic("Failed to initialize the CIPSO/IPv4 cache (%d)\n",
1486 ret_val);
1487
1488 return 0;
1489}
1490
1491subsys_initcall(cipso_v4_init);