70858dccaca200ac10a2614a49ccd0d3e3842eb6
[deliverable/linux.git] / net / ipv4 / cipso_ipv4.c
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>
46 #include <asm/atomic.h>
47 #include <asm/bug.h>
48
49 struct 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. */
64 static DEFINE_SPINLOCK(cipso_v4_doi_list_lock);
65 static struct list_head cipso_v4_doi_list = LIST_HEAD_INIT(cipso_v4_doi_list);
66
67 /* Label mapping cache */
68 int cipso_v4_cache_enabled = 1;
69 int 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
73 struct cipso_v4_map_cache_bkt {
74 spinlock_t lock;
75 u32 size;
76 struct list_head list;
77 };
78 struct cipso_v4_map_cache_entry {
79 u32 hash;
80 unsigned char *key;
81 size_t key_len;
82
83 struct netlbl_lsm_cache *lsm_data;
84
85 u32 activity;
86 struct list_head list;
87 };
88 static struct cipso_v4_map_cache_bkt *cipso_v4_cache = NULL;
89
90 /* Restricted bitmap (tag #1) flags */
91 int cipso_v4_rbm_optfmt = 0;
92 int 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 */
110 static 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 */
152 static 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 */
178 static 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:
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.
194 *
195 */
196 static void cipso_v4_cache_entry_free(struct cipso_v4_map_cache_entry *entry)
197 {
198 if (entry->lsm_data)
199 netlbl_secattr_cache_free(entry->lsm_data);
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 */
213 static 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 */
231 static 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 */
258 void 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++) {
264 spin_lock_bh(&cipso_v4_cache[iter].lock);
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;
272 spin_unlock_bh(&cipso_v4_cache[iter].lock);
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 */
300 static 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);
314 spin_lock_bh(&cipso_v4_cache[bkt].lock);
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;
320 atomic_inc(&entry->lsm_data->refcount);
321 secattr->cache = entry->lsm_data;
322 secattr->flags |= NETLBL_SECATTR_CACHE;
323 if (prev_entry == NULL) {
324 spin_unlock_bh(&cipso_v4_cache[bkt].lock);
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
339 spin_unlock_bh(&cipso_v4_cache[bkt].lock);
340 return 0;
341 }
342 prev_entry = entry;
343 }
344 spin_unlock_bh(&cipso_v4_cache[bkt].lock);
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 */
362 int 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;
381 entry->key = kmemdup(cipso_ptr, cipso_ptr_len, GFP_ATOMIC);
382 if (entry->key == NULL) {
383 ret_val = -ENOMEM;
384 goto cache_add_failure;
385 }
386 entry->key_len = cipso_ptr_len;
387 entry->hash = cipso_v4_map_cache_hash(cipso_ptr, cipso_ptr_len);
388 atomic_inc(&secattr->cache->refcount);
389 entry->lsm_data = secattr->cache;
390
391 bkt = entry->hash & (CIPSO_V4_CACHE_BUCKETBITS - 1);
392 spin_lock_bh(&cipso_v4_cache[bkt].lock);
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 }
403 spin_unlock_bh(&cipso_v4_cache[bkt].lock);
404
405 return 0;
406
407 cache_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 */
426 static 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 */
448 int cipso_v4_doi_add(struct cipso_v4_doi *doi_def)
449 {
450 u32 iter;
451
452 if (doi_def == NULL || doi_def->doi == CIPSO_V4_DOI_UNKNOWN)
453 return -EINVAL;
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 case CIPSO_V4_TAG_ENUM:
463 if (doi_def->type != CIPSO_V4_MAP_PASS)
464 return -EINVAL;
465 break;
466 default:
467 return -EINVAL;
468 }
469 }
470
471 doi_def->valid = 1;
472 INIT_RCU_HEAD(&doi_def->rcu);
473 INIT_LIST_HEAD(&doi_def->dom_list);
474
475 rcu_read_lock();
476 if (cipso_v4_doi_search(doi_def->doi) != NULL)
477 goto doi_add_failure_rlock;
478 spin_lock(&cipso_v4_doi_list_lock);
479 if (cipso_v4_doi_search(doi_def->doi) != NULL)
480 goto doi_add_failure_slock;
481 list_add_tail_rcu(&doi_def->list, &cipso_v4_doi_list);
482 spin_unlock(&cipso_v4_doi_list_lock);
483 rcu_read_unlock();
484
485 return 0;
486
487 doi_add_failure_slock:
488 spin_unlock(&cipso_v4_doi_list_lock);
489 doi_add_failure_rlock:
490 rcu_read_unlock();
491 return -EEXIST;
492 }
493
494 /**
495 * cipso_v4_doi_remove - Remove an existing DOI from the CIPSO protocol engine
496 * @doi: the DOI value
497 * @audit_secid: the LSM secid to use in the audit message
498 * @callback: the DOI cleanup/free callback
499 *
500 * Description:
501 * Removes a DOI definition from the CIPSO engine, @callback is called to
502 * free any memory. The NetLabel routines will be called to release their own
503 * LSM domain mappings as well as our own domain list. Returns zero on
504 * success and negative values on failure.
505 *
506 */
507 int cipso_v4_doi_remove(u32 doi,
508 struct netlbl_audit *audit_info,
509 void (*callback) (struct rcu_head * head))
510 {
511 struct cipso_v4_doi *doi_def;
512 struct cipso_v4_domhsh_entry *dom_iter;
513
514 rcu_read_lock();
515 if (cipso_v4_doi_search(doi) != NULL) {
516 spin_lock(&cipso_v4_doi_list_lock);
517 doi_def = cipso_v4_doi_search(doi);
518 if (doi_def == NULL) {
519 spin_unlock(&cipso_v4_doi_list_lock);
520 rcu_read_unlock();
521 return -ENOENT;
522 }
523 doi_def->valid = 0;
524 list_del_rcu(&doi_def->list);
525 spin_unlock(&cipso_v4_doi_list_lock);
526 list_for_each_entry_rcu(dom_iter, &doi_def->dom_list, list)
527 if (dom_iter->valid)
528 netlbl_domhsh_remove(dom_iter->domain,
529 audit_info);
530 cipso_v4_cache_invalidate();
531 rcu_read_unlock();
532
533 call_rcu(&doi_def->rcu, callback);
534 return 0;
535 }
536 rcu_read_unlock();
537
538 return -ENOENT;
539 }
540
541 /**
542 * cipso_v4_doi_getdef - Returns a pointer to a valid DOI definition
543 * @doi: the DOI value
544 *
545 * Description:
546 * Searches for a valid DOI definition and if one is found it is returned to
547 * the caller. Otherwise NULL is returned. The caller must ensure that
548 * rcu_read_lock() is held while accessing the returned definition.
549 *
550 */
551 struct cipso_v4_doi *cipso_v4_doi_getdef(u32 doi)
552 {
553 return cipso_v4_doi_search(doi);
554 }
555
556 /**
557 * cipso_v4_doi_walk - Iterate through the DOI definitions
558 * @skip_cnt: skip past this number of DOI definitions, updated
559 * @callback: callback for each DOI definition
560 * @cb_arg: argument for the callback function
561 *
562 * Description:
563 * Iterate over the DOI definition list, skipping the first @skip_cnt entries.
564 * For each entry call @callback, if @callback returns a negative value stop
565 * 'walking' through the list and return. Updates the value in @skip_cnt upon
566 * return. Returns zero on success, negative values on failure.
567 *
568 */
569 int cipso_v4_doi_walk(u32 *skip_cnt,
570 int (*callback) (struct cipso_v4_doi *doi_def, void *arg),
571 void *cb_arg)
572 {
573 int ret_val = -ENOENT;
574 u32 doi_cnt = 0;
575 struct cipso_v4_doi *iter_doi;
576
577 rcu_read_lock();
578 list_for_each_entry_rcu(iter_doi, &cipso_v4_doi_list, list)
579 if (iter_doi->valid) {
580 if (doi_cnt++ < *skip_cnt)
581 continue;
582 ret_val = callback(iter_doi, cb_arg);
583 if (ret_val < 0) {
584 doi_cnt--;
585 goto doi_walk_return;
586 }
587 }
588
589 doi_walk_return:
590 rcu_read_unlock();
591 *skip_cnt = doi_cnt;
592 return ret_val;
593 }
594
595 /**
596 * cipso_v4_doi_domhsh_add - Adds a domain entry to a DOI definition
597 * @doi_def: the DOI definition
598 * @domain: the domain to add
599 *
600 * Description:
601 * Adds the @domain to the the DOI specified by @doi_def, this function
602 * should only be called by external functions (i.e. NetLabel). This function
603 * does allocate memory. Returns zero on success, negative values on failure.
604 *
605 */
606 int cipso_v4_doi_domhsh_add(struct cipso_v4_doi *doi_def, const char *domain)
607 {
608 struct cipso_v4_domhsh_entry *iter;
609 struct cipso_v4_domhsh_entry *new_dom;
610
611 new_dom = kzalloc(sizeof(*new_dom), GFP_KERNEL);
612 if (new_dom == NULL)
613 return -ENOMEM;
614 if (domain) {
615 new_dom->domain = kstrdup(domain, GFP_KERNEL);
616 if (new_dom->domain == NULL) {
617 kfree(new_dom);
618 return -ENOMEM;
619 }
620 }
621 new_dom->valid = 1;
622 INIT_RCU_HEAD(&new_dom->rcu);
623
624 rcu_read_lock();
625 spin_lock(&cipso_v4_doi_list_lock);
626 list_for_each_entry_rcu(iter, &doi_def->dom_list, list)
627 if (iter->valid &&
628 ((domain != NULL && iter->domain != NULL &&
629 strcmp(iter->domain, domain) == 0) ||
630 (domain == NULL && iter->domain == NULL))) {
631 spin_unlock(&cipso_v4_doi_list_lock);
632 rcu_read_unlock();
633 kfree(new_dom->domain);
634 kfree(new_dom);
635 return -EEXIST;
636 }
637 list_add_tail_rcu(&new_dom->list, &doi_def->dom_list);
638 spin_unlock(&cipso_v4_doi_list_lock);
639 rcu_read_unlock();
640
641 return 0;
642 }
643
644 /**
645 * cipso_v4_doi_domhsh_remove - Removes a domain entry from a DOI definition
646 * @doi_def: the DOI definition
647 * @domain: the domain to remove
648 *
649 * Description:
650 * Removes the @domain from the DOI specified by @doi_def, this function
651 * should only be called by external functions (i.e. NetLabel). Returns zero
652 * on success and negative values on error.
653 *
654 */
655 int cipso_v4_doi_domhsh_remove(struct cipso_v4_doi *doi_def,
656 const char *domain)
657 {
658 struct cipso_v4_domhsh_entry *iter;
659
660 rcu_read_lock();
661 spin_lock(&cipso_v4_doi_list_lock);
662 list_for_each_entry_rcu(iter, &doi_def->dom_list, list)
663 if (iter->valid &&
664 ((domain != NULL && iter->domain != NULL &&
665 strcmp(iter->domain, domain) == 0) ||
666 (domain == NULL && iter->domain == NULL))) {
667 iter->valid = 0;
668 list_del_rcu(&iter->list);
669 spin_unlock(&cipso_v4_doi_list_lock);
670 rcu_read_unlock();
671 call_rcu(&iter->rcu, cipso_v4_doi_domhsh_free);
672
673 return 0;
674 }
675 spin_unlock(&cipso_v4_doi_list_lock);
676 rcu_read_unlock();
677
678 return -ENOENT;
679 }
680
681 /*
682 * Label Mapping Functions
683 */
684
685 /**
686 * cipso_v4_map_lvl_valid - Checks to see if the given level is understood
687 * @doi_def: the DOI definition
688 * @level: the level to check
689 *
690 * Description:
691 * Checks the given level against the given DOI definition and returns a
692 * negative value if the level does not have a valid mapping and a zero value
693 * if the level is defined by the DOI.
694 *
695 */
696 static int cipso_v4_map_lvl_valid(const struct cipso_v4_doi *doi_def, u8 level)
697 {
698 switch (doi_def->type) {
699 case CIPSO_V4_MAP_PASS:
700 return 0;
701 case CIPSO_V4_MAP_STD:
702 if (doi_def->map.std->lvl.cipso[level] < CIPSO_V4_INV_LVL)
703 return 0;
704 break;
705 }
706
707 return -EFAULT;
708 }
709
710 /**
711 * cipso_v4_map_lvl_hton - Perform a level mapping from the host to the network
712 * @doi_def: the DOI definition
713 * @host_lvl: the host MLS level
714 * @net_lvl: the network/CIPSO MLS level
715 *
716 * Description:
717 * Perform a label mapping to translate a local MLS level to the correct
718 * CIPSO level using the given DOI definition. Returns zero on success,
719 * negative values otherwise.
720 *
721 */
722 static int cipso_v4_map_lvl_hton(const struct cipso_v4_doi *doi_def,
723 u32 host_lvl,
724 u32 *net_lvl)
725 {
726 switch (doi_def->type) {
727 case CIPSO_V4_MAP_PASS:
728 *net_lvl = host_lvl;
729 return 0;
730 case CIPSO_V4_MAP_STD:
731 if (host_lvl < doi_def->map.std->lvl.local_size) {
732 *net_lvl = doi_def->map.std->lvl.local[host_lvl];
733 return 0;
734 }
735 break;
736 }
737
738 return -EINVAL;
739 }
740
741 /**
742 * cipso_v4_map_lvl_ntoh - Perform a level mapping from the network to the host
743 * @doi_def: the DOI definition
744 * @net_lvl: the network/CIPSO MLS level
745 * @host_lvl: the host MLS level
746 *
747 * Description:
748 * Perform a label mapping to translate a CIPSO level to the correct local MLS
749 * level using the given DOI definition. Returns zero on success, negative
750 * values otherwise.
751 *
752 */
753 static int cipso_v4_map_lvl_ntoh(const struct cipso_v4_doi *doi_def,
754 u32 net_lvl,
755 u32 *host_lvl)
756 {
757 struct cipso_v4_std_map_tbl *map_tbl;
758
759 switch (doi_def->type) {
760 case CIPSO_V4_MAP_PASS:
761 *host_lvl = net_lvl;
762 return 0;
763 case CIPSO_V4_MAP_STD:
764 map_tbl = doi_def->map.std;
765 if (net_lvl < map_tbl->lvl.cipso_size &&
766 map_tbl->lvl.cipso[net_lvl] < CIPSO_V4_INV_LVL) {
767 *host_lvl = doi_def->map.std->lvl.cipso[net_lvl];
768 return 0;
769 }
770 break;
771 }
772
773 return -EINVAL;
774 }
775
776 /**
777 * cipso_v4_map_cat_rbm_valid - Checks to see if the category bitmap is valid
778 * @doi_def: the DOI definition
779 * @bitmap: category bitmap
780 * @bitmap_len: bitmap length in bytes
781 *
782 * Description:
783 * Checks the given category bitmap against the given DOI definition and
784 * returns a negative value if any of the categories in the bitmap do not have
785 * a valid mapping and a zero value if all of the categories are valid.
786 *
787 */
788 static int cipso_v4_map_cat_rbm_valid(const struct cipso_v4_doi *doi_def,
789 const unsigned char *bitmap,
790 u32 bitmap_len)
791 {
792 int cat = -1;
793 u32 bitmap_len_bits = bitmap_len * 8;
794 u32 cipso_cat_size;
795 u32 *cipso_array;
796
797 switch (doi_def->type) {
798 case CIPSO_V4_MAP_PASS:
799 return 0;
800 case CIPSO_V4_MAP_STD:
801 cipso_cat_size = doi_def->map.std->cat.cipso_size;
802 cipso_array = doi_def->map.std->cat.cipso;
803 for (;;) {
804 cat = cipso_v4_bitmap_walk(bitmap,
805 bitmap_len_bits,
806 cat + 1,
807 1);
808 if (cat < 0)
809 break;
810 if (cat >= cipso_cat_size ||
811 cipso_array[cat] >= CIPSO_V4_INV_CAT)
812 return -EFAULT;
813 }
814
815 if (cat == -1)
816 return 0;
817 break;
818 }
819
820 return -EFAULT;
821 }
822
823 /**
824 * cipso_v4_map_cat_rbm_hton - Perform a category mapping from host to network
825 * @doi_def: the DOI definition
826 * @secattr: the security attributes
827 * @net_cat: the zero'd out category bitmap in network/CIPSO format
828 * @net_cat_len: the length of the CIPSO bitmap in bytes
829 *
830 * Description:
831 * Perform a label mapping to translate a local MLS category bitmap to the
832 * correct CIPSO bitmap using the given DOI definition. Returns the minimum
833 * size in bytes of the network bitmap on success, negative values otherwise.
834 *
835 */
836 static int cipso_v4_map_cat_rbm_hton(const struct cipso_v4_doi *doi_def,
837 const struct netlbl_lsm_secattr *secattr,
838 unsigned char *net_cat,
839 u32 net_cat_len)
840 {
841 int host_spot = -1;
842 u32 net_spot = CIPSO_V4_INV_CAT;
843 u32 net_spot_max = 0;
844 u32 net_clen_bits = net_cat_len * 8;
845 u32 host_cat_size = 0;
846 u32 *host_cat_array = NULL;
847
848 if (doi_def->type == CIPSO_V4_MAP_STD) {
849 host_cat_size = doi_def->map.std->cat.local_size;
850 host_cat_array = doi_def->map.std->cat.local;
851 }
852
853 for (;;) {
854 host_spot = netlbl_secattr_catmap_walk(secattr->mls_cat,
855 host_spot + 1);
856 if (host_spot < 0)
857 break;
858
859 switch (doi_def->type) {
860 case CIPSO_V4_MAP_PASS:
861 net_spot = host_spot;
862 break;
863 case CIPSO_V4_MAP_STD:
864 if (host_spot >= host_cat_size)
865 return -EPERM;
866 net_spot = host_cat_array[host_spot];
867 if (net_spot >= CIPSO_V4_INV_CAT)
868 return -EPERM;
869 break;
870 }
871 if (net_spot >= net_clen_bits)
872 return -ENOSPC;
873 cipso_v4_bitmap_setbit(net_cat, net_spot, 1);
874
875 if (net_spot > net_spot_max)
876 net_spot_max = net_spot;
877 }
878
879 if (++net_spot_max % 8)
880 return net_spot_max / 8 + 1;
881 return net_spot_max / 8;
882 }
883
884 /**
885 * cipso_v4_map_cat_rbm_ntoh - Perform a category mapping from network to host
886 * @doi_def: the DOI definition
887 * @net_cat: the category bitmap in network/CIPSO format
888 * @net_cat_len: the length of the CIPSO bitmap in bytes
889 * @secattr: the security attributes
890 *
891 * Description:
892 * Perform a label mapping to translate a CIPSO bitmap to the correct local
893 * MLS category bitmap using the given DOI definition. Returns zero on
894 * success, negative values on failure.
895 *
896 */
897 static int cipso_v4_map_cat_rbm_ntoh(const struct cipso_v4_doi *doi_def,
898 const unsigned char *net_cat,
899 u32 net_cat_len,
900 struct netlbl_lsm_secattr *secattr)
901 {
902 int ret_val;
903 int net_spot = -1;
904 u32 host_spot = CIPSO_V4_INV_CAT;
905 u32 net_clen_bits = net_cat_len * 8;
906 u32 net_cat_size = 0;
907 u32 *net_cat_array = NULL;
908
909 if (doi_def->type == CIPSO_V4_MAP_STD) {
910 net_cat_size = doi_def->map.std->cat.cipso_size;
911 net_cat_array = doi_def->map.std->cat.cipso;
912 }
913
914 for (;;) {
915 net_spot = cipso_v4_bitmap_walk(net_cat,
916 net_clen_bits,
917 net_spot + 1,
918 1);
919 if (net_spot < 0) {
920 if (net_spot == -2)
921 return -EFAULT;
922 return 0;
923 }
924
925 switch (doi_def->type) {
926 case CIPSO_V4_MAP_PASS:
927 host_spot = net_spot;
928 break;
929 case CIPSO_V4_MAP_STD:
930 if (net_spot >= net_cat_size)
931 return -EPERM;
932 host_spot = net_cat_array[net_spot];
933 if (host_spot >= CIPSO_V4_INV_CAT)
934 return -EPERM;
935 break;
936 }
937 ret_val = netlbl_secattr_catmap_setbit(secattr->mls_cat,
938 host_spot,
939 GFP_ATOMIC);
940 if (ret_val != 0)
941 return ret_val;
942 }
943
944 return -EINVAL;
945 }
946
947 /**
948 * cipso_v4_map_cat_enum_valid - Checks to see if the categories are valid
949 * @doi_def: the DOI definition
950 * @enumcat: category list
951 * @enumcat_len: length of the category list in bytes
952 *
953 * Description:
954 * Checks the given categories against the given DOI definition and returns a
955 * negative value if any of the categories do not have a valid mapping and a
956 * zero value if all of the categories are valid.
957 *
958 */
959 static int cipso_v4_map_cat_enum_valid(const struct cipso_v4_doi *doi_def,
960 const unsigned char *enumcat,
961 u32 enumcat_len)
962 {
963 u16 cat;
964 int cat_prev = -1;
965 u32 iter;
966
967 if (doi_def->type != CIPSO_V4_MAP_PASS || enumcat_len & 0x01)
968 return -EFAULT;
969
970 for (iter = 0; iter < enumcat_len; iter += 2) {
971 cat = ntohs(*((__be16 *)&enumcat[iter]));
972 if (cat <= cat_prev)
973 return -EFAULT;
974 cat_prev = cat;
975 }
976
977 return 0;
978 }
979
980 /**
981 * cipso_v4_map_cat_enum_hton - Perform a category mapping from host to network
982 * @doi_def: the DOI definition
983 * @secattr: the security attributes
984 * @net_cat: the zero'd out category list in network/CIPSO format
985 * @net_cat_len: the length of the CIPSO category list in bytes
986 *
987 * Description:
988 * Perform a label mapping to translate a local MLS category bitmap to the
989 * correct CIPSO category list using the given DOI definition. Returns the
990 * size in bytes of the network category bitmap on success, negative values
991 * otherwise.
992 *
993 */
994 static int cipso_v4_map_cat_enum_hton(const struct cipso_v4_doi *doi_def,
995 const struct netlbl_lsm_secattr *secattr,
996 unsigned char *net_cat,
997 u32 net_cat_len)
998 {
999 int cat = -1;
1000 u32 cat_iter = 0;
1001
1002 for (;;) {
1003 cat = netlbl_secattr_catmap_walk(secattr->mls_cat, cat + 1);
1004 if (cat < 0)
1005 break;
1006 if ((cat_iter + 2) > net_cat_len)
1007 return -ENOSPC;
1008
1009 *((__be16 *)&net_cat[cat_iter]) = htons(cat);
1010 cat_iter += 2;
1011 }
1012
1013 return cat_iter;
1014 }
1015
1016 /**
1017 * cipso_v4_map_cat_enum_ntoh - Perform a category mapping from network to host
1018 * @doi_def: the DOI definition
1019 * @net_cat: the category list in network/CIPSO format
1020 * @net_cat_len: the length of the CIPSO bitmap in bytes
1021 * @secattr: the security attributes
1022 *
1023 * Description:
1024 * Perform a label mapping to translate a CIPSO category list to the correct
1025 * local MLS category bitmap using the given DOI definition. Returns zero on
1026 * success, negative values on failure.
1027 *
1028 */
1029 static int cipso_v4_map_cat_enum_ntoh(const struct cipso_v4_doi *doi_def,
1030 const unsigned char *net_cat,
1031 u32 net_cat_len,
1032 struct netlbl_lsm_secattr *secattr)
1033 {
1034 int ret_val;
1035 u32 iter;
1036
1037 for (iter = 0; iter < net_cat_len; iter += 2) {
1038 ret_val = netlbl_secattr_catmap_setbit(secattr->mls_cat,
1039 ntohs(*((__be16 *)&net_cat[iter])),
1040 GFP_ATOMIC);
1041 if (ret_val != 0)
1042 return ret_val;
1043 }
1044
1045 return 0;
1046 }
1047
1048 /*
1049 * Protocol Handling Functions
1050 */
1051
1052 #define CIPSO_V4_OPT_LEN_MAX 40
1053 #define CIPSO_V4_HDR_LEN 6
1054
1055 /**
1056 * cipso_v4_gentag_hdr - Generate a CIPSO option header
1057 * @doi_def: the DOI definition
1058 * @len: the total tag length in bytes, not including this header
1059 * @buf: the CIPSO option buffer
1060 *
1061 * Description:
1062 * Write a CIPSO header into the beginning of @buffer.
1063 *
1064 */
1065 static void cipso_v4_gentag_hdr(const struct cipso_v4_doi *doi_def,
1066 unsigned char *buf,
1067 u32 len)
1068 {
1069 buf[0] = IPOPT_CIPSO;
1070 buf[1] = CIPSO_V4_HDR_LEN + len;
1071 *(__be32 *)&buf[2] = htonl(doi_def->doi);
1072 }
1073
1074 /**
1075 * cipso_v4_gentag_rbm - Generate a CIPSO restricted bitmap tag (type #1)
1076 * @doi_def: the DOI definition
1077 * @secattr: the security attributes
1078 * @buffer: the option buffer
1079 * @buffer_len: length of buffer in bytes
1080 *
1081 * Description:
1082 * Generate a CIPSO option using the restricted bitmap tag, tag type #1. The
1083 * actual buffer length may be larger than the indicated size due to
1084 * translation between host and network category bitmaps. Returns the size of
1085 * the tag on success, negative values on failure.
1086 *
1087 */
1088 static int cipso_v4_gentag_rbm(const struct cipso_v4_doi *doi_def,
1089 const struct netlbl_lsm_secattr *secattr,
1090 unsigned char *buffer,
1091 u32 buffer_len)
1092 {
1093 int ret_val;
1094 u32 tag_len;
1095 u32 level;
1096
1097 if ((secattr->flags & NETLBL_SECATTR_MLS_LVL) == 0)
1098 return -EPERM;
1099
1100 ret_val = cipso_v4_map_lvl_hton(doi_def, secattr->mls_lvl, &level);
1101 if (ret_val != 0)
1102 return ret_val;
1103
1104 if (secattr->flags & NETLBL_SECATTR_MLS_CAT) {
1105 ret_val = cipso_v4_map_cat_rbm_hton(doi_def,
1106 secattr,
1107 &buffer[4],
1108 buffer_len - 4);
1109 if (ret_val < 0)
1110 return ret_val;
1111
1112 /* This will send packets using the "optimized" format when
1113 * possibile as specified in section 3.4.2.6 of the
1114 * CIPSO draft. */
1115 if (cipso_v4_rbm_optfmt && ret_val > 0 && ret_val <= 10)
1116 tag_len = 14;
1117 else
1118 tag_len = 4 + ret_val;
1119 } else
1120 tag_len = 4;
1121
1122 buffer[0] = 0x01;
1123 buffer[1] = tag_len;
1124 buffer[3] = level;
1125
1126 return tag_len;
1127 }
1128
1129 /**
1130 * cipso_v4_parsetag_rbm - Parse a CIPSO restricted bitmap tag
1131 * @doi_def: the DOI definition
1132 * @tag: the CIPSO tag
1133 * @secattr: the security attributes
1134 *
1135 * Description:
1136 * Parse a CIPSO restricted bitmap tag (tag type #1) and return the security
1137 * attributes in @secattr. Return zero on success, negatives values on
1138 * failure.
1139 *
1140 */
1141 static int cipso_v4_parsetag_rbm(const struct cipso_v4_doi *doi_def,
1142 const unsigned char *tag,
1143 struct netlbl_lsm_secattr *secattr)
1144 {
1145 int ret_val;
1146 u8 tag_len = tag[1];
1147 u32 level;
1148
1149 ret_val = cipso_v4_map_lvl_ntoh(doi_def, tag[3], &level);
1150 if (ret_val != 0)
1151 return ret_val;
1152 secattr->mls_lvl = level;
1153 secattr->flags |= NETLBL_SECATTR_MLS_LVL;
1154
1155 if (tag_len > 4) {
1156 secattr->mls_cat = netlbl_secattr_catmap_alloc(GFP_ATOMIC);
1157 if (secattr->mls_cat == NULL)
1158 return -ENOMEM;
1159
1160 ret_val = cipso_v4_map_cat_rbm_ntoh(doi_def,
1161 &tag[4],
1162 tag_len - 4,
1163 secattr);
1164 if (ret_val != 0) {
1165 netlbl_secattr_catmap_free(secattr->mls_cat);
1166 return ret_val;
1167 }
1168
1169 secattr->flags |= NETLBL_SECATTR_MLS_CAT;
1170 }
1171
1172 return 0;
1173 }
1174
1175 /**
1176 * cipso_v4_gentag_enum - Generate a CIPSO enumerated tag (type #2)
1177 * @doi_def: the DOI definition
1178 * @secattr: the security attributes
1179 * @buffer: the option buffer
1180 * @buffer_len: length of buffer in bytes
1181 *
1182 * Description:
1183 * Generate a CIPSO option using the enumerated tag, tag type #2. Returns the
1184 * size of the tag on success, negative values on failure.
1185 *
1186 */
1187 static int cipso_v4_gentag_enum(const struct cipso_v4_doi *doi_def,
1188 const struct netlbl_lsm_secattr *secattr,
1189 unsigned char *buffer,
1190 u32 buffer_len)
1191 {
1192 int ret_val;
1193 u32 tag_len;
1194 u32 level;
1195
1196 if (!(secattr->flags & NETLBL_SECATTR_MLS_LVL))
1197 return -EPERM;
1198
1199 ret_val = cipso_v4_map_lvl_hton(doi_def, secattr->mls_lvl, &level);
1200 if (ret_val != 0)
1201 return ret_val;
1202
1203 if (secattr->flags & NETLBL_SECATTR_MLS_CAT) {
1204 ret_val = cipso_v4_map_cat_enum_hton(doi_def,
1205 secattr,
1206 &buffer[4],
1207 buffer_len - 4);
1208 if (ret_val < 0)
1209 return ret_val;
1210
1211 tag_len = 4 + ret_val;
1212 } else
1213 tag_len = 4;
1214
1215 buffer[0] = 0x02;
1216 buffer[1] = tag_len;
1217 buffer[3] = level;
1218
1219 return tag_len;
1220 }
1221
1222 /**
1223 * cipso_v4_parsetag_enum - Parse a CIPSO enumerated tag
1224 * @doi_def: the DOI definition
1225 * @tag: the CIPSO tag
1226 * @secattr: the security attributes
1227 *
1228 * Description:
1229 * Parse a CIPSO enumerated tag (tag type #2) and return the security
1230 * attributes in @secattr. Return zero on success, negatives values on
1231 * failure.
1232 *
1233 */
1234 static int cipso_v4_parsetag_enum(const struct cipso_v4_doi *doi_def,
1235 const unsigned char *tag,
1236 struct netlbl_lsm_secattr *secattr)
1237 {
1238 int ret_val;
1239 u8 tag_len = tag[1];
1240 u32 level;
1241
1242 ret_val = cipso_v4_map_lvl_ntoh(doi_def, tag[3], &level);
1243 if (ret_val != 0)
1244 return ret_val;
1245 secattr->mls_lvl = level;
1246 secattr->flags |= NETLBL_SECATTR_MLS_LVL;
1247
1248 if (tag_len > 4) {
1249 secattr->mls_cat = netlbl_secattr_catmap_alloc(GFP_ATOMIC);
1250 if (secattr->mls_cat == NULL)
1251 return -ENOMEM;
1252
1253 ret_val = cipso_v4_map_cat_enum_ntoh(doi_def,
1254 &tag[4],
1255 tag_len - 4,
1256 secattr);
1257 if (ret_val != 0) {
1258 netlbl_secattr_catmap_free(secattr->mls_cat);
1259 return ret_val;
1260 }
1261
1262 secattr->flags |= NETLBL_SECATTR_MLS_CAT;
1263 }
1264
1265 return 0;
1266 }
1267
1268 /**
1269 * cipso_v4_validate - Validate a CIPSO option
1270 * @option: the start of the option, on error it is set to point to the error
1271 *
1272 * Description:
1273 * This routine is called to validate a CIPSO option, it checks all of the
1274 * fields to ensure that they are at least valid, see the draft snippet below
1275 * for details. If the option is valid then a zero value is returned and
1276 * the value of @option is unchanged. If the option is invalid then a
1277 * non-zero value is returned and @option is adjusted to point to the
1278 * offending portion of the option. From the IETF draft ...
1279 *
1280 * "If any field within the CIPSO options, such as the DOI identifier, is not
1281 * recognized the IP datagram is discarded and an ICMP 'parameter problem'
1282 * (type 12) is generated and returned. The ICMP code field is set to 'bad
1283 * parameter' (code 0) and the pointer is set to the start of the CIPSO field
1284 * that is unrecognized."
1285 *
1286 */
1287 int cipso_v4_validate(unsigned char **option)
1288 {
1289 unsigned char *opt = *option;
1290 unsigned char *tag;
1291 unsigned char opt_iter;
1292 unsigned char err_offset = 0;
1293 u8 opt_len;
1294 u8 tag_len;
1295 struct cipso_v4_doi *doi_def = NULL;
1296 u32 tag_iter;
1297
1298 /* caller already checks for length values that are too large */
1299 opt_len = opt[1];
1300 if (opt_len < 8) {
1301 err_offset = 1;
1302 goto validate_return;
1303 }
1304
1305 rcu_read_lock();
1306 doi_def = cipso_v4_doi_search(ntohl(*((__be32 *)&opt[2])));
1307 if (doi_def == NULL) {
1308 err_offset = 2;
1309 goto validate_return_locked;
1310 }
1311
1312 opt_iter = 6;
1313 tag = opt + opt_iter;
1314 while (opt_iter < opt_len) {
1315 for (tag_iter = 0; doi_def->tags[tag_iter] != tag[0];)
1316 if (doi_def->tags[tag_iter] == CIPSO_V4_TAG_INVALID ||
1317 ++tag_iter == CIPSO_V4_TAG_MAXCNT) {
1318 err_offset = opt_iter;
1319 goto validate_return_locked;
1320 }
1321
1322 tag_len = tag[1];
1323 if (tag_len > (opt_len - opt_iter)) {
1324 err_offset = opt_iter + 1;
1325 goto validate_return_locked;
1326 }
1327
1328 switch (tag[0]) {
1329 case CIPSO_V4_TAG_RBITMAP:
1330 if (tag_len < 4) {
1331 err_offset = opt_iter + 1;
1332 goto validate_return_locked;
1333 }
1334
1335 /* We are already going to do all the verification
1336 * necessary at the socket layer so from our point of
1337 * view it is safe to turn these checks off (and less
1338 * work), however, the CIPSO draft says we should do
1339 * all the CIPSO validations here but it doesn't
1340 * really specify _exactly_ what we need to validate
1341 * ... so, just make it a sysctl tunable. */
1342 if (cipso_v4_rbm_strictvalid) {
1343 if (cipso_v4_map_lvl_valid(doi_def,
1344 tag[3]) < 0) {
1345 err_offset = opt_iter + 3;
1346 goto validate_return_locked;
1347 }
1348 if (tag_len > 4 &&
1349 cipso_v4_map_cat_rbm_valid(doi_def,
1350 &tag[4],
1351 tag_len - 4) < 0) {
1352 err_offset = opt_iter + 4;
1353 goto validate_return_locked;
1354 }
1355 }
1356 break;
1357 case CIPSO_V4_TAG_ENUM:
1358 if (tag_len < 4) {
1359 err_offset = opt_iter + 1;
1360 goto validate_return_locked;
1361 }
1362
1363 if (cipso_v4_map_lvl_valid(doi_def,
1364 tag[3]) < 0) {
1365 err_offset = opt_iter + 3;
1366 goto validate_return_locked;
1367 }
1368 if (tag_len > 4 &&
1369 cipso_v4_map_cat_enum_valid(doi_def,
1370 &tag[4],
1371 tag_len - 4) < 0) {
1372 err_offset = opt_iter + 4;
1373 goto validate_return_locked;
1374 }
1375 break;
1376 default:
1377 err_offset = opt_iter;
1378 goto validate_return_locked;
1379 }
1380
1381 tag += tag_len;
1382 opt_iter += tag_len;
1383 }
1384
1385 validate_return_locked:
1386 rcu_read_unlock();
1387 validate_return:
1388 *option = opt + err_offset;
1389 return err_offset;
1390 }
1391
1392 /**
1393 * cipso_v4_error - Send the correct reponse for a bad packet
1394 * @skb: the packet
1395 * @error: the error code
1396 * @gateway: CIPSO gateway flag
1397 *
1398 * Description:
1399 * Based on the error code given in @error, send an ICMP error message back to
1400 * the originating host. From the IETF draft ...
1401 *
1402 * "If the contents of the CIPSO [option] are valid but the security label is
1403 * outside of the configured host or port label range, the datagram is
1404 * discarded and an ICMP 'destination unreachable' (type 3) is generated and
1405 * returned. The code field of the ICMP is set to 'communication with
1406 * destination network administratively prohibited' (code 9) or to
1407 * 'communication with destination host administratively prohibited'
1408 * (code 10). The value of the code is dependent on whether the originator
1409 * of the ICMP message is acting as a CIPSO host or a CIPSO gateway. The
1410 * recipient of the ICMP message MUST be able to handle either value. The
1411 * same procedure is performed if a CIPSO [option] can not be added to an
1412 * IP packet because it is too large to fit in the IP options area."
1413 *
1414 * "If the error is triggered by receipt of an ICMP message, the message is
1415 * discarded and no response is permitted (consistent with general ICMP
1416 * processing rules)."
1417 *
1418 */
1419 void cipso_v4_error(struct sk_buff *skb, int error, u32 gateway)
1420 {
1421 if (skb->nh.iph->protocol == IPPROTO_ICMP || error != -EACCES)
1422 return;
1423
1424 if (gateway)
1425 icmp_send(skb, ICMP_DEST_UNREACH, ICMP_NET_ANO, 0);
1426 else
1427 icmp_send(skb, ICMP_DEST_UNREACH, ICMP_HOST_ANO, 0);
1428 }
1429
1430 /**
1431 * cipso_v4_socket_setattr - Add a CIPSO option to a socket
1432 * @sock: the socket
1433 * @doi_def: the CIPSO DOI to use
1434 * @secattr: the specific security attributes of the socket
1435 *
1436 * Description:
1437 * Set the CIPSO option on the given socket using the DOI definition and
1438 * security attributes passed to the function. This function requires
1439 * exclusive access to @sock->sk, which means it either needs to be in the
1440 * process of being created or locked via lock_sock(sock->sk). Returns zero on
1441 * success and negative values on failure.
1442 *
1443 */
1444 int cipso_v4_socket_setattr(const struct socket *sock,
1445 const struct cipso_v4_doi *doi_def,
1446 const struct netlbl_lsm_secattr *secattr)
1447 {
1448 int ret_val = -EPERM;
1449 u32 iter;
1450 unsigned char *buf;
1451 u32 buf_len = 0;
1452 u32 opt_len;
1453 struct ip_options *opt = NULL;
1454 struct sock *sk;
1455 struct inet_sock *sk_inet;
1456 struct inet_connection_sock *sk_conn;
1457
1458 /* In the case of sock_create_lite(), the sock->sk field is not
1459 * defined yet but it is not a problem as the only users of these
1460 * "lite" PF_INET sockets are functions which do an accept() call
1461 * afterwards so we will label the socket as part of the accept(). */
1462 sk = sock->sk;
1463 if (sk == NULL)
1464 return 0;
1465
1466 /* We allocate the maximum CIPSO option size here so we are probably
1467 * being a little wasteful, but it makes our life _much_ easier later
1468 * on and after all we are only talking about 40 bytes. */
1469 buf_len = CIPSO_V4_OPT_LEN_MAX;
1470 buf = kmalloc(buf_len, GFP_ATOMIC);
1471 if (buf == NULL) {
1472 ret_val = -ENOMEM;
1473 goto socket_setattr_failure;
1474 }
1475
1476 /* XXX - This code assumes only one tag per CIPSO option which isn't
1477 * really a good assumption to make but since we only support the MAC
1478 * tags right now it is a safe assumption. */
1479 iter = 0;
1480 do {
1481 memset(buf, 0, buf_len);
1482 switch (doi_def->tags[iter]) {
1483 case CIPSO_V4_TAG_RBITMAP:
1484 ret_val = cipso_v4_gentag_rbm(doi_def,
1485 secattr,
1486 &buf[CIPSO_V4_HDR_LEN],
1487 buf_len - CIPSO_V4_HDR_LEN);
1488 break;
1489 case CIPSO_V4_TAG_ENUM:
1490 ret_val = cipso_v4_gentag_enum(doi_def,
1491 secattr,
1492 &buf[CIPSO_V4_HDR_LEN],
1493 buf_len - CIPSO_V4_HDR_LEN);
1494 break;
1495 default:
1496 ret_val = -EPERM;
1497 goto socket_setattr_failure;
1498 }
1499
1500 iter++;
1501 } while (ret_val < 0 &&
1502 iter < CIPSO_V4_TAG_MAXCNT &&
1503 doi_def->tags[iter] != CIPSO_V4_TAG_INVALID);
1504 if (ret_val < 0)
1505 goto socket_setattr_failure;
1506 cipso_v4_gentag_hdr(doi_def, buf, ret_val);
1507 buf_len = CIPSO_V4_HDR_LEN + ret_val;
1508
1509 /* We can't use ip_options_get() directly because it makes a call to
1510 * ip_options_get_alloc() which allocates memory with GFP_KERNEL and
1511 * we won't always have CAP_NET_RAW even though we _always_ want to
1512 * set the IPOPT_CIPSO option. */
1513 opt_len = (buf_len + 3) & ~3;
1514 opt = kzalloc(sizeof(*opt) + opt_len, GFP_ATOMIC);
1515 if (opt == NULL) {
1516 ret_val = -ENOMEM;
1517 goto socket_setattr_failure;
1518 }
1519 memcpy(opt->__data, buf, buf_len);
1520 opt->optlen = opt_len;
1521 opt->is_data = 1;
1522 opt->cipso = sizeof(struct iphdr);
1523 kfree(buf);
1524 buf = NULL;
1525
1526 sk_inet = inet_sk(sk);
1527 if (sk_inet->is_icsk) {
1528 sk_conn = inet_csk(sk);
1529 if (sk_inet->opt)
1530 sk_conn->icsk_ext_hdr_len -= sk_inet->opt->optlen;
1531 sk_conn->icsk_ext_hdr_len += opt->optlen;
1532 sk_conn->icsk_sync_mss(sk, sk_conn->icsk_pmtu_cookie);
1533 }
1534 opt = xchg(&sk_inet->opt, opt);
1535 kfree(opt);
1536
1537 return 0;
1538
1539 socket_setattr_failure:
1540 kfree(buf);
1541 kfree(opt);
1542 return ret_val;
1543 }
1544
1545 /**
1546 * cipso_v4_sock_getattr - Get the security attributes from a sock
1547 * @sk: the sock
1548 * @secattr: the security attributes
1549 *
1550 * Description:
1551 * Query @sk to see if there is a CIPSO option attached to the sock and if
1552 * there is return the CIPSO security attributes in @secattr. This function
1553 * requires that @sk be locked, or privately held, but it does not do any
1554 * locking itself. Returns zero on success and negative values on failure.
1555 *
1556 */
1557 int cipso_v4_sock_getattr(struct sock *sk, struct netlbl_lsm_secattr *secattr)
1558 {
1559 int ret_val = -ENOMSG;
1560 struct inet_sock *sk_inet;
1561 unsigned char *cipso_ptr;
1562 u32 doi;
1563 struct cipso_v4_doi *doi_def;
1564
1565 sk_inet = inet_sk(sk);
1566 if (sk_inet->opt == NULL || sk_inet->opt->cipso == 0)
1567 return -ENOMSG;
1568 cipso_ptr = sk_inet->opt->__data + sk_inet->opt->cipso -
1569 sizeof(struct iphdr);
1570 ret_val = cipso_v4_cache_check(cipso_ptr, cipso_ptr[1], secattr);
1571 if (ret_val == 0)
1572 return ret_val;
1573
1574 doi = ntohl(*(__be32 *)&cipso_ptr[2]);
1575 rcu_read_lock();
1576 doi_def = cipso_v4_doi_search(doi);
1577 if (doi_def == NULL) {
1578 rcu_read_unlock();
1579 return -ENOMSG;
1580 }
1581
1582 /* XXX - This code assumes only one tag per CIPSO option which isn't
1583 * really a good assumption to make but since we only support the MAC
1584 * tags right now it is a safe assumption. */
1585 switch (cipso_ptr[6]) {
1586 case CIPSO_V4_TAG_RBITMAP:
1587 ret_val = cipso_v4_parsetag_rbm(doi_def,
1588 &cipso_ptr[6],
1589 secattr);
1590 break;
1591 case CIPSO_V4_TAG_ENUM:
1592 ret_val = cipso_v4_parsetag_enum(doi_def,
1593 &cipso_ptr[6],
1594 secattr);
1595 break;
1596 }
1597 rcu_read_unlock();
1598
1599 return ret_val;
1600 }
1601
1602 /**
1603 * cipso_v4_socket_getattr - Get the security attributes from a socket
1604 * @sock: the socket
1605 * @secattr: the security attributes
1606 *
1607 * Description:
1608 * Query @sock to see if there is a CIPSO option attached to the socket and if
1609 * there is return the CIPSO security attributes in @secattr. Returns zero on
1610 * success and negative values on failure.
1611 *
1612 */
1613 int cipso_v4_socket_getattr(const struct socket *sock,
1614 struct netlbl_lsm_secattr *secattr)
1615 {
1616 int ret_val;
1617
1618 lock_sock(sock->sk);
1619 ret_val = cipso_v4_sock_getattr(sock->sk, secattr);
1620 release_sock(sock->sk);
1621
1622 return ret_val;
1623 }
1624
1625 /**
1626 * cipso_v4_skbuff_getattr - Get the security attributes from the CIPSO option
1627 * @skb: the packet
1628 * @secattr: the security attributes
1629 *
1630 * Description:
1631 * Parse the given packet's CIPSO option and return the security attributes.
1632 * Returns zero on success and negative values on failure.
1633 *
1634 */
1635 int cipso_v4_skbuff_getattr(const struct sk_buff *skb,
1636 struct netlbl_lsm_secattr *secattr)
1637 {
1638 int ret_val = -ENOMSG;
1639 unsigned char *cipso_ptr;
1640 u32 doi;
1641 struct cipso_v4_doi *doi_def;
1642
1643 cipso_ptr = CIPSO_V4_OPTPTR(skb);
1644 if (cipso_v4_cache_check(cipso_ptr, cipso_ptr[1], secattr) == 0)
1645 return 0;
1646
1647 doi = ntohl(*(__be32 *)&cipso_ptr[2]);
1648 rcu_read_lock();
1649 doi_def = cipso_v4_doi_search(doi);
1650 if (doi_def == NULL)
1651 goto skbuff_getattr_return;
1652
1653 /* XXX - This code assumes only one tag per CIPSO option which isn't
1654 * really a good assumption to make but since we only support the MAC
1655 * tags right now it is a safe assumption. */
1656 switch (cipso_ptr[6]) {
1657 case CIPSO_V4_TAG_RBITMAP:
1658 ret_val = cipso_v4_parsetag_rbm(doi_def,
1659 &cipso_ptr[6],
1660 secattr);
1661 break;
1662 case CIPSO_V4_TAG_ENUM:
1663 ret_val = cipso_v4_parsetag_enum(doi_def,
1664 &cipso_ptr[6],
1665 secattr);
1666 break;
1667 }
1668
1669 skbuff_getattr_return:
1670 rcu_read_unlock();
1671 return ret_val;
1672 }
1673
1674 /*
1675 * Setup Functions
1676 */
1677
1678 /**
1679 * cipso_v4_init - Initialize the CIPSO module
1680 *
1681 * Description:
1682 * Initialize the CIPSO module and prepare it for use. Returns zero on success
1683 * and negative values on failure.
1684 *
1685 */
1686 static int __init cipso_v4_init(void)
1687 {
1688 int ret_val;
1689
1690 ret_val = cipso_v4_cache_init();
1691 if (ret_val != 0)
1692 panic("Failed to initialize the CIPSO/IPv4 cache (%d)\n",
1693 ret_val);
1694
1695 return 0;
1696 }
1697
1698 subsys_initcall(cipso_v4_init);
This page took 0.080199 seconds and 4 git commands to generate.