Paul Moore | 446fda4 | 2006-08-03 16:48:06 -0700 | [diff] [blame] | 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 | /* |
Paul Moore | 948bf85 | 2008-10-10 10:16:32 -0400 | [diff] [blame] | 16 | * (c) Copyright Hewlett-Packard Development Company, L.P., 2006, 2008 |
Paul Moore | 446fda4 | 2006-08-03 16:48:06 -0700 | [diff] [blame] | 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> |
paul.moore@hp.com | ffb733c | 2006-10-04 11:46:31 -0400 | [diff] [blame] | 46 | #include <asm/atomic.h> |
Paul Moore | 446fda4 | 2006-08-03 16:48:06 -0700 | [diff] [blame] | 47 | #include <asm/bug.h> |
Paul Moore | 50e5d35 | 2007-06-07 18:38:14 -0700 | [diff] [blame] | 48 | #include <asm/unaligned.h> |
Paul Moore | 446fda4 | 2006-08-03 16:48:06 -0700 | [diff] [blame] | 49 | |
Paul Moore | 446fda4 | 2006-08-03 16:48:06 -0700 | [diff] [blame] | 50 | /* List of available DOI definitions */ |
Paul Moore | 446fda4 | 2006-08-03 16:48:06 -0700 | [diff] [blame] | 51 | /* XXX - This currently assumes a minimal number of different DOIs in use, |
| 52 | * if in practice there are a lot of different DOIs this list should |
| 53 | * probably be turned into a hash table or something similar so we |
| 54 | * can do quick lookups. */ |
Adrian Bunk | 8ce11e6 | 2006-08-07 21:50:48 -0700 | [diff] [blame] | 55 | static DEFINE_SPINLOCK(cipso_v4_doi_list_lock); |
Denis Cheng | 1596c97 | 2007-12-07 00:49:47 -0800 | [diff] [blame] | 56 | static LIST_HEAD(cipso_v4_doi_list); |
Paul Moore | 446fda4 | 2006-08-03 16:48:06 -0700 | [diff] [blame] | 57 | |
| 58 | /* Label mapping cache */ |
| 59 | int cipso_v4_cache_enabled = 1; |
| 60 | int cipso_v4_cache_bucketsize = 10; |
| 61 | #define CIPSO_V4_CACHE_BUCKETBITS 7 |
| 62 | #define CIPSO_V4_CACHE_BUCKETS (1 << CIPSO_V4_CACHE_BUCKETBITS) |
| 63 | #define CIPSO_V4_CACHE_REORDERLIMIT 10 |
| 64 | struct cipso_v4_map_cache_bkt { |
| 65 | spinlock_t lock; |
| 66 | u32 size; |
| 67 | struct list_head list; |
| 68 | }; |
| 69 | struct cipso_v4_map_cache_entry { |
| 70 | u32 hash; |
| 71 | unsigned char *key; |
| 72 | size_t key_len; |
| 73 | |
paul.moore@hp.com | ffb733c | 2006-10-04 11:46:31 -0400 | [diff] [blame] | 74 | struct netlbl_lsm_cache *lsm_data; |
Paul Moore | 446fda4 | 2006-08-03 16:48:06 -0700 | [diff] [blame] | 75 | |
| 76 | u32 activity; |
| 77 | struct list_head list; |
| 78 | }; |
| 79 | static struct cipso_v4_map_cache_bkt *cipso_v4_cache = NULL; |
| 80 | |
| 81 | /* Restricted bitmap (tag #1) flags */ |
| 82 | int cipso_v4_rbm_optfmt = 0; |
| 83 | int cipso_v4_rbm_strictvalid = 1; |
| 84 | |
| 85 | /* |
Paul Moore | f998e8c | 2007-02-28 15:14:20 -0500 | [diff] [blame] | 86 | * Protocol Constants |
| 87 | */ |
| 88 | |
| 89 | /* Maximum size of the CIPSO IP option, derived from the fact that the maximum |
| 90 | * IPv4 header size is 60 bytes and the base IPv4 header is 20 bytes long. */ |
| 91 | #define CIPSO_V4_OPT_LEN_MAX 40 |
| 92 | |
| 93 | /* Length of the base CIPSO option, this includes the option type (1 byte), the |
| 94 | * option length (1 byte), and the DOI (4 bytes). */ |
| 95 | #define CIPSO_V4_HDR_LEN 6 |
| 96 | |
| 97 | /* Base length of the restrictive category bitmap tag (tag #1). */ |
| 98 | #define CIPSO_V4_TAG_RBM_BLEN 4 |
| 99 | |
| 100 | /* Base length of the enumerated category tag (tag #2). */ |
| 101 | #define CIPSO_V4_TAG_ENUM_BLEN 4 |
| 102 | |
| 103 | /* Base length of the ranged categories bitmap tag (tag #5). */ |
| 104 | #define CIPSO_V4_TAG_RNG_BLEN 4 |
| 105 | /* The maximum number of category ranges permitted in the ranged category tag |
| 106 | * (tag #5). You may note that the IETF draft states that the maximum number |
| 107 | * of category ranges is 7, but if the low end of the last category range is |
| 108 | * zero then it is possibile to fit 8 category ranges because the zero should |
| 109 | * be omitted. */ |
| 110 | #define CIPSO_V4_TAG_RNG_CAT_MAX 8 |
| 111 | |
Paul Moore | 15c45f7 | 2008-10-10 10:16:34 -0400 | [diff] [blame] | 112 | /* Base length of the local tag (non-standard tag). |
| 113 | * Tag definition (may change between kernel versions) |
| 114 | * |
| 115 | * 0 8 16 24 32 |
| 116 | * +----------+----------+----------+----------+ |
| 117 | * | 10000000 | 00000110 | 32-bit secid value | |
| 118 | * +----------+----------+----------+----------+ |
| 119 | * | in (host byte order)| |
| 120 | * +----------+----------+ |
| 121 | * |
| 122 | */ |
| 123 | #define CIPSO_V4_TAG_LOC_BLEN 6 |
| 124 | |
Paul Moore | f998e8c | 2007-02-28 15:14:20 -0500 | [diff] [blame] | 125 | /* |
Paul Moore | 446fda4 | 2006-08-03 16:48:06 -0700 | [diff] [blame] | 126 | * Helper Functions |
| 127 | */ |
| 128 | |
| 129 | /** |
| 130 | * cipso_v4_bitmap_walk - Walk a bitmap looking for a bit |
| 131 | * @bitmap: the bitmap |
| 132 | * @bitmap_len: length in bits |
| 133 | * @offset: starting offset |
| 134 | * @state: if non-zero, look for a set (1) bit else look for a cleared (0) bit |
| 135 | * |
| 136 | * Description: |
| 137 | * Starting at @offset, walk the bitmap from left to right until either the |
| 138 | * desired bit is found or we reach the end. Return the bit offset, -1 if |
| 139 | * not found, or -2 if error. |
| 140 | */ |
| 141 | static int cipso_v4_bitmap_walk(const unsigned char *bitmap, |
| 142 | u32 bitmap_len, |
| 143 | u32 offset, |
| 144 | u8 state) |
| 145 | { |
| 146 | u32 bit_spot; |
| 147 | u32 byte_offset; |
| 148 | unsigned char bitmask; |
| 149 | unsigned char byte; |
| 150 | |
| 151 | /* gcc always rounds to zero when doing integer division */ |
| 152 | byte_offset = offset / 8; |
| 153 | byte = bitmap[byte_offset]; |
| 154 | bit_spot = offset; |
| 155 | bitmask = 0x80 >> (offset % 8); |
| 156 | |
| 157 | while (bit_spot < bitmap_len) { |
| 158 | if ((state && (byte & bitmask) == bitmask) || |
| 159 | (state == 0 && (byte & bitmask) == 0)) |
| 160 | return bit_spot; |
| 161 | |
| 162 | bit_spot++; |
| 163 | bitmask >>= 1; |
| 164 | if (bitmask == 0) { |
| 165 | byte = bitmap[++byte_offset]; |
| 166 | bitmask = 0x80; |
| 167 | } |
| 168 | } |
| 169 | |
| 170 | return -1; |
| 171 | } |
| 172 | |
| 173 | /** |
| 174 | * cipso_v4_bitmap_setbit - Sets a single bit in a bitmap |
| 175 | * @bitmap: the bitmap |
| 176 | * @bit: the bit |
| 177 | * @state: if non-zero, set the bit (1) else clear the bit (0) |
| 178 | * |
| 179 | * Description: |
| 180 | * Set a single bit in the bitmask. Returns zero on success, negative values |
| 181 | * on error. |
| 182 | */ |
| 183 | static void cipso_v4_bitmap_setbit(unsigned char *bitmap, |
| 184 | u32 bit, |
| 185 | u8 state) |
| 186 | { |
| 187 | u32 byte_spot; |
| 188 | u8 bitmask; |
| 189 | |
| 190 | /* gcc always rounds to zero when doing integer division */ |
| 191 | byte_spot = bit / 8; |
| 192 | bitmask = 0x80 >> (bit % 8); |
| 193 | if (state) |
| 194 | bitmap[byte_spot] |= bitmask; |
| 195 | else |
| 196 | bitmap[byte_spot] &= ~bitmask; |
| 197 | } |
| 198 | |
| 199 | /** |
Paul Moore | 446fda4 | 2006-08-03 16:48:06 -0700 | [diff] [blame] | 200 | * cipso_v4_cache_entry_free - Frees a cache entry |
| 201 | * @entry: the entry to free |
| 202 | * |
| 203 | * Description: |
paul.moore@hp.com | ffb733c | 2006-10-04 11:46:31 -0400 | [diff] [blame] | 204 | * This function frees the memory associated with a cache entry including the |
| 205 | * LSM cache data if there are no longer any users, i.e. reference count == 0. |
Paul Moore | 446fda4 | 2006-08-03 16:48:06 -0700 | [diff] [blame] | 206 | * |
| 207 | */ |
| 208 | static void cipso_v4_cache_entry_free(struct cipso_v4_map_cache_entry *entry) |
| 209 | { |
paul.moore@hp.com | ffb733c | 2006-10-04 11:46:31 -0400 | [diff] [blame] | 210 | if (entry->lsm_data) |
| 211 | netlbl_secattr_cache_free(entry->lsm_data); |
Paul Moore | 446fda4 | 2006-08-03 16:48:06 -0700 | [diff] [blame] | 212 | kfree(entry->key); |
| 213 | kfree(entry); |
| 214 | } |
| 215 | |
| 216 | /** |
| 217 | * cipso_v4_map_cache_hash - Hashing function for the CIPSO cache |
| 218 | * @key: the hash key |
| 219 | * @key_len: the length of the key in bytes |
| 220 | * |
| 221 | * Description: |
| 222 | * The CIPSO tag hashing function. Returns a 32-bit hash value. |
| 223 | * |
| 224 | */ |
| 225 | static u32 cipso_v4_map_cache_hash(const unsigned char *key, u32 key_len) |
| 226 | { |
| 227 | return jhash(key, key_len, 0); |
| 228 | } |
| 229 | |
| 230 | /* |
| 231 | * Label Mapping Cache Functions |
| 232 | */ |
| 233 | |
| 234 | /** |
| 235 | * cipso_v4_cache_init - Initialize the CIPSO cache |
| 236 | * |
| 237 | * Description: |
| 238 | * Initializes the CIPSO label mapping cache, this function should be called |
| 239 | * before any of the other functions defined in this file. Returns zero on |
| 240 | * success, negative values on error. |
| 241 | * |
| 242 | */ |
| 243 | static int cipso_v4_cache_init(void) |
| 244 | { |
| 245 | u32 iter; |
| 246 | |
| 247 | cipso_v4_cache = kcalloc(CIPSO_V4_CACHE_BUCKETS, |
| 248 | sizeof(struct cipso_v4_map_cache_bkt), |
| 249 | GFP_KERNEL); |
| 250 | if (cipso_v4_cache == NULL) |
| 251 | return -ENOMEM; |
| 252 | |
| 253 | for (iter = 0; iter < CIPSO_V4_CACHE_BUCKETS; iter++) { |
| 254 | spin_lock_init(&cipso_v4_cache[iter].lock); |
| 255 | cipso_v4_cache[iter].size = 0; |
| 256 | INIT_LIST_HEAD(&cipso_v4_cache[iter].list); |
| 257 | } |
| 258 | |
| 259 | return 0; |
| 260 | } |
| 261 | |
| 262 | /** |
| 263 | * cipso_v4_cache_invalidate - Invalidates the current CIPSO cache |
| 264 | * |
| 265 | * Description: |
| 266 | * Invalidates and frees any entries in the CIPSO cache. Returns zero on |
| 267 | * success and negative values on failure. |
| 268 | * |
| 269 | */ |
| 270 | void cipso_v4_cache_invalidate(void) |
| 271 | { |
| 272 | struct cipso_v4_map_cache_entry *entry, *tmp_entry; |
| 273 | u32 iter; |
| 274 | |
| 275 | for (iter = 0; iter < CIPSO_V4_CACHE_BUCKETS; iter++) { |
Paul Moore | 609c92f | 2006-09-25 15:52:37 -0700 | [diff] [blame] | 276 | spin_lock_bh(&cipso_v4_cache[iter].lock); |
Paul Moore | 446fda4 | 2006-08-03 16:48:06 -0700 | [diff] [blame] | 277 | list_for_each_entry_safe(entry, |
| 278 | tmp_entry, |
| 279 | &cipso_v4_cache[iter].list, list) { |
| 280 | list_del(&entry->list); |
| 281 | cipso_v4_cache_entry_free(entry); |
| 282 | } |
| 283 | cipso_v4_cache[iter].size = 0; |
Paul Moore | 609c92f | 2006-09-25 15:52:37 -0700 | [diff] [blame] | 284 | spin_unlock_bh(&cipso_v4_cache[iter].lock); |
Paul Moore | 446fda4 | 2006-08-03 16:48:06 -0700 | [diff] [blame] | 285 | } |
| 286 | |
| 287 | return; |
| 288 | } |
| 289 | |
| 290 | /** |
| 291 | * cipso_v4_cache_check - Check the CIPSO cache for a label mapping |
| 292 | * @key: the buffer to check |
| 293 | * @key_len: buffer length in bytes |
| 294 | * @secattr: the security attribute struct to use |
| 295 | * |
| 296 | * Description: |
| 297 | * This function checks the cache to see if a label mapping already exists for |
| 298 | * the given key. If there is a match then the cache is adjusted and the |
| 299 | * @secattr struct is populated with the correct LSM security attributes. The |
| 300 | * cache is adjusted in the following manner if the entry is not already the |
| 301 | * first in the cache bucket: |
| 302 | * |
| 303 | * 1. The cache entry's activity counter is incremented |
| 304 | * 2. The previous (higher ranking) entry's activity counter is decremented |
| 305 | * 3. If the difference between the two activity counters is geater than |
| 306 | * CIPSO_V4_CACHE_REORDERLIMIT the two entries are swapped |
| 307 | * |
| 308 | * Returns zero on success, -ENOENT for a cache miss, and other negative values |
| 309 | * on error. |
| 310 | * |
| 311 | */ |
| 312 | static int cipso_v4_cache_check(const unsigned char *key, |
| 313 | u32 key_len, |
| 314 | struct netlbl_lsm_secattr *secattr) |
| 315 | { |
| 316 | u32 bkt; |
| 317 | struct cipso_v4_map_cache_entry *entry; |
| 318 | struct cipso_v4_map_cache_entry *prev_entry = NULL; |
| 319 | u32 hash; |
| 320 | |
| 321 | if (!cipso_v4_cache_enabled) |
| 322 | return -ENOENT; |
| 323 | |
| 324 | hash = cipso_v4_map_cache_hash(key, key_len); |
Pavel Emelyanov | 5e0f892 | 2008-05-13 23:23:55 -0700 | [diff] [blame] | 325 | bkt = hash & (CIPSO_V4_CACHE_BUCKETS - 1); |
Paul Moore | 609c92f | 2006-09-25 15:52:37 -0700 | [diff] [blame] | 326 | spin_lock_bh(&cipso_v4_cache[bkt].lock); |
Paul Moore | 446fda4 | 2006-08-03 16:48:06 -0700 | [diff] [blame] | 327 | list_for_each_entry(entry, &cipso_v4_cache[bkt].list, list) { |
| 328 | if (entry->hash == hash && |
| 329 | entry->key_len == key_len && |
| 330 | memcmp(entry->key, key, key_len) == 0) { |
| 331 | entry->activity += 1; |
paul.moore@hp.com | ffb733c | 2006-10-04 11:46:31 -0400 | [diff] [blame] | 332 | atomic_inc(&entry->lsm_data->refcount); |
| 333 | secattr->cache = entry->lsm_data; |
Paul Moore | 701a90b | 2006-11-17 17:38:46 -0500 | [diff] [blame] | 334 | secattr->flags |= NETLBL_SECATTR_CACHE; |
Paul Moore | 16efd45 | 2008-01-29 08:37:59 -0500 | [diff] [blame] | 335 | secattr->type = NETLBL_NLTYPE_CIPSOV4; |
Paul Moore | 446fda4 | 2006-08-03 16:48:06 -0700 | [diff] [blame] | 336 | if (prev_entry == NULL) { |
Paul Moore | 609c92f | 2006-09-25 15:52:37 -0700 | [diff] [blame] | 337 | spin_unlock_bh(&cipso_v4_cache[bkt].lock); |
Paul Moore | 446fda4 | 2006-08-03 16:48:06 -0700 | [diff] [blame] | 338 | return 0; |
| 339 | } |
| 340 | |
| 341 | if (prev_entry->activity > 0) |
| 342 | prev_entry->activity -= 1; |
| 343 | if (entry->activity > prev_entry->activity && |
| 344 | entry->activity - prev_entry->activity > |
| 345 | CIPSO_V4_CACHE_REORDERLIMIT) { |
| 346 | __list_del(entry->list.prev, entry->list.next); |
| 347 | __list_add(&entry->list, |
| 348 | prev_entry->list.prev, |
| 349 | &prev_entry->list); |
| 350 | } |
| 351 | |
Paul Moore | 609c92f | 2006-09-25 15:52:37 -0700 | [diff] [blame] | 352 | spin_unlock_bh(&cipso_v4_cache[bkt].lock); |
Paul Moore | 446fda4 | 2006-08-03 16:48:06 -0700 | [diff] [blame] | 353 | return 0; |
| 354 | } |
| 355 | prev_entry = entry; |
| 356 | } |
Paul Moore | 609c92f | 2006-09-25 15:52:37 -0700 | [diff] [blame] | 357 | spin_unlock_bh(&cipso_v4_cache[bkt].lock); |
Paul Moore | 446fda4 | 2006-08-03 16:48:06 -0700 | [diff] [blame] | 358 | |
| 359 | return -ENOENT; |
| 360 | } |
| 361 | |
| 362 | /** |
| 363 | * cipso_v4_cache_add - Add an entry to the CIPSO cache |
| 364 | * @skb: the packet |
| 365 | * @secattr: the packet's security attributes |
| 366 | * |
| 367 | * Description: |
| 368 | * Add a new entry into the CIPSO label mapping cache. Add the new entry to |
| 369 | * head of the cache bucket's list, if the cache bucket is out of room remove |
| 370 | * the last entry in the list first. It is important to note that there is |
| 371 | * currently no checking for duplicate keys. Returns zero on success, |
| 372 | * negative values on failure. |
| 373 | * |
| 374 | */ |
| 375 | int cipso_v4_cache_add(const struct sk_buff *skb, |
| 376 | const struct netlbl_lsm_secattr *secattr) |
| 377 | { |
| 378 | int ret_val = -EPERM; |
| 379 | u32 bkt; |
| 380 | struct cipso_v4_map_cache_entry *entry = NULL; |
| 381 | struct cipso_v4_map_cache_entry *old_entry = NULL; |
| 382 | unsigned char *cipso_ptr; |
| 383 | u32 cipso_ptr_len; |
| 384 | |
| 385 | if (!cipso_v4_cache_enabled || cipso_v4_cache_bucketsize <= 0) |
| 386 | return 0; |
| 387 | |
| 388 | cipso_ptr = CIPSO_V4_OPTPTR(skb); |
| 389 | cipso_ptr_len = cipso_ptr[1]; |
| 390 | |
| 391 | entry = kzalloc(sizeof(*entry), GFP_ATOMIC); |
| 392 | if (entry == NULL) |
| 393 | return -ENOMEM; |
Arnaldo Carvalho de Melo | fac5d73 | 2006-11-17 11:14:16 -0200 | [diff] [blame] | 394 | entry->key = kmemdup(cipso_ptr, cipso_ptr_len, GFP_ATOMIC); |
Paul Moore | 446fda4 | 2006-08-03 16:48:06 -0700 | [diff] [blame] | 395 | if (entry->key == NULL) { |
| 396 | ret_val = -ENOMEM; |
| 397 | goto cache_add_failure; |
| 398 | } |
Paul Moore | 446fda4 | 2006-08-03 16:48:06 -0700 | [diff] [blame] | 399 | entry->key_len = cipso_ptr_len; |
| 400 | entry->hash = cipso_v4_map_cache_hash(cipso_ptr, cipso_ptr_len); |
paul.moore@hp.com | ffb733c | 2006-10-04 11:46:31 -0400 | [diff] [blame] | 401 | atomic_inc(&secattr->cache->refcount); |
| 402 | entry->lsm_data = secattr->cache; |
Paul Moore | 446fda4 | 2006-08-03 16:48:06 -0700 | [diff] [blame] | 403 | |
Pavel Emelyanov | 5e0f892 | 2008-05-13 23:23:55 -0700 | [diff] [blame] | 404 | bkt = entry->hash & (CIPSO_V4_CACHE_BUCKETS - 1); |
Paul Moore | 609c92f | 2006-09-25 15:52:37 -0700 | [diff] [blame] | 405 | spin_lock_bh(&cipso_v4_cache[bkt].lock); |
Paul Moore | 446fda4 | 2006-08-03 16:48:06 -0700 | [diff] [blame] | 406 | if (cipso_v4_cache[bkt].size < cipso_v4_cache_bucketsize) { |
| 407 | list_add(&entry->list, &cipso_v4_cache[bkt].list); |
| 408 | cipso_v4_cache[bkt].size += 1; |
| 409 | } else { |
| 410 | old_entry = list_entry(cipso_v4_cache[bkt].list.prev, |
| 411 | struct cipso_v4_map_cache_entry, list); |
| 412 | list_del(&old_entry->list); |
| 413 | list_add(&entry->list, &cipso_v4_cache[bkt].list); |
| 414 | cipso_v4_cache_entry_free(old_entry); |
| 415 | } |
Paul Moore | 609c92f | 2006-09-25 15:52:37 -0700 | [diff] [blame] | 416 | spin_unlock_bh(&cipso_v4_cache[bkt].lock); |
Paul Moore | 446fda4 | 2006-08-03 16:48:06 -0700 | [diff] [blame] | 417 | |
| 418 | return 0; |
| 419 | |
| 420 | cache_add_failure: |
| 421 | if (entry) |
| 422 | cipso_v4_cache_entry_free(entry); |
| 423 | return ret_val; |
| 424 | } |
| 425 | |
| 426 | /* |
| 427 | * DOI List Functions |
| 428 | */ |
| 429 | |
| 430 | /** |
| 431 | * cipso_v4_doi_search - Searches for a DOI definition |
| 432 | * @doi: the DOI to search for |
| 433 | * |
| 434 | * Description: |
| 435 | * Search the DOI definition list for a DOI definition with a DOI value that |
| 436 | * matches @doi. The caller is responsibile for calling rcu_read_[un]lock(). |
| 437 | * Returns a pointer to the DOI definition on success and NULL on failure. |
| 438 | */ |
| 439 | static struct cipso_v4_doi *cipso_v4_doi_search(u32 doi) |
| 440 | { |
| 441 | struct cipso_v4_doi *iter; |
| 442 | |
| 443 | list_for_each_entry_rcu(iter, &cipso_v4_doi_list, list) |
Paul Moore | b1edeb1 | 2008-10-10 10:16:31 -0400 | [diff] [blame] | 444 | if (iter->doi == doi && atomic_read(&iter->refcount)) |
Paul Moore | 446fda4 | 2006-08-03 16:48:06 -0700 | [diff] [blame] | 445 | return iter; |
| 446 | return NULL; |
| 447 | } |
| 448 | |
| 449 | /** |
| 450 | * cipso_v4_doi_add - Add a new DOI to the CIPSO protocol engine |
| 451 | * @doi_def: the DOI structure |
| 452 | * |
| 453 | * Description: |
| 454 | * The caller defines a new DOI for use by the CIPSO engine and calls this |
| 455 | * function to add it to the list of acceptable domains. The caller must |
| 456 | * ensure that the mapping table specified in @doi_def->map meets all of the |
| 457 | * requirements of the mapping type (see cipso_ipv4.h for details). Returns |
| 458 | * zero on success and non-zero on failure. |
| 459 | * |
| 460 | */ |
| 461 | int cipso_v4_doi_add(struct cipso_v4_doi *doi_def) |
| 462 | { |
Paul Moore | 6ce61a7 | 2006-11-17 17:38:48 -0500 | [diff] [blame] | 463 | u32 iter; |
| 464 | |
Paul Moore | 446fda4 | 2006-08-03 16:48:06 -0700 | [diff] [blame] | 465 | if (doi_def == NULL || doi_def->doi == CIPSO_V4_DOI_UNKNOWN) |
| 466 | return -EINVAL; |
Paul Moore | 6ce61a7 | 2006-11-17 17:38:48 -0500 | [diff] [blame] | 467 | for (iter = 0; iter < CIPSO_V4_TAG_MAXCNT; iter++) { |
| 468 | switch (doi_def->tags[iter]) { |
| 469 | case CIPSO_V4_TAG_RBITMAP: |
| 470 | break; |
Paul Moore | 484b366 | 2006-11-29 13:18:20 -0500 | [diff] [blame] | 471 | case CIPSO_V4_TAG_RANGE: |
| 472 | if (doi_def->type != CIPSO_V4_MAP_PASS) |
| 473 | return -EINVAL; |
| 474 | break; |
Paul Moore | 6ce61a7 | 2006-11-17 17:38:48 -0500 | [diff] [blame] | 475 | case CIPSO_V4_TAG_INVALID: |
| 476 | if (iter == 0) |
| 477 | return -EINVAL; |
| 478 | break; |
Paul Moore | 654bbc2 | 2006-11-29 13:18:19 -0500 | [diff] [blame] | 479 | case CIPSO_V4_TAG_ENUM: |
| 480 | if (doi_def->type != CIPSO_V4_MAP_PASS) |
| 481 | return -EINVAL; |
| 482 | break; |
Paul Moore | 15c45f7 | 2008-10-10 10:16:34 -0400 | [diff] [blame] | 483 | case CIPSO_V4_TAG_LOCAL: |
| 484 | if (doi_def->type != CIPSO_V4_MAP_LOCAL) |
| 485 | return -EINVAL; |
| 486 | break; |
Paul Moore | 6ce61a7 | 2006-11-17 17:38:48 -0500 | [diff] [blame] | 487 | default: |
| 488 | return -EINVAL; |
| 489 | } |
| 490 | } |
Paul Moore | 446fda4 | 2006-08-03 16:48:06 -0700 | [diff] [blame] | 491 | |
Paul Moore | b1edeb1 | 2008-10-10 10:16:31 -0400 | [diff] [blame] | 492 | atomic_set(&doi_def->refcount, 1); |
Paul Moore | 446fda4 | 2006-08-03 16:48:06 -0700 | [diff] [blame] | 493 | |
Paul Moore | 446fda4 | 2006-08-03 16:48:06 -0700 | [diff] [blame] | 494 | spin_lock(&cipso_v4_doi_list_lock); |
| 495 | if (cipso_v4_doi_search(doi_def->doi) != NULL) |
Paul Moore | 4be2700 | 2007-10-26 04:29:08 -0700 | [diff] [blame] | 496 | goto doi_add_failure; |
Paul Moore | 446fda4 | 2006-08-03 16:48:06 -0700 | [diff] [blame] | 497 | list_add_tail_rcu(&doi_def->list, &cipso_v4_doi_list); |
| 498 | spin_unlock(&cipso_v4_doi_list_lock); |
Paul Moore | 446fda4 | 2006-08-03 16:48:06 -0700 | [diff] [blame] | 499 | |
| 500 | return 0; |
| 501 | |
Paul Moore | 4be2700 | 2007-10-26 04:29:08 -0700 | [diff] [blame] | 502 | doi_add_failure: |
Paul Moore | 446fda4 | 2006-08-03 16:48:06 -0700 | [diff] [blame] | 503 | spin_unlock(&cipso_v4_doi_list_lock); |
Paul Moore | 446fda4 | 2006-08-03 16:48:06 -0700 | [diff] [blame] | 504 | return -EEXIST; |
| 505 | } |
| 506 | |
| 507 | /** |
Paul Moore | b1edeb1 | 2008-10-10 10:16:31 -0400 | [diff] [blame] | 508 | * cipso_v4_doi_free - Frees a DOI definition |
| 509 | * @entry: the entry's RCU field |
Paul Moore | 446fda4 | 2006-08-03 16:48:06 -0700 | [diff] [blame] | 510 | * |
| 511 | * Description: |
Paul Moore | b1edeb1 | 2008-10-10 10:16:31 -0400 | [diff] [blame] | 512 | * This function frees all of the memory associated with a DOI definition. |
Paul Moore | 446fda4 | 2006-08-03 16:48:06 -0700 | [diff] [blame] | 513 | * |
| 514 | */ |
Paul Moore | b1edeb1 | 2008-10-10 10:16:31 -0400 | [diff] [blame] | 515 | void cipso_v4_doi_free(struct cipso_v4_doi *doi_def) |
Paul Moore | 446fda4 | 2006-08-03 16:48:06 -0700 | [diff] [blame] | 516 | { |
Paul Moore | b1edeb1 | 2008-10-10 10:16:31 -0400 | [diff] [blame] | 517 | if (doi_def == NULL) |
| 518 | return; |
Paul Moore | 446fda4 | 2006-08-03 16:48:06 -0700 | [diff] [blame] | 519 | |
Paul Moore | b1edeb1 | 2008-10-10 10:16:31 -0400 | [diff] [blame] | 520 | switch (doi_def->type) { |
Paul Moore | 15c45f7 | 2008-10-10 10:16:34 -0400 | [diff] [blame] | 521 | case CIPSO_V4_MAP_TRANS: |
Paul Moore | b1edeb1 | 2008-10-10 10:16:31 -0400 | [diff] [blame] | 522 | kfree(doi_def->map.std->lvl.cipso); |
| 523 | kfree(doi_def->map.std->lvl.local); |
| 524 | kfree(doi_def->map.std->cat.cipso); |
| 525 | kfree(doi_def->map.std->cat.local); |
| 526 | break; |
Paul Moore | 446fda4 | 2006-08-03 16:48:06 -0700 | [diff] [blame] | 527 | } |
Paul Moore | b1edeb1 | 2008-10-10 10:16:31 -0400 | [diff] [blame] | 528 | kfree(doi_def); |
Paul Moore | 446fda4 | 2006-08-03 16:48:06 -0700 | [diff] [blame] | 529 | } |
| 530 | |
| 531 | /** |
Paul Moore | b1edeb1 | 2008-10-10 10:16:31 -0400 | [diff] [blame] | 532 | * cipso_v4_doi_free_rcu - Frees a DOI definition via the RCU pointer |
| 533 | * @entry: the entry's RCU field |
| 534 | * |
| 535 | * Description: |
| 536 | * This function is designed to be used as a callback to the call_rcu() |
| 537 | * function so that the memory allocated to the DOI definition can be released |
| 538 | * safely. |
| 539 | * |
| 540 | */ |
| 541 | static void cipso_v4_doi_free_rcu(struct rcu_head *entry) |
| 542 | { |
| 543 | struct cipso_v4_doi *doi_def; |
| 544 | |
| 545 | doi_def = container_of(entry, struct cipso_v4_doi, rcu); |
| 546 | cipso_v4_doi_free(doi_def); |
| 547 | } |
| 548 | |
| 549 | /** |
| 550 | * cipso_v4_doi_remove - Remove an existing DOI from the CIPSO protocol engine |
| 551 | * @doi: the DOI value |
| 552 | * @audit_secid: the LSM secid to use in the audit message |
| 553 | * |
| 554 | * Description: |
| 555 | * Removes a DOI definition from the CIPSO engine. The NetLabel routines will |
| 556 | * be called to release their own LSM domain mappings as well as our own |
| 557 | * domain list. Returns zero on success and negative values on failure. |
| 558 | * |
| 559 | */ |
| 560 | int cipso_v4_doi_remove(u32 doi, struct netlbl_audit *audit_info) |
| 561 | { |
| 562 | struct cipso_v4_doi *doi_def; |
| 563 | |
| 564 | spin_lock(&cipso_v4_doi_list_lock); |
| 565 | doi_def = cipso_v4_doi_search(doi); |
| 566 | if (doi_def == NULL) { |
| 567 | spin_unlock(&cipso_v4_doi_list_lock); |
| 568 | return -ENOENT; |
| 569 | } |
| 570 | if (!atomic_dec_and_test(&doi_def->refcount)) { |
| 571 | spin_unlock(&cipso_v4_doi_list_lock); |
| 572 | return -EBUSY; |
| 573 | } |
| 574 | list_del_rcu(&doi_def->list); |
| 575 | spin_unlock(&cipso_v4_doi_list_lock); |
| 576 | |
| 577 | cipso_v4_cache_invalidate(); |
| 578 | call_rcu(&doi_def->rcu, cipso_v4_doi_free_rcu); |
| 579 | |
| 580 | return 0; |
| 581 | } |
| 582 | |
| 583 | /** |
| 584 | * cipso_v4_doi_getdef - Returns a reference to a valid DOI definition |
Paul Moore | 446fda4 | 2006-08-03 16:48:06 -0700 | [diff] [blame] | 585 | * @doi: the DOI value |
| 586 | * |
| 587 | * Description: |
| 588 | * Searches for a valid DOI definition and if one is found it is returned to |
| 589 | * the caller. Otherwise NULL is returned. The caller must ensure that |
Paul Moore | b1edeb1 | 2008-10-10 10:16:31 -0400 | [diff] [blame] | 590 | * rcu_read_lock() is held while accessing the returned definition and the DOI |
| 591 | * definition reference count is decremented when the caller is done. |
Paul Moore | 446fda4 | 2006-08-03 16:48:06 -0700 | [diff] [blame] | 592 | * |
| 593 | */ |
| 594 | struct cipso_v4_doi *cipso_v4_doi_getdef(u32 doi) |
| 595 | { |
Paul Moore | b1edeb1 | 2008-10-10 10:16:31 -0400 | [diff] [blame] | 596 | struct cipso_v4_doi *doi_def; |
| 597 | |
| 598 | rcu_read_lock(); |
| 599 | doi_def = cipso_v4_doi_search(doi); |
| 600 | if (doi_def == NULL) |
| 601 | goto doi_getdef_return; |
| 602 | if (!atomic_inc_not_zero(&doi_def->refcount)) |
| 603 | doi_def = NULL; |
| 604 | |
| 605 | doi_getdef_return: |
| 606 | rcu_read_unlock(); |
| 607 | return doi_def; |
| 608 | } |
| 609 | |
| 610 | /** |
| 611 | * cipso_v4_doi_putdef - Releases a reference for the given DOI definition |
| 612 | * @doi_def: the DOI definition |
| 613 | * |
| 614 | * Description: |
| 615 | * Releases a DOI definition reference obtained from cipso_v4_doi_getdef(). |
| 616 | * |
| 617 | */ |
| 618 | void cipso_v4_doi_putdef(struct cipso_v4_doi *doi_def) |
| 619 | { |
| 620 | if (doi_def == NULL) |
| 621 | return; |
| 622 | |
| 623 | if (!atomic_dec_and_test(&doi_def->refcount)) |
| 624 | return; |
| 625 | spin_lock(&cipso_v4_doi_list_lock); |
| 626 | list_del_rcu(&doi_def->list); |
| 627 | spin_unlock(&cipso_v4_doi_list_lock); |
| 628 | |
| 629 | cipso_v4_cache_invalidate(); |
| 630 | call_rcu(&doi_def->rcu, cipso_v4_doi_free_rcu); |
Paul Moore | 446fda4 | 2006-08-03 16:48:06 -0700 | [diff] [blame] | 631 | } |
| 632 | |
| 633 | /** |
Paul Moore | fcd4828 | 2006-09-25 15:56:09 -0700 | [diff] [blame] | 634 | * cipso_v4_doi_walk - Iterate through the DOI definitions |
| 635 | * @skip_cnt: skip past this number of DOI definitions, updated |
| 636 | * @callback: callback for each DOI definition |
| 637 | * @cb_arg: argument for the callback function |
Paul Moore | 446fda4 | 2006-08-03 16:48:06 -0700 | [diff] [blame] | 638 | * |
| 639 | * Description: |
Paul Moore | fcd4828 | 2006-09-25 15:56:09 -0700 | [diff] [blame] | 640 | * Iterate over the DOI definition list, skipping the first @skip_cnt entries. |
| 641 | * For each entry call @callback, if @callback returns a negative value stop |
| 642 | * 'walking' through the list and return. Updates the value in @skip_cnt upon |
| 643 | * return. Returns zero on success, negative values on failure. |
Paul Moore | 446fda4 | 2006-08-03 16:48:06 -0700 | [diff] [blame] | 644 | * |
| 645 | */ |
Paul Moore | fcd4828 | 2006-09-25 15:56:09 -0700 | [diff] [blame] | 646 | int cipso_v4_doi_walk(u32 *skip_cnt, |
| 647 | int (*callback) (struct cipso_v4_doi *doi_def, void *arg), |
| 648 | void *cb_arg) |
Paul Moore | 446fda4 | 2006-08-03 16:48:06 -0700 | [diff] [blame] | 649 | { |
Paul Moore | fcd4828 | 2006-09-25 15:56:09 -0700 | [diff] [blame] | 650 | int ret_val = -ENOENT; |
Paul Moore | 446fda4 | 2006-08-03 16:48:06 -0700 | [diff] [blame] | 651 | u32 doi_cnt = 0; |
Paul Moore | fcd4828 | 2006-09-25 15:56:09 -0700 | [diff] [blame] | 652 | struct cipso_v4_doi *iter_doi; |
Paul Moore | 446fda4 | 2006-08-03 16:48:06 -0700 | [diff] [blame] | 653 | |
| 654 | rcu_read_lock(); |
Paul Moore | fcd4828 | 2006-09-25 15:56:09 -0700 | [diff] [blame] | 655 | list_for_each_entry_rcu(iter_doi, &cipso_v4_doi_list, list) |
Paul Moore | b1edeb1 | 2008-10-10 10:16:31 -0400 | [diff] [blame] | 656 | if (atomic_read(&iter_doi->refcount) > 0) { |
Paul Moore | fcd4828 | 2006-09-25 15:56:09 -0700 | [diff] [blame] | 657 | if (doi_cnt++ < *skip_cnt) |
| 658 | continue; |
| 659 | ret_val = callback(iter_doi, cb_arg); |
| 660 | if (ret_val < 0) { |
| 661 | doi_cnt--; |
| 662 | goto doi_walk_return; |
Paul Moore | 446fda4 | 2006-08-03 16:48:06 -0700 | [diff] [blame] | 663 | } |
Paul Moore | fcd4828 | 2006-09-25 15:56:09 -0700 | [diff] [blame] | 664 | } |
Paul Moore | 446fda4 | 2006-08-03 16:48:06 -0700 | [diff] [blame] | 665 | |
Paul Moore | fcd4828 | 2006-09-25 15:56:09 -0700 | [diff] [blame] | 666 | doi_walk_return: |
Paul Moore | 446fda4 | 2006-08-03 16:48:06 -0700 | [diff] [blame] | 667 | rcu_read_unlock(); |
Paul Moore | fcd4828 | 2006-09-25 15:56:09 -0700 | [diff] [blame] | 668 | *skip_cnt = doi_cnt; |
| 669 | return ret_val; |
Paul Moore | 446fda4 | 2006-08-03 16:48:06 -0700 | [diff] [blame] | 670 | } |
| 671 | |
Paul Moore | 446fda4 | 2006-08-03 16:48:06 -0700 | [diff] [blame] | 672 | /* |
| 673 | * Label Mapping Functions |
| 674 | */ |
| 675 | |
| 676 | /** |
| 677 | * cipso_v4_map_lvl_valid - Checks to see if the given level is understood |
| 678 | * @doi_def: the DOI definition |
| 679 | * @level: the level to check |
| 680 | * |
| 681 | * Description: |
| 682 | * Checks the given level against the given DOI definition and returns a |
| 683 | * negative value if the level does not have a valid mapping and a zero value |
| 684 | * if the level is defined by the DOI. |
| 685 | * |
| 686 | */ |
| 687 | static int cipso_v4_map_lvl_valid(const struct cipso_v4_doi *doi_def, u8 level) |
| 688 | { |
| 689 | switch (doi_def->type) { |
| 690 | case CIPSO_V4_MAP_PASS: |
| 691 | return 0; |
Paul Moore | 15c45f7 | 2008-10-10 10:16:34 -0400 | [diff] [blame] | 692 | case CIPSO_V4_MAP_TRANS: |
Paul Moore | 446fda4 | 2006-08-03 16:48:06 -0700 | [diff] [blame] | 693 | if (doi_def->map.std->lvl.cipso[level] < CIPSO_V4_INV_LVL) |
| 694 | return 0; |
| 695 | break; |
| 696 | } |
| 697 | |
| 698 | return -EFAULT; |
| 699 | } |
| 700 | |
| 701 | /** |
| 702 | * cipso_v4_map_lvl_hton - Perform a level mapping from the host to the network |
| 703 | * @doi_def: the DOI definition |
| 704 | * @host_lvl: the host MLS level |
| 705 | * @net_lvl: the network/CIPSO MLS level |
| 706 | * |
| 707 | * Description: |
| 708 | * Perform a label mapping to translate a local MLS level to the correct |
| 709 | * CIPSO level using the given DOI definition. Returns zero on success, |
| 710 | * negative values otherwise. |
| 711 | * |
| 712 | */ |
| 713 | static int cipso_v4_map_lvl_hton(const struct cipso_v4_doi *doi_def, |
| 714 | u32 host_lvl, |
| 715 | u32 *net_lvl) |
| 716 | { |
| 717 | switch (doi_def->type) { |
| 718 | case CIPSO_V4_MAP_PASS: |
| 719 | *net_lvl = host_lvl; |
| 720 | return 0; |
Paul Moore | 15c45f7 | 2008-10-10 10:16:34 -0400 | [diff] [blame] | 721 | case CIPSO_V4_MAP_TRANS: |
Paul Moore | c6387a8 | 2007-03-02 13:19:02 -0800 | [diff] [blame] | 722 | if (host_lvl < doi_def->map.std->lvl.local_size && |
| 723 | doi_def->map.std->lvl.local[host_lvl] < CIPSO_V4_INV_LVL) { |
Paul Moore | 446fda4 | 2006-08-03 16:48:06 -0700 | [diff] [blame] | 724 | *net_lvl = doi_def->map.std->lvl.local[host_lvl]; |
| 725 | return 0; |
| 726 | } |
Paul Moore | c6387a8 | 2007-03-02 13:19:02 -0800 | [diff] [blame] | 727 | return -EPERM; |
Paul Moore | 446fda4 | 2006-08-03 16:48:06 -0700 | [diff] [blame] | 728 | } |
| 729 | |
| 730 | return -EINVAL; |
| 731 | } |
| 732 | |
| 733 | /** |
| 734 | * cipso_v4_map_lvl_ntoh - Perform a level mapping from the network to the host |
| 735 | * @doi_def: the DOI definition |
| 736 | * @net_lvl: the network/CIPSO MLS level |
| 737 | * @host_lvl: the host MLS level |
| 738 | * |
| 739 | * Description: |
| 740 | * Perform a label mapping to translate a CIPSO level to the correct local MLS |
| 741 | * level using the given DOI definition. Returns zero on success, negative |
| 742 | * values otherwise. |
| 743 | * |
| 744 | */ |
| 745 | static int cipso_v4_map_lvl_ntoh(const struct cipso_v4_doi *doi_def, |
| 746 | u32 net_lvl, |
| 747 | u32 *host_lvl) |
| 748 | { |
| 749 | struct cipso_v4_std_map_tbl *map_tbl; |
| 750 | |
| 751 | switch (doi_def->type) { |
| 752 | case CIPSO_V4_MAP_PASS: |
| 753 | *host_lvl = net_lvl; |
| 754 | return 0; |
Paul Moore | 15c45f7 | 2008-10-10 10:16:34 -0400 | [diff] [blame] | 755 | case CIPSO_V4_MAP_TRANS: |
Paul Moore | 446fda4 | 2006-08-03 16:48:06 -0700 | [diff] [blame] | 756 | map_tbl = doi_def->map.std; |
| 757 | if (net_lvl < map_tbl->lvl.cipso_size && |
| 758 | map_tbl->lvl.cipso[net_lvl] < CIPSO_V4_INV_LVL) { |
| 759 | *host_lvl = doi_def->map.std->lvl.cipso[net_lvl]; |
| 760 | return 0; |
| 761 | } |
Paul Moore | c6387a8 | 2007-03-02 13:19:02 -0800 | [diff] [blame] | 762 | return -EPERM; |
Paul Moore | 446fda4 | 2006-08-03 16:48:06 -0700 | [diff] [blame] | 763 | } |
| 764 | |
| 765 | return -EINVAL; |
| 766 | } |
| 767 | |
| 768 | /** |
| 769 | * cipso_v4_map_cat_rbm_valid - Checks to see if the category bitmap is valid |
| 770 | * @doi_def: the DOI definition |
| 771 | * @bitmap: category bitmap |
| 772 | * @bitmap_len: bitmap length in bytes |
| 773 | * |
| 774 | * Description: |
| 775 | * Checks the given category bitmap against the given DOI definition and |
| 776 | * returns a negative value if any of the categories in the bitmap do not have |
| 777 | * a valid mapping and a zero value if all of the categories are valid. |
| 778 | * |
| 779 | */ |
| 780 | static int cipso_v4_map_cat_rbm_valid(const struct cipso_v4_doi *doi_def, |
| 781 | const unsigned char *bitmap, |
| 782 | u32 bitmap_len) |
| 783 | { |
| 784 | int cat = -1; |
| 785 | u32 bitmap_len_bits = bitmap_len * 8; |
Paul Moore | 044a68e | 2006-10-11 19:10:47 -0400 | [diff] [blame] | 786 | u32 cipso_cat_size; |
| 787 | u32 *cipso_array; |
Paul Moore | 446fda4 | 2006-08-03 16:48:06 -0700 | [diff] [blame] | 788 | |
| 789 | switch (doi_def->type) { |
| 790 | case CIPSO_V4_MAP_PASS: |
| 791 | return 0; |
Paul Moore | 15c45f7 | 2008-10-10 10:16:34 -0400 | [diff] [blame] | 792 | case CIPSO_V4_MAP_TRANS: |
Paul Moore | 044a68e | 2006-10-11 19:10:47 -0400 | [diff] [blame] | 793 | cipso_cat_size = doi_def->map.std->cat.cipso_size; |
| 794 | cipso_array = doi_def->map.std->cat.cipso; |
Paul Moore | 446fda4 | 2006-08-03 16:48:06 -0700 | [diff] [blame] | 795 | for (;;) { |
| 796 | cat = cipso_v4_bitmap_walk(bitmap, |
| 797 | bitmap_len_bits, |
| 798 | cat + 1, |
| 799 | 1); |
| 800 | if (cat < 0) |
| 801 | break; |
| 802 | if (cat >= cipso_cat_size || |
| 803 | cipso_array[cat] >= CIPSO_V4_INV_CAT) |
| 804 | return -EFAULT; |
| 805 | } |
| 806 | |
| 807 | if (cat == -1) |
| 808 | return 0; |
| 809 | break; |
| 810 | } |
| 811 | |
| 812 | return -EFAULT; |
| 813 | } |
| 814 | |
| 815 | /** |
| 816 | * cipso_v4_map_cat_rbm_hton - Perform a category mapping from host to network |
| 817 | * @doi_def: the DOI definition |
Paul Moore | 0275276 | 2006-11-29 13:18:18 -0500 | [diff] [blame] | 818 | * @secattr: the security attributes |
Paul Moore | 446fda4 | 2006-08-03 16:48:06 -0700 | [diff] [blame] | 819 | * @net_cat: the zero'd out category bitmap in network/CIPSO format |
| 820 | * @net_cat_len: the length of the CIPSO bitmap in bytes |
| 821 | * |
| 822 | * Description: |
| 823 | * Perform a label mapping to translate a local MLS category bitmap to the |
| 824 | * correct CIPSO bitmap using the given DOI definition. Returns the minimum |
| 825 | * size in bytes of the network bitmap on success, negative values otherwise. |
| 826 | * |
| 827 | */ |
| 828 | static int cipso_v4_map_cat_rbm_hton(const struct cipso_v4_doi *doi_def, |
Paul Moore | 0275276 | 2006-11-29 13:18:18 -0500 | [diff] [blame] | 829 | const struct netlbl_lsm_secattr *secattr, |
Paul Moore | 446fda4 | 2006-08-03 16:48:06 -0700 | [diff] [blame] | 830 | unsigned char *net_cat, |
| 831 | u32 net_cat_len) |
| 832 | { |
| 833 | int host_spot = -1; |
Paul Moore | 0275276 | 2006-11-29 13:18:18 -0500 | [diff] [blame] | 834 | u32 net_spot = CIPSO_V4_INV_CAT; |
Paul Moore | 446fda4 | 2006-08-03 16:48:06 -0700 | [diff] [blame] | 835 | u32 net_spot_max = 0; |
Paul Moore | 446fda4 | 2006-08-03 16:48:06 -0700 | [diff] [blame] | 836 | u32 net_clen_bits = net_cat_len * 8; |
Paul Moore | 0275276 | 2006-11-29 13:18:18 -0500 | [diff] [blame] | 837 | u32 host_cat_size = 0; |
| 838 | u32 *host_cat_array = NULL; |
Paul Moore | 446fda4 | 2006-08-03 16:48:06 -0700 | [diff] [blame] | 839 | |
Paul Moore | 15c45f7 | 2008-10-10 10:16:34 -0400 | [diff] [blame] | 840 | if (doi_def->type == CIPSO_V4_MAP_TRANS) { |
Paul Moore | 044a68e | 2006-10-11 19:10:47 -0400 | [diff] [blame] | 841 | host_cat_size = doi_def->map.std->cat.local_size; |
| 842 | host_cat_array = doi_def->map.std->cat.local; |
Paul Moore | 0275276 | 2006-11-29 13:18:18 -0500 | [diff] [blame] | 843 | } |
| 844 | |
| 845 | for (;;) { |
Paul Moore | 16efd45 | 2008-01-29 08:37:59 -0500 | [diff] [blame] | 846 | host_spot = netlbl_secattr_catmap_walk(secattr->attr.mls.cat, |
Paul Moore | 0275276 | 2006-11-29 13:18:18 -0500 | [diff] [blame] | 847 | host_spot + 1); |
| 848 | if (host_spot < 0) |
| 849 | break; |
| 850 | |
| 851 | switch (doi_def->type) { |
| 852 | case CIPSO_V4_MAP_PASS: |
| 853 | net_spot = host_spot; |
| 854 | break; |
Paul Moore | 15c45f7 | 2008-10-10 10:16:34 -0400 | [diff] [blame] | 855 | case CIPSO_V4_MAP_TRANS: |
Paul Moore | 446fda4 | 2006-08-03 16:48:06 -0700 | [diff] [blame] | 856 | if (host_spot >= host_cat_size) |
| 857 | return -EPERM; |
Paul Moore | 446fda4 | 2006-08-03 16:48:06 -0700 | [diff] [blame] | 858 | net_spot = host_cat_array[host_spot]; |
Paul Moore | 9fade4b | 2006-11-17 17:38:50 -0500 | [diff] [blame] | 859 | if (net_spot >= CIPSO_V4_INV_CAT) |
| 860 | return -EPERM; |
Paul Moore | 0275276 | 2006-11-29 13:18:18 -0500 | [diff] [blame] | 861 | break; |
Paul Moore | 446fda4 | 2006-08-03 16:48:06 -0700 | [diff] [blame] | 862 | } |
Paul Moore | 0275276 | 2006-11-29 13:18:18 -0500 | [diff] [blame] | 863 | if (net_spot >= net_clen_bits) |
| 864 | return -ENOSPC; |
| 865 | cipso_v4_bitmap_setbit(net_cat, net_spot, 1); |
Paul Moore | 446fda4 | 2006-08-03 16:48:06 -0700 | [diff] [blame] | 866 | |
Paul Moore | 0275276 | 2006-11-29 13:18:18 -0500 | [diff] [blame] | 867 | if (net_spot > net_spot_max) |
| 868 | net_spot_max = net_spot; |
Paul Moore | 446fda4 | 2006-08-03 16:48:06 -0700 | [diff] [blame] | 869 | } |
| 870 | |
Paul Moore | 0275276 | 2006-11-29 13:18:18 -0500 | [diff] [blame] | 871 | if (++net_spot_max % 8) |
| 872 | return net_spot_max / 8 + 1; |
| 873 | return net_spot_max / 8; |
Paul Moore | 446fda4 | 2006-08-03 16:48:06 -0700 | [diff] [blame] | 874 | } |
| 875 | |
| 876 | /** |
| 877 | * cipso_v4_map_cat_rbm_ntoh - Perform a category mapping from network to host |
| 878 | * @doi_def: the DOI definition |
| 879 | * @net_cat: the category bitmap in network/CIPSO format |
| 880 | * @net_cat_len: the length of the CIPSO bitmap in bytes |
Paul Moore | 0275276 | 2006-11-29 13:18:18 -0500 | [diff] [blame] | 881 | * @secattr: the security attributes |
Paul Moore | 446fda4 | 2006-08-03 16:48:06 -0700 | [diff] [blame] | 882 | * |
| 883 | * Description: |
| 884 | * Perform a label mapping to translate a CIPSO bitmap to the correct local |
Paul Moore | 0275276 | 2006-11-29 13:18:18 -0500 | [diff] [blame] | 885 | * MLS category bitmap using the given DOI definition. Returns zero on |
| 886 | * success, negative values on failure. |
Paul Moore | 446fda4 | 2006-08-03 16:48:06 -0700 | [diff] [blame] | 887 | * |
| 888 | */ |
| 889 | static int cipso_v4_map_cat_rbm_ntoh(const struct cipso_v4_doi *doi_def, |
| 890 | const unsigned char *net_cat, |
| 891 | u32 net_cat_len, |
Paul Moore | 0275276 | 2006-11-29 13:18:18 -0500 | [diff] [blame] | 892 | struct netlbl_lsm_secattr *secattr) |
Paul Moore | 446fda4 | 2006-08-03 16:48:06 -0700 | [diff] [blame] | 893 | { |
Paul Moore | 0275276 | 2006-11-29 13:18:18 -0500 | [diff] [blame] | 894 | int ret_val; |
Paul Moore | 446fda4 | 2006-08-03 16:48:06 -0700 | [diff] [blame] | 895 | int net_spot = -1; |
Paul Moore | 0275276 | 2006-11-29 13:18:18 -0500 | [diff] [blame] | 896 | u32 host_spot = CIPSO_V4_INV_CAT; |
Paul Moore | 446fda4 | 2006-08-03 16:48:06 -0700 | [diff] [blame] | 897 | u32 net_clen_bits = net_cat_len * 8; |
Paul Moore | 0275276 | 2006-11-29 13:18:18 -0500 | [diff] [blame] | 898 | u32 net_cat_size = 0; |
| 899 | u32 *net_cat_array = NULL; |
Paul Moore | 446fda4 | 2006-08-03 16:48:06 -0700 | [diff] [blame] | 900 | |
Paul Moore | 15c45f7 | 2008-10-10 10:16:34 -0400 | [diff] [blame] | 901 | if (doi_def->type == CIPSO_V4_MAP_TRANS) { |
Paul Moore | 044a68e | 2006-10-11 19:10:47 -0400 | [diff] [blame] | 902 | net_cat_size = doi_def->map.std->cat.cipso_size; |
| 903 | net_cat_array = doi_def->map.std->cat.cipso; |
Paul Moore | 0275276 | 2006-11-29 13:18:18 -0500 | [diff] [blame] | 904 | } |
Paul Moore | 446fda4 | 2006-08-03 16:48:06 -0700 | [diff] [blame] | 905 | |
Paul Moore | 0275276 | 2006-11-29 13:18:18 -0500 | [diff] [blame] | 906 | for (;;) { |
| 907 | net_spot = cipso_v4_bitmap_walk(net_cat, |
| 908 | net_clen_bits, |
| 909 | net_spot + 1, |
| 910 | 1); |
| 911 | if (net_spot < 0) { |
| 912 | if (net_spot == -2) |
| 913 | return -EFAULT; |
| 914 | return 0; |
| 915 | } |
| 916 | |
| 917 | switch (doi_def->type) { |
| 918 | case CIPSO_V4_MAP_PASS: |
| 919 | host_spot = net_spot; |
| 920 | break; |
Paul Moore | 15c45f7 | 2008-10-10 10:16:34 -0400 | [diff] [blame] | 921 | case CIPSO_V4_MAP_TRANS: |
Paul Moore | 0275276 | 2006-11-29 13:18:18 -0500 | [diff] [blame] | 922 | if (net_spot >= net_cat_size) |
| 923 | return -EPERM; |
Paul Moore | 446fda4 | 2006-08-03 16:48:06 -0700 | [diff] [blame] | 924 | host_spot = net_cat_array[net_spot]; |
Paul Moore | 9fade4b | 2006-11-17 17:38:50 -0500 | [diff] [blame] | 925 | if (host_spot >= CIPSO_V4_INV_CAT) |
| 926 | return -EPERM; |
Paul Moore | 0275276 | 2006-11-29 13:18:18 -0500 | [diff] [blame] | 927 | break; |
Paul Moore | 446fda4 | 2006-08-03 16:48:06 -0700 | [diff] [blame] | 928 | } |
Paul Moore | 16efd45 | 2008-01-29 08:37:59 -0500 | [diff] [blame] | 929 | ret_val = netlbl_secattr_catmap_setbit(secattr->attr.mls.cat, |
Paul Moore | 0275276 | 2006-11-29 13:18:18 -0500 | [diff] [blame] | 930 | host_spot, |
| 931 | GFP_ATOMIC); |
| 932 | if (ret_val != 0) |
| 933 | return ret_val; |
Paul Moore | 446fda4 | 2006-08-03 16:48:06 -0700 | [diff] [blame] | 934 | } |
| 935 | |
| 936 | return -EINVAL; |
| 937 | } |
| 938 | |
Paul Moore | 654bbc2 | 2006-11-29 13:18:19 -0500 | [diff] [blame] | 939 | /** |
| 940 | * cipso_v4_map_cat_enum_valid - Checks to see if the categories are valid |
| 941 | * @doi_def: the DOI definition |
| 942 | * @enumcat: category list |
| 943 | * @enumcat_len: length of the category list in bytes |
| 944 | * |
| 945 | * Description: |
| 946 | * Checks the given categories against the given DOI definition and returns a |
| 947 | * negative value if any of the categories do not have a valid mapping and a |
| 948 | * zero value if all of the categories are valid. |
| 949 | * |
| 950 | */ |
| 951 | static int cipso_v4_map_cat_enum_valid(const struct cipso_v4_doi *doi_def, |
| 952 | const unsigned char *enumcat, |
| 953 | u32 enumcat_len) |
| 954 | { |
| 955 | u16 cat; |
| 956 | int cat_prev = -1; |
| 957 | u32 iter; |
| 958 | |
| 959 | if (doi_def->type != CIPSO_V4_MAP_PASS || enumcat_len & 0x01) |
| 960 | return -EFAULT; |
| 961 | |
| 962 | for (iter = 0; iter < enumcat_len; iter += 2) { |
Harvey Harrison | d3e2ce3 | 2008-05-02 16:26:16 -0700 | [diff] [blame] | 963 | cat = get_unaligned_be16(&enumcat[iter]); |
Paul Moore | 654bbc2 | 2006-11-29 13:18:19 -0500 | [diff] [blame] | 964 | if (cat <= cat_prev) |
| 965 | return -EFAULT; |
| 966 | cat_prev = cat; |
| 967 | } |
| 968 | |
| 969 | return 0; |
| 970 | } |
| 971 | |
| 972 | /** |
| 973 | * cipso_v4_map_cat_enum_hton - Perform a category mapping from host to network |
| 974 | * @doi_def: the DOI definition |
| 975 | * @secattr: the security attributes |
| 976 | * @net_cat: the zero'd out category list in network/CIPSO format |
| 977 | * @net_cat_len: the length of the CIPSO category list in bytes |
| 978 | * |
| 979 | * Description: |
| 980 | * Perform a label mapping to translate a local MLS category bitmap to the |
| 981 | * correct CIPSO category list using the given DOI definition. Returns the |
| 982 | * size in bytes of the network category bitmap on success, negative values |
| 983 | * otherwise. |
| 984 | * |
| 985 | */ |
| 986 | static int cipso_v4_map_cat_enum_hton(const struct cipso_v4_doi *doi_def, |
| 987 | const struct netlbl_lsm_secattr *secattr, |
| 988 | unsigned char *net_cat, |
| 989 | u32 net_cat_len) |
| 990 | { |
| 991 | int cat = -1; |
| 992 | u32 cat_iter = 0; |
| 993 | |
| 994 | for (;;) { |
Paul Moore | 16efd45 | 2008-01-29 08:37:59 -0500 | [diff] [blame] | 995 | cat = netlbl_secattr_catmap_walk(secattr->attr.mls.cat, |
| 996 | cat + 1); |
Paul Moore | 654bbc2 | 2006-11-29 13:18:19 -0500 | [diff] [blame] | 997 | if (cat < 0) |
| 998 | break; |
| 999 | if ((cat_iter + 2) > net_cat_len) |
| 1000 | return -ENOSPC; |
| 1001 | |
| 1002 | *((__be16 *)&net_cat[cat_iter]) = htons(cat); |
| 1003 | cat_iter += 2; |
| 1004 | } |
| 1005 | |
| 1006 | return cat_iter; |
| 1007 | } |
| 1008 | |
| 1009 | /** |
| 1010 | * cipso_v4_map_cat_enum_ntoh - Perform a category mapping from network to host |
| 1011 | * @doi_def: the DOI definition |
| 1012 | * @net_cat: the category list in network/CIPSO format |
| 1013 | * @net_cat_len: the length of the CIPSO bitmap in bytes |
| 1014 | * @secattr: the security attributes |
| 1015 | * |
| 1016 | * Description: |
| 1017 | * Perform a label mapping to translate a CIPSO category list to the correct |
| 1018 | * local MLS category bitmap using the given DOI definition. Returns zero on |
| 1019 | * success, negative values on failure. |
| 1020 | * |
| 1021 | */ |
| 1022 | static int cipso_v4_map_cat_enum_ntoh(const struct cipso_v4_doi *doi_def, |
| 1023 | const unsigned char *net_cat, |
| 1024 | u32 net_cat_len, |
| 1025 | struct netlbl_lsm_secattr *secattr) |
| 1026 | { |
| 1027 | int ret_val; |
| 1028 | u32 iter; |
| 1029 | |
| 1030 | for (iter = 0; iter < net_cat_len; iter += 2) { |
Paul Moore | 16efd45 | 2008-01-29 08:37:59 -0500 | [diff] [blame] | 1031 | ret_val = netlbl_secattr_catmap_setbit(secattr->attr.mls.cat, |
Harvey Harrison | d3e2ce3 | 2008-05-02 16:26:16 -0700 | [diff] [blame] | 1032 | get_unaligned_be16(&net_cat[iter]), |
Paul Moore | 50e5d35 | 2007-06-07 18:38:14 -0700 | [diff] [blame] | 1033 | GFP_ATOMIC); |
Paul Moore | 654bbc2 | 2006-11-29 13:18:19 -0500 | [diff] [blame] | 1034 | if (ret_val != 0) |
| 1035 | return ret_val; |
| 1036 | } |
| 1037 | |
| 1038 | return 0; |
| 1039 | } |
| 1040 | |
Paul Moore | 484b366 | 2006-11-29 13:18:20 -0500 | [diff] [blame] | 1041 | /** |
| 1042 | * cipso_v4_map_cat_rng_valid - Checks to see if the categories are valid |
| 1043 | * @doi_def: the DOI definition |
| 1044 | * @rngcat: category list |
| 1045 | * @rngcat_len: length of the category list in bytes |
| 1046 | * |
| 1047 | * Description: |
| 1048 | * Checks the given categories against the given DOI definition and returns a |
| 1049 | * negative value if any of the categories do not have a valid mapping and a |
| 1050 | * zero value if all of the categories are valid. |
| 1051 | * |
| 1052 | */ |
| 1053 | static int cipso_v4_map_cat_rng_valid(const struct cipso_v4_doi *doi_def, |
| 1054 | const unsigned char *rngcat, |
| 1055 | u32 rngcat_len) |
| 1056 | { |
| 1057 | u16 cat_high; |
| 1058 | u16 cat_low; |
| 1059 | u32 cat_prev = CIPSO_V4_MAX_REM_CATS + 1; |
| 1060 | u32 iter; |
| 1061 | |
| 1062 | if (doi_def->type != CIPSO_V4_MAP_PASS || rngcat_len & 0x01) |
| 1063 | return -EFAULT; |
| 1064 | |
| 1065 | for (iter = 0; iter < rngcat_len; iter += 4) { |
Harvey Harrison | d3e2ce3 | 2008-05-02 16:26:16 -0700 | [diff] [blame] | 1066 | cat_high = get_unaligned_be16(&rngcat[iter]); |
Paul Moore | 484b366 | 2006-11-29 13:18:20 -0500 | [diff] [blame] | 1067 | if ((iter + 4) <= rngcat_len) |
Harvey Harrison | d3e2ce3 | 2008-05-02 16:26:16 -0700 | [diff] [blame] | 1068 | cat_low = get_unaligned_be16(&rngcat[iter + 2]); |
Paul Moore | 484b366 | 2006-11-29 13:18:20 -0500 | [diff] [blame] | 1069 | else |
| 1070 | cat_low = 0; |
| 1071 | |
| 1072 | if (cat_high > cat_prev) |
| 1073 | return -EFAULT; |
| 1074 | |
| 1075 | cat_prev = cat_low; |
| 1076 | } |
| 1077 | |
| 1078 | return 0; |
| 1079 | } |
| 1080 | |
| 1081 | /** |
| 1082 | * cipso_v4_map_cat_rng_hton - Perform a category mapping from host to network |
| 1083 | * @doi_def: the DOI definition |
| 1084 | * @secattr: the security attributes |
| 1085 | * @net_cat: the zero'd out category list in network/CIPSO format |
| 1086 | * @net_cat_len: the length of the CIPSO category list in bytes |
| 1087 | * |
| 1088 | * Description: |
| 1089 | * Perform a label mapping to translate a local MLS category bitmap to the |
| 1090 | * correct CIPSO category list using the given DOI definition. Returns the |
| 1091 | * size in bytes of the network category bitmap on success, negative values |
| 1092 | * otherwise. |
| 1093 | * |
| 1094 | */ |
| 1095 | static int cipso_v4_map_cat_rng_hton(const struct cipso_v4_doi *doi_def, |
| 1096 | const struct netlbl_lsm_secattr *secattr, |
| 1097 | unsigned char *net_cat, |
| 1098 | u32 net_cat_len) |
| 1099 | { |
Paul Moore | 484b366 | 2006-11-29 13:18:20 -0500 | [diff] [blame] | 1100 | int iter = -1; |
Paul Moore | f998e8c | 2007-02-28 15:14:20 -0500 | [diff] [blame] | 1101 | u16 array[CIPSO_V4_TAG_RNG_CAT_MAX * 2]; |
Paul Moore | 484b366 | 2006-11-29 13:18:20 -0500 | [diff] [blame] | 1102 | u32 array_cnt = 0; |
| 1103 | u32 cat_size = 0; |
| 1104 | |
Paul Moore | f998e8c | 2007-02-28 15:14:20 -0500 | [diff] [blame] | 1105 | /* make sure we don't overflow the 'array[]' variable */ |
Paul Moore | 128c6b6 | 2007-02-28 15:14:21 -0500 | [diff] [blame] | 1106 | if (net_cat_len > |
| 1107 | (CIPSO_V4_OPT_LEN_MAX - CIPSO_V4_HDR_LEN - CIPSO_V4_TAG_RNG_BLEN)) |
| 1108 | return -ENOSPC; |
Paul Moore | 484b366 | 2006-11-29 13:18:20 -0500 | [diff] [blame] | 1109 | |
| 1110 | for (;;) { |
Paul Moore | 16efd45 | 2008-01-29 08:37:59 -0500 | [diff] [blame] | 1111 | iter = netlbl_secattr_catmap_walk(secattr->attr.mls.cat, |
| 1112 | iter + 1); |
Paul Moore | 484b366 | 2006-11-29 13:18:20 -0500 | [diff] [blame] | 1113 | if (iter < 0) |
| 1114 | break; |
| 1115 | cat_size += (iter == 0 ? 0 : sizeof(u16)); |
| 1116 | if (cat_size > net_cat_len) |
| 1117 | return -ENOSPC; |
| 1118 | array[array_cnt++] = iter; |
| 1119 | |
Paul Moore | 16efd45 | 2008-01-29 08:37:59 -0500 | [diff] [blame] | 1120 | iter = netlbl_secattr_catmap_walk_rng(secattr->attr.mls.cat, |
| 1121 | iter); |
Paul Moore | 484b366 | 2006-11-29 13:18:20 -0500 | [diff] [blame] | 1122 | if (iter < 0) |
| 1123 | return -EFAULT; |
| 1124 | cat_size += sizeof(u16); |
| 1125 | if (cat_size > net_cat_len) |
| 1126 | return -ENOSPC; |
| 1127 | array[array_cnt++] = iter; |
| 1128 | } |
| 1129 | |
| 1130 | for (iter = 0; array_cnt > 0;) { |
| 1131 | *((__be16 *)&net_cat[iter]) = htons(array[--array_cnt]); |
| 1132 | iter += 2; |
| 1133 | array_cnt--; |
| 1134 | if (array[array_cnt] != 0) { |
| 1135 | *((__be16 *)&net_cat[iter]) = htons(array[array_cnt]); |
| 1136 | iter += 2; |
| 1137 | } |
| 1138 | } |
| 1139 | |
| 1140 | return cat_size; |
| 1141 | } |
| 1142 | |
| 1143 | /** |
| 1144 | * cipso_v4_map_cat_rng_ntoh - Perform a category mapping from network to host |
| 1145 | * @doi_def: the DOI definition |
| 1146 | * @net_cat: the category list in network/CIPSO format |
| 1147 | * @net_cat_len: the length of the CIPSO bitmap in bytes |
| 1148 | * @secattr: the security attributes |
| 1149 | * |
| 1150 | * Description: |
| 1151 | * Perform a label mapping to translate a CIPSO category list to the correct |
| 1152 | * local MLS category bitmap using the given DOI definition. Returns zero on |
| 1153 | * success, negative values on failure. |
| 1154 | * |
| 1155 | */ |
| 1156 | static int cipso_v4_map_cat_rng_ntoh(const struct cipso_v4_doi *doi_def, |
| 1157 | const unsigned char *net_cat, |
| 1158 | u32 net_cat_len, |
| 1159 | struct netlbl_lsm_secattr *secattr) |
| 1160 | { |
| 1161 | int ret_val; |
| 1162 | u32 net_iter; |
| 1163 | u16 cat_low; |
| 1164 | u16 cat_high; |
| 1165 | |
Stephen Hemminger | 132adf5 | 2007-03-08 20:44:43 -0800 | [diff] [blame] | 1166 | for (net_iter = 0; net_iter < net_cat_len; net_iter += 4) { |
Harvey Harrison | d3e2ce3 | 2008-05-02 16:26:16 -0700 | [diff] [blame] | 1167 | cat_high = get_unaligned_be16(&net_cat[net_iter]); |
Paul Moore | 484b366 | 2006-11-29 13:18:20 -0500 | [diff] [blame] | 1168 | if ((net_iter + 4) <= net_cat_len) |
Harvey Harrison | d3e2ce3 | 2008-05-02 16:26:16 -0700 | [diff] [blame] | 1169 | cat_low = get_unaligned_be16(&net_cat[net_iter + 2]); |
Paul Moore | 484b366 | 2006-11-29 13:18:20 -0500 | [diff] [blame] | 1170 | else |
| 1171 | cat_low = 0; |
| 1172 | |
Paul Moore | 16efd45 | 2008-01-29 08:37:59 -0500 | [diff] [blame] | 1173 | ret_val = netlbl_secattr_catmap_setrng(secattr->attr.mls.cat, |
Paul Moore | 484b366 | 2006-11-29 13:18:20 -0500 | [diff] [blame] | 1174 | cat_low, |
| 1175 | cat_high, |
| 1176 | GFP_ATOMIC); |
| 1177 | if (ret_val != 0) |
| 1178 | return ret_val; |
| 1179 | } |
| 1180 | |
| 1181 | return 0; |
| 1182 | } |
| 1183 | |
Paul Moore | 446fda4 | 2006-08-03 16:48:06 -0700 | [diff] [blame] | 1184 | /* |
| 1185 | * Protocol Handling Functions |
| 1186 | */ |
| 1187 | |
Paul Moore | 446fda4 | 2006-08-03 16:48:06 -0700 | [diff] [blame] | 1188 | /** |
| 1189 | * cipso_v4_gentag_hdr - Generate a CIPSO option header |
| 1190 | * @doi_def: the DOI definition |
Paul Moore | 91b1ed0 | 2006-11-17 17:38:49 -0500 | [diff] [blame] | 1191 | * @len: the total tag length in bytes, not including this header |
Paul Moore | 446fda4 | 2006-08-03 16:48:06 -0700 | [diff] [blame] | 1192 | * @buf: the CIPSO option buffer |
| 1193 | * |
| 1194 | * Description: |
Paul Moore | 91b1ed0 | 2006-11-17 17:38:49 -0500 | [diff] [blame] | 1195 | * Write a CIPSO header into the beginning of @buffer. |
Paul Moore | 446fda4 | 2006-08-03 16:48:06 -0700 | [diff] [blame] | 1196 | * |
| 1197 | */ |
Paul Moore | 91b1ed0 | 2006-11-17 17:38:49 -0500 | [diff] [blame] | 1198 | static void cipso_v4_gentag_hdr(const struct cipso_v4_doi *doi_def, |
| 1199 | unsigned char *buf, |
| 1200 | u32 len) |
Paul Moore | 446fda4 | 2006-08-03 16:48:06 -0700 | [diff] [blame] | 1201 | { |
Paul Moore | 446fda4 | 2006-08-03 16:48:06 -0700 | [diff] [blame] | 1202 | buf[0] = IPOPT_CIPSO; |
| 1203 | buf[1] = CIPSO_V4_HDR_LEN + len; |
Al Viro | 714e85b | 2006-11-14 20:51:49 -0800 | [diff] [blame] | 1204 | *(__be32 *)&buf[2] = htonl(doi_def->doi); |
Paul Moore | 446fda4 | 2006-08-03 16:48:06 -0700 | [diff] [blame] | 1205 | } |
| 1206 | |
Paul Moore | 446fda4 | 2006-08-03 16:48:06 -0700 | [diff] [blame] | 1207 | /** |
| 1208 | * cipso_v4_gentag_rbm - Generate a CIPSO restricted bitmap tag (type #1) |
| 1209 | * @doi_def: the DOI definition |
| 1210 | * @secattr: the security attributes |
| 1211 | * @buffer: the option buffer |
| 1212 | * @buffer_len: length of buffer in bytes |
| 1213 | * |
| 1214 | * Description: |
| 1215 | * Generate a CIPSO option using the restricted bitmap tag, tag type #1. The |
| 1216 | * actual buffer length may be larger than the indicated size due to |
Paul Moore | 91b1ed0 | 2006-11-17 17:38:49 -0500 | [diff] [blame] | 1217 | * translation between host and network category bitmaps. Returns the size of |
| 1218 | * the tag on success, negative values on failure. |
Paul Moore | 446fda4 | 2006-08-03 16:48:06 -0700 | [diff] [blame] | 1219 | * |
| 1220 | */ |
| 1221 | static int cipso_v4_gentag_rbm(const struct cipso_v4_doi *doi_def, |
| 1222 | const struct netlbl_lsm_secattr *secattr, |
Paul Moore | 91b1ed0 | 2006-11-17 17:38:49 -0500 | [diff] [blame] | 1223 | unsigned char *buffer, |
| 1224 | u32 buffer_len) |
Paul Moore | 446fda4 | 2006-08-03 16:48:06 -0700 | [diff] [blame] | 1225 | { |
Paul Moore | 701a90b | 2006-11-17 17:38:46 -0500 | [diff] [blame] | 1226 | int ret_val; |
Paul Moore | 91b1ed0 | 2006-11-17 17:38:49 -0500 | [diff] [blame] | 1227 | u32 tag_len; |
Paul Moore | 446fda4 | 2006-08-03 16:48:06 -0700 | [diff] [blame] | 1228 | u32 level; |
| 1229 | |
Paul Moore | 701a90b | 2006-11-17 17:38:46 -0500 | [diff] [blame] | 1230 | if ((secattr->flags & NETLBL_SECATTR_MLS_LVL) == 0) |
| 1231 | return -EPERM; |
| 1232 | |
Paul Moore | 16efd45 | 2008-01-29 08:37:59 -0500 | [diff] [blame] | 1233 | ret_val = cipso_v4_map_lvl_hton(doi_def, |
| 1234 | secattr->attr.mls.lvl, |
| 1235 | &level); |
Paul Moore | 91b1ed0 | 2006-11-17 17:38:49 -0500 | [diff] [blame] | 1236 | if (ret_val != 0) |
| 1237 | return ret_val; |
Paul Moore | 446fda4 | 2006-08-03 16:48:06 -0700 | [diff] [blame] | 1238 | |
Paul Moore | 91b1ed0 | 2006-11-17 17:38:49 -0500 | [diff] [blame] | 1239 | if (secattr->flags & NETLBL_SECATTR_MLS_CAT) { |
Paul Moore | 446fda4 | 2006-08-03 16:48:06 -0700 | [diff] [blame] | 1240 | ret_val = cipso_v4_map_cat_rbm_hton(doi_def, |
Paul Moore | 0275276 | 2006-11-29 13:18:18 -0500 | [diff] [blame] | 1241 | secattr, |
Paul Moore | 91b1ed0 | 2006-11-17 17:38:49 -0500 | [diff] [blame] | 1242 | &buffer[4], |
| 1243 | buffer_len - 4); |
Paul Moore | 446fda4 | 2006-08-03 16:48:06 -0700 | [diff] [blame] | 1244 | if (ret_val < 0) |
Paul Moore | 91b1ed0 | 2006-11-17 17:38:49 -0500 | [diff] [blame] | 1245 | return ret_val; |
Paul Moore | 446fda4 | 2006-08-03 16:48:06 -0700 | [diff] [blame] | 1246 | |
| 1247 | /* This will send packets using the "optimized" format when |
| 1248 | * possibile as specified in section 3.4.2.6 of the |
| 1249 | * CIPSO draft. */ |
Paul Moore | 701a90b | 2006-11-17 17:38:46 -0500 | [diff] [blame] | 1250 | if (cipso_v4_rbm_optfmt && ret_val > 0 && ret_val <= 10) |
Paul Moore | 91b1ed0 | 2006-11-17 17:38:49 -0500 | [diff] [blame] | 1251 | tag_len = 14; |
Paul Moore | 701a90b | 2006-11-17 17:38:46 -0500 | [diff] [blame] | 1252 | else |
Paul Moore | 91b1ed0 | 2006-11-17 17:38:49 -0500 | [diff] [blame] | 1253 | tag_len = 4 + ret_val; |
| 1254 | } else |
| 1255 | tag_len = 4; |
Paul Moore | 446fda4 | 2006-08-03 16:48:06 -0700 | [diff] [blame] | 1256 | |
Paul Moore | 15c45f7 | 2008-10-10 10:16:34 -0400 | [diff] [blame] | 1257 | buffer[0] = CIPSO_V4_TAG_RBITMAP; |
Paul Moore | 91b1ed0 | 2006-11-17 17:38:49 -0500 | [diff] [blame] | 1258 | buffer[1] = tag_len; |
| 1259 | buffer[3] = level; |
Paul Moore | 446fda4 | 2006-08-03 16:48:06 -0700 | [diff] [blame] | 1260 | |
Paul Moore | 91b1ed0 | 2006-11-17 17:38:49 -0500 | [diff] [blame] | 1261 | return tag_len; |
Paul Moore | 446fda4 | 2006-08-03 16:48:06 -0700 | [diff] [blame] | 1262 | } |
| 1263 | |
| 1264 | /** |
| 1265 | * cipso_v4_parsetag_rbm - Parse a CIPSO restricted bitmap tag |
| 1266 | * @doi_def: the DOI definition |
| 1267 | * @tag: the CIPSO tag |
| 1268 | * @secattr: the security attributes |
| 1269 | * |
| 1270 | * Description: |
| 1271 | * Parse a CIPSO restricted bitmap tag (tag type #1) and return the security |
| 1272 | * attributes in @secattr. Return zero on success, negatives values on |
| 1273 | * failure. |
| 1274 | * |
| 1275 | */ |
| 1276 | static int cipso_v4_parsetag_rbm(const struct cipso_v4_doi *doi_def, |
| 1277 | const unsigned char *tag, |
| 1278 | struct netlbl_lsm_secattr *secattr) |
| 1279 | { |
| 1280 | int ret_val; |
| 1281 | u8 tag_len = tag[1]; |
| 1282 | u32 level; |
| 1283 | |
| 1284 | ret_val = cipso_v4_map_lvl_ntoh(doi_def, tag[3], &level); |
| 1285 | if (ret_val != 0) |
| 1286 | return ret_val; |
Paul Moore | 16efd45 | 2008-01-29 08:37:59 -0500 | [diff] [blame] | 1287 | secattr->attr.mls.lvl = level; |
Paul Moore | 701a90b | 2006-11-17 17:38:46 -0500 | [diff] [blame] | 1288 | secattr->flags |= NETLBL_SECATTR_MLS_LVL; |
Paul Moore | 446fda4 | 2006-08-03 16:48:06 -0700 | [diff] [blame] | 1289 | |
| 1290 | if (tag_len > 4) { |
Paul Moore | 16efd45 | 2008-01-29 08:37:59 -0500 | [diff] [blame] | 1291 | secattr->attr.mls.cat = |
| 1292 | netlbl_secattr_catmap_alloc(GFP_ATOMIC); |
| 1293 | if (secattr->attr.mls.cat == NULL) |
Paul Moore | 446fda4 | 2006-08-03 16:48:06 -0700 | [diff] [blame] | 1294 | return -ENOMEM; |
| 1295 | |
| 1296 | ret_val = cipso_v4_map_cat_rbm_ntoh(doi_def, |
| 1297 | &tag[4], |
| 1298 | tag_len - 4, |
Paul Moore | 0275276 | 2006-11-29 13:18:18 -0500 | [diff] [blame] | 1299 | secattr); |
| 1300 | if (ret_val != 0) { |
Paul Moore | 16efd45 | 2008-01-29 08:37:59 -0500 | [diff] [blame] | 1301 | netlbl_secattr_catmap_free(secattr->attr.mls.cat); |
Paul Moore | 446fda4 | 2006-08-03 16:48:06 -0700 | [diff] [blame] | 1302 | return ret_val; |
| 1303 | } |
Paul Moore | 0275276 | 2006-11-29 13:18:18 -0500 | [diff] [blame] | 1304 | |
| 1305 | secattr->flags |= NETLBL_SECATTR_MLS_CAT; |
Paul Moore | 446fda4 | 2006-08-03 16:48:06 -0700 | [diff] [blame] | 1306 | } |
| 1307 | |
| 1308 | return 0; |
| 1309 | } |
| 1310 | |
| 1311 | /** |
Paul Moore | 654bbc2 | 2006-11-29 13:18:19 -0500 | [diff] [blame] | 1312 | * cipso_v4_gentag_enum - Generate a CIPSO enumerated tag (type #2) |
| 1313 | * @doi_def: the DOI definition |
| 1314 | * @secattr: the security attributes |
| 1315 | * @buffer: the option buffer |
| 1316 | * @buffer_len: length of buffer in bytes |
| 1317 | * |
| 1318 | * Description: |
| 1319 | * Generate a CIPSO option using the enumerated tag, tag type #2. Returns the |
| 1320 | * size of the tag on success, negative values on failure. |
| 1321 | * |
| 1322 | */ |
| 1323 | static int cipso_v4_gentag_enum(const struct cipso_v4_doi *doi_def, |
| 1324 | const struct netlbl_lsm_secattr *secattr, |
| 1325 | unsigned char *buffer, |
| 1326 | u32 buffer_len) |
| 1327 | { |
| 1328 | int ret_val; |
| 1329 | u32 tag_len; |
| 1330 | u32 level; |
| 1331 | |
| 1332 | if (!(secattr->flags & NETLBL_SECATTR_MLS_LVL)) |
| 1333 | return -EPERM; |
| 1334 | |
Paul Moore | 16efd45 | 2008-01-29 08:37:59 -0500 | [diff] [blame] | 1335 | ret_val = cipso_v4_map_lvl_hton(doi_def, |
| 1336 | secattr->attr.mls.lvl, |
| 1337 | &level); |
Paul Moore | 654bbc2 | 2006-11-29 13:18:19 -0500 | [diff] [blame] | 1338 | if (ret_val != 0) |
| 1339 | return ret_val; |
| 1340 | |
| 1341 | if (secattr->flags & NETLBL_SECATTR_MLS_CAT) { |
| 1342 | ret_val = cipso_v4_map_cat_enum_hton(doi_def, |
| 1343 | secattr, |
| 1344 | &buffer[4], |
| 1345 | buffer_len - 4); |
| 1346 | if (ret_val < 0) |
| 1347 | return ret_val; |
| 1348 | |
| 1349 | tag_len = 4 + ret_val; |
| 1350 | } else |
| 1351 | tag_len = 4; |
| 1352 | |
Paul Moore | 15c45f7 | 2008-10-10 10:16:34 -0400 | [diff] [blame] | 1353 | buffer[0] = CIPSO_V4_TAG_ENUM; |
Paul Moore | 654bbc2 | 2006-11-29 13:18:19 -0500 | [diff] [blame] | 1354 | buffer[1] = tag_len; |
| 1355 | buffer[3] = level; |
| 1356 | |
| 1357 | return tag_len; |
| 1358 | } |
| 1359 | |
| 1360 | /** |
| 1361 | * cipso_v4_parsetag_enum - Parse a CIPSO enumerated tag |
| 1362 | * @doi_def: the DOI definition |
| 1363 | * @tag: the CIPSO tag |
| 1364 | * @secattr: the security attributes |
| 1365 | * |
| 1366 | * Description: |
| 1367 | * Parse a CIPSO enumerated tag (tag type #2) and return the security |
| 1368 | * attributes in @secattr. Return zero on success, negatives values on |
| 1369 | * failure. |
| 1370 | * |
| 1371 | */ |
| 1372 | static int cipso_v4_parsetag_enum(const struct cipso_v4_doi *doi_def, |
| 1373 | const unsigned char *tag, |
| 1374 | struct netlbl_lsm_secattr *secattr) |
| 1375 | { |
| 1376 | int ret_val; |
| 1377 | u8 tag_len = tag[1]; |
| 1378 | u32 level; |
| 1379 | |
| 1380 | ret_val = cipso_v4_map_lvl_ntoh(doi_def, tag[3], &level); |
| 1381 | if (ret_val != 0) |
| 1382 | return ret_val; |
Paul Moore | 16efd45 | 2008-01-29 08:37:59 -0500 | [diff] [blame] | 1383 | secattr->attr.mls.lvl = level; |
Paul Moore | 654bbc2 | 2006-11-29 13:18:19 -0500 | [diff] [blame] | 1384 | secattr->flags |= NETLBL_SECATTR_MLS_LVL; |
| 1385 | |
| 1386 | if (tag_len > 4) { |
Paul Moore | 16efd45 | 2008-01-29 08:37:59 -0500 | [diff] [blame] | 1387 | secattr->attr.mls.cat = |
| 1388 | netlbl_secattr_catmap_alloc(GFP_ATOMIC); |
| 1389 | if (secattr->attr.mls.cat == NULL) |
Paul Moore | 654bbc2 | 2006-11-29 13:18:19 -0500 | [diff] [blame] | 1390 | return -ENOMEM; |
| 1391 | |
| 1392 | ret_val = cipso_v4_map_cat_enum_ntoh(doi_def, |
| 1393 | &tag[4], |
| 1394 | tag_len - 4, |
| 1395 | secattr); |
| 1396 | if (ret_val != 0) { |
Paul Moore | 16efd45 | 2008-01-29 08:37:59 -0500 | [diff] [blame] | 1397 | netlbl_secattr_catmap_free(secattr->attr.mls.cat); |
Paul Moore | 654bbc2 | 2006-11-29 13:18:19 -0500 | [diff] [blame] | 1398 | return ret_val; |
| 1399 | } |
| 1400 | |
| 1401 | secattr->flags |= NETLBL_SECATTR_MLS_CAT; |
| 1402 | } |
| 1403 | |
| 1404 | return 0; |
| 1405 | } |
| 1406 | |
| 1407 | /** |
Paul Moore | 484b366 | 2006-11-29 13:18:20 -0500 | [diff] [blame] | 1408 | * cipso_v4_gentag_rng - Generate a CIPSO ranged tag (type #5) |
| 1409 | * @doi_def: the DOI definition |
| 1410 | * @secattr: the security attributes |
| 1411 | * @buffer: the option buffer |
| 1412 | * @buffer_len: length of buffer in bytes |
| 1413 | * |
| 1414 | * Description: |
| 1415 | * Generate a CIPSO option using the ranged tag, tag type #5. Returns the |
| 1416 | * size of the tag on success, negative values on failure. |
| 1417 | * |
| 1418 | */ |
| 1419 | static int cipso_v4_gentag_rng(const struct cipso_v4_doi *doi_def, |
| 1420 | const struct netlbl_lsm_secattr *secattr, |
| 1421 | unsigned char *buffer, |
| 1422 | u32 buffer_len) |
| 1423 | { |
| 1424 | int ret_val; |
| 1425 | u32 tag_len; |
| 1426 | u32 level; |
| 1427 | |
| 1428 | if (!(secattr->flags & NETLBL_SECATTR_MLS_LVL)) |
| 1429 | return -EPERM; |
| 1430 | |
Paul Moore | 16efd45 | 2008-01-29 08:37:59 -0500 | [diff] [blame] | 1431 | ret_val = cipso_v4_map_lvl_hton(doi_def, |
| 1432 | secattr->attr.mls.lvl, |
| 1433 | &level); |
Paul Moore | 484b366 | 2006-11-29 13:18:20 -0500 | [diff] [blame] | 1434 | if (ret_val != 0) |
| 1435 | return ret_val; |
| 1436 | |
| 1437 | if (secattr->flags & NETLBL_SECATTR_MLS_CAT) { |
| 1438 | ret_val = cipso_v4_map_cat_rng_hton(doi_def, |
| 1439 | secattr, |
| 1440 | &buffer[4], |
| 1441 | buffer_len - 4); |
| 1442 | if (ret_val < 0) |
| 1443 | return ret_val; |
| 1444 | |
| 1445 | tag_len = 4 + ret_val; |
| 1446 | } else |
| 1447 | tag_len = 4; |
| 1448 | |
Paul Moore | 15c45f7 | 2008-10-10 10:16:34 -0400 | [diff] [blame] | 1449 | buffer[0] = CIPSO_V4_TAG_RANGE; |
Paul Moore | 484b366 | 2006-11-29 13:18:20 -0500 | [diff] [blame] | 1450 | buffer[1] = tag_len; |
| 1451 | buffer[3] = level; |
| 1452 | |
| 1453 | return tag_len; |
| 1454 | } |
| 1455 | |
| 1456 | /** |
| 1457 | * cipso_v4_parsetag_rng - Parse a CIPSO ranged tag |
| 1458 | * @doi_def: the DOI definition |
| 1459 | * @tag: the CIPSO tag |
| 1460 | * @secattr: the security attributes |
| 1461 | * |
| 1462 | * Description: |
| 1463 | * Parse a CIPSO ranged tag (tag type #5) and return the security attributes |
| 1464 | * in @secattr. Return zero on success, negatives values on failure. |
| 1465 | * |
| 1466 | */ |
| 1467 | static int cipso_v4_parsetag_rng(const struct cipso_v4_doi *doi_def, |
| 1468 | const unsigned char *tag, |
| 1469 | struct netlbl_lsm_secattr *secattr) |
| 1470 | { |
| 1471 | int ret_val; |
| 1472 | u8 tag_len = tag[1]; |
| 1473 | u32 level; |
| 1474 | |
| 1475 | ret_val = cipso_v4_map_lvl_ntoh(doi_def, tag[3], &level); |
| 1476 | if (ret_val != 0) |
| 1477 | return ret_val; |
Paul Moore | 16efd45 | 2008-01-29 08:37:59 -0500 | [diff] [blame] | 1478 | secattr->attr.mls.lvl = level; |
Paul Moore | 484b366 | 2006-11-29 13:18:20 -0500 | [diff] [blame] | 1479 | secattr->flags |= NETLBL_SECATTR_MLS_LVL; |
| 1480 | |
| 1481 | if (tag_len > 4) { |
Paul Moore | 16efd45 | 2008-01-29 08:37:59 -0500 | [diff] [blame] | 1482 | secattr->attr.mls.cat = |
| 1483 | netlbl_secattr_catmap_alloc(GFP_ATOMIC); |
| 1484 | if (secattr->attr.mls.cat == NULL) |
Paul Moore | 484b366 | 2006-11-29 13:18:20 -0500 | [diff] [blame] | 1485 | return -ENOMEM; |
| 1486 | |
| 1487 | ret_val = cipso_v4_map_cat_rng_ntoh(doi_def, |
| 1488 | &tag[4], |
| 1489 | tag_len - 4, |
| 1490 | secattr); |
| 1491 | if (ret_val != 0) { |
Paul Moore | 16efd45 | 2008-01-29 08:37:59 -0500 | [diff] [blame] | 1492 | netlbl_secattr_catmap_free(secattr->attr.mls.cat); |
Paul Moore | 484b366 | 2006-11-29 13:18:20 -0500 | [diff] [blame] | 1493 | return ret_val; |
| 1494 | } |
| 1495 | |
| 1496 | secattr->flags |= NETLBL_SECATTR_MLS_CAT; |
| 1497 | } |
| 1498 | |
| 1499 | return 0; |
| 1500 | } |
| 1501 | |
| 1502 | /** |
Paul Moore | 15c45f7 | 2008-10-10 10:16:34 -0400 | [diff] [blame] | 1503 | * cipso_v4_gentag_loc - Generate a CIPSO local tag (non-standard) |
| 1504 | * @doi_def: the DOI definition |
| 1505 | * @secattr: the security attributes |
| 1506 | * @buffer: the option buffer |
| 1507 | * @buffer_len: length of buffer in bytes |
| 1508 | * |
| 1509 | * Description: |
| 1510 | * Generate a CIPSO option using the local tag. Returns the size of the tag |
| 1511 | * on success, negative values on failure. |
| 1512 | * |
| 1513 | */ |
| 1514 | static int cipso_v4_gentag_loc(const struct cipso_v4_doi *doi_def, |
| 1515 | const struct netlbl_lsm_secattr *secattr, |
| 1516 | unsigned char *buffer, |
| 1517 | u32 buffer_len) |
| 1518 | { |
| 1519 | if (!(secattr->flags & NETLBL_SECATTR_SECID)) |
| 1520 | return -EPERM; |
| 1521 | |
| 1522 | buffer[0] = CIPSO_V4_TAG_LOCAL; |
| 1523 | buffer[1] = CIPSO_V4_TAG_LOC_BLEN; |
| 1524 | *(u32 *)&buffer[2] = secattr->attr.secid; |
| 1525 | |
| 1526 | return CIPSO_V4_TAG_LOC_BLEN; |
| 1527 | } |
| 1528 | |
| 1529 | /** |
| 1530 | * cipso_v4_parsetag_loc - Parse a CIPSO local tag |
| 1531 | * @doi_def: the DOI definition |
| 1532 | * @tag: the CIPSO tag |
| 1533 | * @secattr: the security attributes |
| 1534 | * |
| 1535 | * Description: |
| 1536 | * Parse a CIPSO local tag and return the security attributes in @secattr. |
| 1537 | * Return zero on success, negatives values on failure. |
| 1538 | * |
| 1539 | */ |
| 1540 | static int cipso_v4_parsetag_loc(const struct cipso_v4_doi *doi_def, |
| 1541 | const unsigned char *tag, |
| 1542 | struct netlbl_lsm_secattr *secattr) |
| 1543 | { |
| 1544 | secattr->attr.secid = *(u32 *)&tag[2]; |
| 1545 | secattr->flags |= NETLBL_SECATTR_SECID; |
| 1546 | |
| 1547 | return 0; |
| 1548 | } |
| 1549 | |
| 1550 | /** |
Paul Moore | 446fda4 | 2006-08-03 16:48:06 -0700 | [diff] [blame] | 1551 | * cipso_v4_validate - Validate a CIPSO option |
| 1552 | * @option: the start of the option, on error it is set to point to the error |
| 1553 | * |
| 1554 | * Description: |
| 1555 | * This routine is called to validate a CIPSO option, it checks all of the |
| 1556 | * fields to ensure that they are at least valid, see the draft snippet below |
| 1557 | * for details. If the option is valid then a zero value is returned and |
| 1558 | * the value of @option is unchanged. If the option is invalid then a |
| 1559 | * non-zero value is returned and @option is adjusted to point to the |
| 1560 | * offending portion of the option. From the IETF draft ... |
| 1561 | * |
| 1562 | * "If any field within the CIPSO options, such as the DOI identifier, is not |
| 1563 | * recognized the IP datagram is discarded and an ICMP 'parameter problem' |
| 1564 | * (type 12) is generated and returned. The ICMP code field is set to 'bad |
| 1565 | * parameter' (code 0) and the pointer is set to the start of the CIPSO field |
| 1566 | * that is unrecognized." |
| 1567 | * |
| 1568 | */ |
Paul Moore | 15c45f7 | 2008-10-10 10:16:34 -0400 | [diff] [blame] | 1569 | int cipso_v4_validate(const struct sk_buff *skb, unsigned char **option) |
Paul Moore | 446fda4 | 2006-08-03 16:48:06 -0700 | [diff] [blame] | 1570 | { |
| 1571 | unsigned char *opt = *option; |
| 1572 | unsigned char *tag; |
| 1573 | unsigned char opt_iter; |
| 1574 | unsigned char err_offset = 0; |
| 1575 | u8 opt_len; |
| 1576 | u8 tag_len; |
| 1577 | struct cipso_v4_doi *doi_def = NULL; |
| 1578 | u32 tag_iter; |
| 1579 | |
| 1580 | /* caller already checks for length values that are too large */ |
| 1581 | opt_len = opt[1]; |
| 1582 | if (opt_len < 8) { |
| 1583 | err_offset = 1; |
| 1584 | goto validate_return; |
| 1585 | } |
| 1586 | |
| 1587 | rcu_read_lock(); |
Harvey Harrison | d3e2ce3 | 2008-05-02 16:26:16 -0700 | [diff] [blame] | 1588 | doi_def = cipso_v4_doi_search(get_unaligned_be32(&opt[2])); |
Paul Moore | 446fda4 | 2006-08-03 16:48:06 -0700 | [diff] [blame] | 1589 | if (doi_def == NULL) { |
| 1590 | err_offset = 2; |
| 1591 | goto validate_return_locked; |
| 1592 | } |
| 1593 | |
Paul Moore | 15c45f7 | 2008-10-10 10:16:34 -0400 | [diff] [blame] | 1594 | opt_iter = CIPSO_V4_HDR_LEN; |
Paul Moore | 446fda4 | 2006-08-03 16:48:06 -0700 | [diff] [blame] | 1595 | tag = opt + opt_iter; |
| 1596 | while (opt_iter < opt_len) { |
| 1597 | for (tag_iter = 0; doi_def->tags[tag_iter] != tag[0];) |
| 1598 | if (doi_def->tags[tag_iter] == CIPSO_V4_TAG_INVALID || |
| 1599 | ++tag_iter == CIPSO_V4_TAG_MAXCNT) { |
| 1600 | err_offset = opt_iter; |
| 1601 | goto validate_return_locked; |
| 1602 | } |
| 1603 | |
| 1604 | tag_len = tag[1]; |
| 1605 | if (tag_len > (opt_len - opt_iter)) { |
| 1606 | err_offset = opt_iter + 1; |
| 1607 | goto validate_return_locked; |
| 1608 | } |
| 1609 | |
| 1610 | switch (tag[0]) { |
| 1611 | case CIPSO_V4_TAG_RBITMAP: |
Paul Moore | 15c45f7 | 2008-10-10 10:16:34 -0400 | [diff] [blame] | 1612 | if (tag_len < CIPSO_V4_TAG_RBM_BLEN) { |
Paul Moore | 446fda4 | 2006-08-03 16:48:06 -0700 | [diff] [blame] | 1613 | err_offset = opt_iter + 1; |
| 1614 | goto validate_return_locked; |
| 1615 | } |
| 1616 | |
| 1617 | /* We are already going to do all the verification |
| 1618 | * necessary at the socket layer so from our point of |
| 1619 | * view it is safe to turn these checks off (and less |
| 1620 | * work), however, the CIPSO draft says we should do |
| 1621 | * all the CIPSO validations here but it doesn't |
| 1622 | * really specify _exactly_ what we need to validate |
| 1623 | * ... so, just make it a sysctl tunable. */ |
| 1624 | if (cipso_v4_rbm_strictvalid) { |
| 1625 | if (cipso_v4_map_lvl_valid(doi_def, |
| 1626 | tag[3]) < 0) { |
| 1627 | err_offset = opt_iter + 3; |
| 1628 | goto validate_return_locked; |
| 1629 | } |
Paul Moore | 15c45f7 | 2008-10-10 10:16:34 -0400 | [diff] [blame] | 1630 | if (tag_len > CIPSO_V4_TAG_RBM_BLEN && |
Paul Moore | 446fda4 | 2006-08-03 16:48:06 -0700 | [diff] [blame] | 1631 | cipso_v4_map_cat_rbm_valid(doi_def, |
| 1632 | &tag[4], |
| 1633 | tag_len - 4) < 0) { |
| 1634 | err_offset = opt_iter + 4; |
| 1635 | goto validate_return_locked; |
| 1636 | } |
| 1637 | } |
| 1638 | break; |
Paul Moore | 654bbc2 | 2006-11-29 13:18:19 -0500 | [diff] [blame] | 1639 | case CIPSO_V4_TAG_ENUM: |
Paul Moore | 15c45f7 | 2008-10-10 10:16:34 -0400 | [diff] [blame] | 1640 | if (tag_len < CIPSO_V4_TAG_ENUM_BLEN) { |
Paul Moore | 654bbc2 | 2006-11-29 13:18:19 -0500 | [diff] [blame] | 1641 | err_offset = opt_iter + 1; |
| 1642 | goto validate_return_locked; |
| 1643 | } |
| 1644 | |
| 1645 | if (cipso_v4_map_lvl_valid(doi_def, |
| 1646 | tag[3]) < 0) { |
| 1647 | err_offset = opt_iter + 3; |
| 1648 | goto validate_return_locked; |
| 1649 | } |
Paul Moore | 15c45f7 | 2008-10-10 10:16:34 -0400 | [diff] [blame] | 1650 | if (tag_len > CIPSO_V4_TAG_ENUM_BLEN && |
Paul Moore | 654bbc2 | 2006-11-29 13:18:19 -0500 | [diff] [blame] | 1651 | cipso_v4_map_cat_enum_valid(doi_def, |
| 1652 | &tag[4], |
| 1653 | tag_len - 4) < 0) { |
| 1654 | err_offset = opt_iter + 4; |
| 1655 | goto validate_return_locked; |
| 1656 | } |
| 1657 | break; |
Paul Moore | 484b366 | 2006-11-29 13:18:20 -0500 | [diff] [blame] | 1658 | case CIPSO_V4_TAG_RANGE: |
Paul Moore | 15c45f7 | 2008-10-10 10:16:34 -0400 | [diff] [blame] | 1659 | if (tag_len < CIPSO_V4_TAG_RNG_BLEN) { |
Paul Moore | 484b366 | 2006-11-29 13:18:20 -0500 | [diff] [blame] | 1660 | err_offset = opt_iter + 1; |
| 1661 | goto validate_return_locked; |
| 1662 | } |
| 1663 | |
| 1664 | if (cipso_v4_map_lvl_valid(doi_def, |
| 1665 | tag[3]) < 0) { |
| 1666 | err_offset = opt_iter + 3; |
| 1667 | goto validate_return_locked; |
| 1668 | } |
Paul Moore | 15c45f7 | 2008-10-10 10:16:34 -0400 | [diff] [blame] | 1669 | if (tag_len > CIPSO_V4_TAG_RNG_BLEN && |
Paul Moore | 484b366 | 2006-11-29 13:18:20 -0500 | [diff] [blame] | 1670 | cipso_v4_map_cat_rng_valid(doi_def, |
| 1671 | &tag[4], |
| 1672 | tag_len - 4) < 0) { |
| 1673 | err_offset = opt_iter + 4; |
| 1674 | goto validate_return_locked; |
| 1675 | } |
| 1676 | break; |
Paul Moore | 15c45f7 | 2008-10-10 10:16:34 -0400 | [diff] [blame] | 1677 | case CIPSO_V4_TAG_LOCAL: |
| 1678 | /* This is a non-standard tag that we only allow for |
| 1679 | * local connections, so if the incoming interface is |
| 1680 | * not the loopback device drop the packet. */ |
| 1681 | if (!(skb->dev->flags & IFF_LOOPBACK)) { |
| 1682 | err_offset = opt_iter; |
| 1683 | goto validate_return_locked; |
| 1684 | } |
| 1685 | if (tag_len != CIPSO_V4_TAG_LOC_BLEN) { |
| 1686 | err_offset = opt_iter + 1; |
| 1687 | goto validate_return_locked; |
| 1688 | } |
| 1689 | break; |
Paul Moore | 446fda4 | 2006-08-03 16:48:06 -0700 | [diff] [blame] | 1690 | default: |
| 1691 | err_offset = opt_iter; |
| 1692 | goto validate_return_locked; |
| 1693 | } |
| 1694 | |
| 1695 | tag += tag_len; |
| 1696 | opt_iter += tag_len; |
| 1697 | } |
| 1698 | |
| 1699 | validate_return_locked: |
| 1700 | rcu_read_unlock(); |
| 1701 | validate_return: |
| 1702 | *option = opt + err_offset; |
| 1703 | return err_offset; |
| 1704 | } |
| 1705 | |
| 1706 | /** |
| 1707 | * cipso_v4_error - Send the correct reponse for a bad packet |
| 1708 | * @skb: the packet |
| 1709 | * @error: the error code |
| 1710 | * @gateway: CIPSO gateway flag |
| 1711 | * |
| 1712 | * Description: |
| 1713 | * Based on the error code given in @error, send an ICMP error message back to |
| 1714 | * the originating host. From the IETF draft ... |
| 1715 | * |
| 1716 | * "If the contents of the CIPSO [option] are valid but the security label is |
| 1717 | * outside of the configured host or port label range, the datagram is |
| 1718 | * discarded and an ICMP 'destination unreachable' (type 3) is generated and |
| 1719 | * returned. The code field of the ICMP is set to 'communication with |
| 1720 | * destination network administratively prohibited' (code 9) or to |
| 1721 | * 'communication with destination host administratively prohibited' |
| 1722 | * (code 10). The value of the code is dependent on whether the originator |
| 1723 | * of the ICMP message is acting as a CIPSO host or a CIPSO gateway. The |
| 1724 | * recipient of the ICMP message MUST be able to handle either value. The |
| 1725 | * same procedure is performed if a CIPSO [option] can not be added to an |
| 1726 | * IP packet because it is too large to fit in the IP options area." |
| 1727 | * |
| 1728 | * "If the error is triggered by receipt of an ICMP message, the message is |
| 1729 | * discarded and no response is permitted (consistent with general ICMP |
| 1730 | * processing rules)." |
| 1731 | * |
| 1732 | */ |
| 1733 | void cipso_v4_error(struct sk_buff *skb, int error, u32 gateway) |
| 1734 | { |
Arnaldo Carvalho de Melo | eddc9ec | 2007-04-20 22:47:35 -0700 | [diff] [blame] | 1735 | if (ip_hdr(skb)->protocol == IPPROTO_ICMP || error != -EACCES) |
Paul Moore | 446fda4 | 2006-08-03 16:48:06 -0700 | [diff] [blame] | 1736 | return; |
| 1737 | |
| 1738 | if (gateway) |
| 1739 | icmp_send(skb, ICMP_DEST_UNREACH, ICMP_NET_ANO, 0); |
| 1740 | else |
| 1741 | icmp_send(skb, ICMP_DEST_UNREACH, ICMP_HOST_ANO, 0); |
| 1742 | } |
| 1743 | |
| 1744 | /** |
Paul Moore | 948bf85 | 2008-10-10 10:16:32 -0400 | [diff] [blame] | 1745 | * cipso_v4_genopt - Generate a CIPSO option |
| 1746 | * @buf: the option buffer |
| 1747 | * @buf_len: the size of opt_buf |
Paul Moore | 446fda4 | 2006-08-03 16:48:06 -0700 | [diff] [blame] | 1748 | * @doi_def: the CIPSO DOI to use |
Paul Moore | 948bf85 | 2008-10-10 10:16:32 -0400 | [diff] [blame] | 1749 | * @secattr: the security attributes |
Paul Moore | 446fda4 | 2006-08-03 16:48:06 -0700 | [diff] [blame] | 1750 | * |
| 1751 | * Description: |
Paul Moore | 948bf85 | 2008-10-10 10:16:32 -0400 | [diff] [blame] | 1752 | * Generate a CIPSO option using the DOI definition and security attributes |
| 1753 | * passed to the function. Returns the length of the option on success and |
| 1754 | * negative values on failure. |
Paul Moore | 446fda4 | 2006-08-03 16:48:06 -0700 | [diff] [blame] | 1755 | * |
| 1756 | */ |
Paul Moore | 948bf85 | 2008-10-10 10:16:32 -0400 | [diff] [blame] | 1757 | static int cipso_v4_genopt(unsigned char *buf, u32 buf_len, |
| 1758 | const struct cipso_v4_doi *doi_def, |
| 1759 | const struct netlbl_lsm_secattr *secattr) |
Paul Moore | 446fda4 | 2006-08-03 16:48:06 -0700 | [diff] [blame] | 1760 | { |
Paul Moore | 948bf85 | 2008-10-10 10:16:32 -0400 | [diff] [blame] | 1761 | int ret_val; |
Paul Moore | 446fda4 | 2006-08-03 16:48:06 -0700 | [diff] [blame] | 1762 | u32 iter; |
Paul Moore | 446fda4 | 2006-08-03 16:48:06 -0700 | [diff] [blame] | 1763 | |
Paul Moore | 948bf85 | 2008-10-10 10:16:32 -0400 | [diff] [blame] | 1764 | if (buf_len <= CIPSO_V4_HDR_LEN) |
| 1765 | return -ENOSPC; |
Paul Moore | 91b1ed0 | 2006-11-17 17:38:49 -0500 | [diff] [blame] | 1766 | |
Paul Moore | 446fda4 | 2006-08-03 16:48:06 -0700 | [diff] [blame] | 1767 | /* XXX - This code assumes only one tag per CIPSO option which isn't |
| 1768 | * really a good assumption to make but since we only support the MAC |
| 1769 | * tags right now it is a safe assumption. */ |
| 1770 | iter = 0; |
| 1771 | do { |
Paul Moore | 91b1ed0 | 2006-11-17 17:38:49 -0500 | [diff] [blame] | 1772 | memset(buf, 0, buf_len); |
Paul Moore | 446fda4 | 2006-08-03 16:48:06 -0700 | [diff] [blame] | 1773 | switch (doi_def->tags[iter]) { |
| 1774 | case CIPSO_V4_TAG_RBITMAP: |
| 1775 | ret_val = cipso_v4_gentag_rbm(doi_def, |
Paul Moore | 91b1ed0 | 2006-11-17 17:38:49 -0500 | [diff] [blame] | 1776 | secattr, |
| 1777 | &buf[CIPSO_V4_HDR_LEN], |
| 1778 | buf_len - CIPSO_V4_HDR_LEN); |
Paul Moore | 446fda4 | 2006-08-03 16:48:06 -0700 | [diff] [blame] | 1779 | break; |
Paul Moore | 654bbc2 | 2006-11-29 13:18:19 -0500 | [diff] [blame] | 1780 | case CIPSO_V4_TAG_ENUM: |
| 1781 | ret_val = cipso_v4_gentag_enum(doi_def, |
| 1782 | secattr, |
| 1783 | &buf[CIPSO_V4_HDR_LEN], |
| 1784 | buf_len - CIPSO_V4_HDR_LEN); |
| 1785 | break; |
Paul Moore | 484b366 | 2006-11-29 13:18:20 -0500 | [diff] [blame] | 1786 | case CIPSO_V4_TAG_RANGE: |
| 1787 | ret_val = cipso_v4_gentag_rng(doi_def, |
| 1788 | secattr, |
| 1789 | &buf[CIPSO_V4_HDR_LEN], |
| 1790 | buf_len - CIPSO_V4_HDR_LEN); |
| 1791 | break; |
Paul Moore | 15c45f7 | 2008-10-10 10:16:34 -0400 | [diff] [blame] | 1792 | case CIPSO_V4_TAG_LOCAL: |
| 1793 | ret_val = cipso_v4_gentag_loc(doi_def, |
| 1794 | secattr, |
| 1795 | &buf[CIPSO_V4_HDR_LEN], |
| 1796 | buf_len - CIPSO_V4_HDR_LEN); |
| 1797 | break; |
Paul Moore | 446fda4 | 2006-08-03 16:48:06 -0700 | [diff] [blame] | 1798 | default: |
Paul Moore | 948bf85 | 2008-10-10 10:16:32 -0400 | [diff] [blame] | 1799 | return -EPERM; |
Paul Moore | 446fda4 | 2006-08-03 16:48:06 -0700 | [diff] [blame] | 1800 | } |
| 1801 | |
| 1802 | iter++; |
Paul Moore | 91b1ed0 | 2006-11-17 17:38:49 -0500 | [diff] [blame] | 1803 | } while (ret_val < 0 && |
Paul Moore | 446fda4 | 2006-08-03 16:48:06 -0700 | [diff] [blame] | 1804 | iter < CIPSO_V4_TAG_MAXCNT && |
| 1805 | doi_def->tags[iter] != CIPSO_V4_TAG_INVALID); |
Paul Moore | 91b1ed0 | 2006-11-17 17:38:49 -0500 | [diff] [blame] | 1806 | if (ret_val < 0) |
Paul Moore | 948bf85 | 2008-10-10 10:16:32 -0400 | [diff] [blame] | 1807 | return ret_val; |
Paul Moore | 91b1ed0 | 2006-11-17 17:38:49 -0500 | [diff] [blame] | 1808 | cipso_v4_gentag_hdr(doi_def, buf, ret_val); |
Paul Moore | 948bf85 | 2008-10-10 10:16:32 -0400 | [diff] [blame] | 1809 | return CIPSO_V4_HDR_LEN + ret_val; |
| 1810 | } |
| 1811 | |
| 1812 | /** |
| 1813 | * cipso_v4_sock_setattr - Add a CIPSO option to a socket |
| 1814 | * @sk: the socket |
| 1815 | * @doi_def: the CIPSO DOI to use |
| 1816 | * @secattr: the specific security attributes of the socket |
| 1817 | * |
| 1818 | * Description: |
| 1819 | * Set the CIPSO option on the given socket using the DOI definition and |
| 1820 | * security attributes passed to the function. This function requires |
| 1821 | * exclusive access to @sk, which means it either needs to be in the |
| 1822 | * process of being created or locked. Returns zero on success and negative |
| 1823 | * values on failure. |
| 1824 | * |
| 1825 | */ |
| 1826 | int cipso_v4_sock_setattr(struct sock *sk, |
| 1827 | const struct cipso_v4_doi *doi_def, |
| 1828 | const struct netlbl_lsm_secattr *secattr) |
| 1829 | { |
| 1830 | int ret_val = -EPERM; |
| 1831 | unsigned char *buf = NULL; |
| 1832 | u32 buf_len; |
| 1833 | u32 opt_len; |
| 1834 | struct ip_options *opt = NULL; |
| 1835 | struct inet_sock *sk_inet; |
| 1836 | struct inet_connection_sock *sk_conn; |
| 1837 | |
| 1838 | /* In the case of sock_create_lite(), the sock->sk field is not |
| 1839 | * defined yet but it is not a problem as the only users of these |
| 1840 | * "lite" PF_INET sockets are functions which do an accept() call |
| 1841 | * afterwards so we will label the socket as part of the accept(). */ |
| 1842 | if (sk == NULL) |
| 1843 | return 0; |
| 1844 | |
| 1845 | /* We allocate the maximum CIPSO option size here so we are probably |
| 1846 | * being a little wasteful, but it makes our life _much_ easier later |
| 1847 | * on and after all we are only talking about 40 bytes. */ |
| 1848 | buf_len = CIPSO_V4_OPT_LEN_MAX; |
| 1849 | buf = kmalloc(buf_len, GFP_ATOMIC); |
| 1850 | if (buf == NULL) { |
| 1851 | ret_val = -ENOMEM; |
| 1852 | goto socket_setattr_failure; |
| 1853 | } |
| 1854 | |
| 1855 | ret_val = cipso_v4_genopt(buf, buf_len, doi_def, secattr); |
| 1856 | if (ret_val < 0) |
| 1857 | goto socket_setattr_failure; |
| 1858 | buf_len = ret_val; |
Paul Moore | 446fda4 | 2006-08-03 16:48:06 -0700 | [diff] [blame] | 1859 | |
| 1860 | /* We can't use ip_options_get() directly because it makes a call to |
| 1861 | * ip_options_get_alloc() which allocates memory with GFP_KERNEL and |
Paul Moore | f8687af | 2006-10-30 15:22:15 -0800 | [diff] [blame] | 1862 | * we won't always have CAP_NET_RAW even though we _always_ want to |
| 1863 | * set the IPOPT_CIPSO option. */ |
Paul Moore | 446fda4 | 2006-08-03 16:48:06 -0700 | [diff] [blame] | 1864 | opt_len = (buf_len + 3) & ~3; |
| 1865 | opt = kzalloc(sizeof(*opt) + opt_len, GFP_ATOMIC); |
| 1866 | if (opt == NULL) { |
| 1867 | ret_val = -ENOMEM; |
| 1868 | goto socket_setattr_failure; |
| 1869 | } |
| 1870 | memcpy(opt->__data, buf, buf_len); |
| 1871 | opt->optlen = opt_len; |
Paul Moore | f8687af | 2006-10-30 15:22:15 -0800 | [diff] [blame] | 1872 | opt->cipso = sizeof(struct iphdr); |
Paul Moore | 446fda4 | 2006-08-03 16:48:06 -0700 | [diff] [blame] | 1873 | kfree(buf); |
| 1874 | buf = NULL; |
Paul Moore | 446fda4 | 2006-08-03 16:48:06 -0700 | [diff] [blame] | 1875 | |
| 1876 | sk_inet = inet_sk(sk); |
| 1877 | if (sk_inet->is_icsk) { |
| 1878 | sk_conn = inet_csk(sk); |
| 1879 | if (sk_inet->opt) |
| 1880 | sk_conn->icsk_ext_hdr_len -= sk_inet->opt->optlen; |
| 1881 | sk_conn->icsk_ext_hdr_len += opt->optlen; |
| 1882 | sk_conn->icsk_sync_mss(sk, sk_conn->icsk_pmtu_cookie); |
| 1883 | } |
| 1884 | opt = xchg(&sk_inet->opt, opt); |
| 1885 | kfree(opt); |
| 1886 | |
| 1887 | return 0; |
| 1888 | |
| 1889 | socket_setattr_failure: |
| 1890 | kfree(buf); |
| 1891 | kfree(opt); |
| 1892 | return ret_val; |
| 1893 | } |
| 1894 | |
| 1895 | /** |
Paul Moore | 014ab19 | 2008-10-10 10:16:33 -0400 | [diff] [blame] | 1896 | * cipso_v4_sock_delattr - Delete the CIPSO option from a socket |
| 1897 | * @sk: the socket |
| 1898 | * |
| 1899 | * Description: |
| 1900 | * Removes the CIPSO option from a socket, if present. |
| 1901 | * |
| 1902 | */ |
| 1903 | void cipso_v4_sock_delattr(struct sock *sk) |
| 1904 | { |
| 1905 | u8 hdr_delta; |
| 1906 | struct ip_options *opt; |
| 1907 | struct inet_sock *sk_inet; |
| 1908 | |
| 1909 | sk_inet = inet_sk(sk); |
| 1910 | opt = sk_inet->opt; |
| 1911 | if (opt == NULL || opt->cipso == 0) |
| 1912 | return; |
| 1913 | |
| 1914 | if (opt->srr || opt->rr || opt->ts || opt->router_alert) { |
| 1915 | u8 cipso_len; |
| 1916 | u8 cipso_off; |
| 1917 | unsigned char *cipso_ptr; |
| 1918 | int iter; |
| 1919 | int optlen_new; |
| 1920 | |
| 1921 | cipso_off = opt->cipso - sizeof(struct iphdr); |
| 1922 | cipso_ptr = &opt->__data[cipso_off]; |
| 1923 | cipso_len = cipso_ptr[1]; |
| 1924 | |
| 1925 | if (opt->srr > opt->cipso) |
| 1926 | opt->srr -= cipso_len; |
| 1927 | if (opt->rr > opt->cipso) |
| 1928 | opt->rr -= cipso_len; |
| 1929 | if (opt->ts > opt->cipso) |
| 1930 | opt->ts -= cipso_len; |
| 1931 | if (opt->router_alert > opt->cipso) |
| 1932 | opt->router_alert -= cipso_len; |
| 1933 | opt->cipso = 0; |
| 1934 | |
| 1935 | memmove(cipso_ptr, cipso_ptr + cipso_len, |
| 1936 | opt->optlen - cipso_off - cipso_len); |
| 1937 | |
| 1938 | /* determining the new total option length is tricky because of |
| 1939 | * the padding necessary, the only thing i can think to do at |
| 1940 | * this point is walk the options one-by-one, skipping the |
| 1941 | * padding at the end to determine the actual option size and |
| 1942 | * from there we can determine the new total option length */ |
| 1943 | iter = 0; |
| 1944 | optlen_new = 0; |
| 1945 | while (iter < opt->optlen) |
| 1946 | if (opt->__data[iter] != IPOPT_NOP) { |
| 1947 | iter += opt->__data[iter + 1]; |
| 1948 | optlen_new = iter; |
| 1949 | } else |
| 1950 | iter++; |
| 1951 | hdr_delta = opt->optlen; |
| 1952 | opt->optlen = (optlen_new + 3) & ~3; |
| 1953 | hdr_delta -= opt->optlen; |
| 1954 | } else { |
| 1955 | /* only the cipso option was present on the socket so we can |
| 1956 | * remove the entire option struct */ |
| 1957 | sk_inet->opt = NULL; |
| 1958 | hdr_delta = opt->optlen; |
| 1959 | kfree(opt); |
| 1960 | } |
| 1961 | |
| 1962 | if (sk_inet->is_icsk && hdr_delta > 0) { |
| 1963 | struct inet_connection_sock *sk_conn = inet_csk(sk); |
| 1964 | sk_conn->icsk_ext_hdr_len -= hdr_delta; |
| 1965 | sk_conn->icsk_sync_mss(sk, sk_conn->icsk_pmtu_cookie); |
| 1966 | } |
| 1967 | } |
| 1968 | |
| 1969 | /** |
Paul Moore | 63d804ea | 2007-09-15 21:45:13 -0700 | [diff] [blame] | 1970 | * cipso_v4_getattr - Helper function for the cipso_v4_*_getattr functions |
| 1971 | * @cipso: the CIPSO v4 option |
| 1972 | * @secattr: the security attributes |
| 1973 | * |
| 1974 | * Description: |
| 1975 | * Inspect @cipso and return the security attributes in @secattr. Returns zero |
| 1976 | * on success and negative values on failure. |
| 1977 | * |
| 1978 | */ |
| 1979 | static int cipso_v4_getattr(const unsigned char *cipso, |
| 1980 | struct netlbl_lsm_secattr *secattr) |
| 1981 | { |
| 1982 | int ret_val = -ENOMSG; |
| 1983 | u32 doi; |
| 1984 | struct cipso_v4_doi *doi_def; |
| 1985 | |
| 1986 | if (cipso_v4_cache_check(cipso, cipso[1], secattr) == 0) |
| 1987 | return 0; |
| 1988 | |
Harvey Harrison | d3e2ce3 | 2008-05-02 16:26:16 -0700 | [diff] [blame] | 1989 | doi = get_unaligned_be32(&cipso[2]); |
Paul Moore | 63d804ea | 2007-09-15 21:45:13 -0700 | [diff] [blame] | 1990 | rcu_read_lock(); |
| 1991 | doi_def = cipso_v4_doi_search(doi); |
| 1992 | if (doi_def == NULL) |
| 1993 | goto getattr_return; |
| 1994 | /* XXX - This code assumes only one tag per CIPSO option which isn't |
| 1995 | * really a good assumption to make but since we only support the MAC |
| 1996 | * tags right now it is a safe assumption. */ |
| 1997 | switch (cipso[6]) { |
| 1998 | case CIPSO_V4_TAG_RBITMAP: |
| 1999 | ret_val = cipso_v4_parsetag_rbm(doi_def, &cipso[6], secattr); |
| 2000 | break; |
| 2001 | case CIPSO_V4_TAG_ENUM: |
| 2002 | ret_val = cipso_v4_parsetag_enum(doi_def, &cipso[6], secattr); |
| 2003 | break; |
| 2004 | case CIPSO_V4_TAG_RANGE: |
| 2005 | ret_val = cipso_v4_parsetag_rng(doi_def, &cipso[6], secattr); |
| 2006 | break; |
Paul Moore | 15c45f7 | 2008-10-10 10:16:34 -0400 | [diff] [blame] | 2007 | case CIPSO_V4_TAG_LOCAL: |
| 2008 | ret_val = cipso_v4_parsetag_loc(doi_def, &cipso[6], secattr); |
| 2009 | break; |
Paul Moore | 63d804ea | 2007-09-15 21:45:13 -0700 | [diff] [blame] | 2010 | } |
Paul Moore | 16efd45 | 2008-01-29 08:37:59 -0500 | [diff] [blame] | 2011 | if (ret_val == 0) |
| 2012 | secattr->type = NETLBL_NLTYPE_CIPSOV4; |
Paul Moore | 63d804ea | 2007-09-15 21:45:13 -0700 | [diff] [blame] | 2013 | |
| 2014 | getattr_return: |
| 2015 | rcu_read_unlock(); |
| 2016 | return ret_val; |
| 2017 | } |
| 2018 | |
| 2019 | /** |
Paul Moore | 14a72f5 | 2006-09-25 15:52:01 -0700 | [diff] [blame] | 2020 | * cipso_v4_sock_getattr - Get the security attributes from a sock |
| 2021 | * @sk: the sock |
| 2022 | * @secattr: the security attributes |
| 2023 | * |
| 2024 | * Description: |
| 2025 | * Query @sk to see if there is a CIPSO option attached to the sock and if |
| 2026 | * there is return the CIPSO security attributes in @secattr. This function |
| 2027 | * requires that @sk be locked, or privately held, but it does not do any |
| 2028 | * locking itself. Returns zero on success and negative values on failure. |
| 2029 | * |
| 2030 | */ |
| 2031 | int cipso_v4_sock_getattr(struct sock *sk, struct netlbl_lsm_secattr *secattr) |
| 2032 | { |
Paul Moore | 63d804ea | 2007-09-15 21:45:13 -0700 | [diff] [blame] | 2033 | struct ip_options *opt; |
Paul Moore | 14a72f5 | 2006-09-25 15:52:01 -0700 | [diff] [blame] | 2034 | |
Paul Moore | 63d804ea | 2007-09-15 21:45:13 -0700 | [diff] [blame] | 2035 | opt = inet_sk(sk)->opt; |
| 2036 | if (opt == NULL || opt->cipso == 0) |
Paul Moore | 14a72f5 | 2006-09-25 15:52:01 -0700 | [diff] [blame] | 2037 | return -ENOMSG; |
Paul Moore | 14a72f5 | 2006-09-25 15:52:01 -0700 | [diff] [blame] | 2038 | |
Paul Moore | 63d804ea | 2007-09-15 21:45:13 -0700 | [diff] [blame] | 2039 | return cipso_v4_getattr(opt->__data + opt->cipso - sizeof(struct iphdr), |
| 2040 | secattr); |
Paul Moore | 14a72f5 | 2006-09-25 15:52:01 -0700 | [diff] [blame] | 2041 | } |
| 2042 | |
| 2043 | /** |
Paul Moore | 948bf85 | 2008-10-10 10:16:32 -0400 | [diff] [blame] | 2044 | * cipso_v4_skbuff_setattr - Set the CIPSO option on a packet |
| 2045 | * @skb: the packet |
| 2046 | * @secattr: the security attributes |
| 2047 | * |
| 2048 | * Description: |
| 2049 | * Set the CIPSO option on the given packet based on the security attributes. |
| 2050 | * Returns a pointer to the IP header on success and NULL on failure. |
| 2051 | * |
| 2052 | */ |
| 2053 | int cipso_v4_skbuff_setattr(struct sk_buff *skb, |
| 2054 | const struct cipso_v4_doi *doi_def, |
| 2055 | const struct netlbl_lsm_secattr *secattr) |
| 2056 | { |
| 2057 | int ret_val; |
| 2058 | struct iphdr *iph; |
| 2059 | struct ip_options *opt = &IPCB(skb)->opt; |
| 2060 | unsigned char buf[CIPSO_V4_OPT_LEN_MAX]; |
| 2061 | u32 buf_len = CIPSO_V4_OPT_LEN_MAX; |
| 2062 | u32 opt_len; |
| 2063 | int len_delta; |
| 2064 | |
roel kluin | 00af5c6 | 2008-10-29 15:55:53 -0400 | [diff] [blame] | 2065 | ret_val = cipso_v4_genopt(buf, buf_len, doi_def, secattr); |
| 2066 | if (ret_val < 0) |
| 2067 | return ret_val; |
| 2068 | buf_len = ret_val; |
Paul Moore | 948bf85 | 2008-10-10 10:16:32 -0400 | [diff] [blame] | 2069 | opt_len = (buf_len + 3) & ~3; |
| 2070 | |
| 2071 | /* we overwrite any existing options to ensure that we have enough |
| 2072 | * room for the CIPSO option, the reason is that we _need_ to guarantee |
| 2073 | * that the security label is applied to the packet - we do the same |
| 2074 | * thing when using the socket options and it hasn't caused a problem, |
| 2075 | * if we need to we can always revisit this choice later */ |
| 2076 | |
| 2077 | len_delta = opt_len - opt->optlen; |
| 2078 | /* if we don't ensure enough headroom we could panic on the skb_push() |
| 2079 | * call below so make sure we have enough, we are also "mangling" the |
| 2080 | * packet so we should probably do a copy-on-write call anyway */ |
| 2081 | ret_val = skb_cow(skb, skb_headroom(skb) + len_delta); |
| 2082 | if (ret_val < 0) |
| 2083 | return ret_val; |
| 2084 | |
| 2085 | if (len_delta > 0) { |
| 2086 | /* we assume that the header + opt->optlen have already been |
| 2087 | * "pushed" in ip_options_build() or similar */ |
| 2088 | iph = ip_hdr(skb); |
| 2089 | skb_push(skb, len_delta); |
| 2090 | memmove((char *)iph - len_delta, iph, iph->ihl << 2); |
| 2091 | skb_reset_network_header(skb); |
| 2092 | iph = ip_hdr(skb); |
| 2093 | } else if (len_delta < 0) { |
| 2094 | iph = ip_hdr(skb); |
| 2095 | memset(iph + 1, IPOPT_NOP, opt->optlen); |
| 2096 | } else |
| 2097 | iph = ip_hdr(skb); |
| 2098 | |
| 2099 | if (opt->optlen > 0) |
| 2100 | memset(opt, 0, sizeof(*opt)); |
| 2101 | opt->optlen = opt_len; |
| 2102 | opt->cipso = sizeof(struct iphdr); |
| 2103 | opt->is_changed = 1; |
| 2104 | |
| 2105 | /* we have to do the following because we are being called from a |
| 2106 | * netfilter hook which means the packet already has had the header |
| 2107 | * fields populated and the checksum calculated - yes this means we |
| 2108 | * are doing more work than needed but we do it to keep the core |
| 2109 | * stack clean and tidy */ |
| 2110 | memcpy(iph + 1, buf, buf_len); |
| 2111 | if (opt_len > buf_len) |
| 2112 | memset((char *)(iph + 1) + buf_len, 0, opt_len - buf_len); |
| 2113 | if (len_delta != 0) { |
| 2114 | iph->ihl = 5 + (opt_len >> 2); |
| 2115 | iph->tot_len = htons(skb->len); |
| 2116 | } |
| 2117 | ip_send_check(iph); |
| 2118 | |
| 2119 | return 0; |
| 2120 | } |
| 2121 | |
| 2122 | /** |
| 2123 | * cipso_v4_skbuff_delattr - Delete any CIPSO options from a packet |
| 2124 | * @skb: the packet |
| 2125 | * |
| 2126 | * Description: |
| 2127 | * Removes any and all CIPSO options from the given packet. Returns zero on |
| 2128 | * success, negative values on failure. |
| 2129 | * |
| 2130 | */ |
| 2131 | int cipso_v4_skbuff_delattr(struct sk_buff *skb) |
| 2132 | { |
| 2133 | int ret_val; |
| 2134 | struct iphdr *iph; |
| 2135 | struct ip_options *opt = &IPCB(skb)->opt; |
| 2136 | unsigned char *cipso_ptr; |
| 2137 | |
| 2138 | if (opt->cipso == 0) |
| 2139 | return 0; |
| 2140 | |
| 2141 | /* since we are changing the packet we should make a copy */ |
| 2142 | ret_val = skb_cow(skb, skb_headroom(skb)); |
| 2143 | if (ret_val < 0) |
| 2144 | return ret_val; |
| 2145 | |
| 2146 | /* the easiest thing to do is just replace the cipso option with noop |
| 2147 | * options since we don't change the size of the packet, although we |
| 2148 | * still need to recalculate the checksum */ |
| 2149 | |
| 2150 | iph = ip_hdr(skb); |
| 2151 | cipso_ptr = (unsigned char *)iph + opt->cipso; |
| 2152 | memset(cipso_ptr, IPOPT_NOOP, cipso_ptr[1]); |
| 2153 | opt->cipso = 0; |
| 2154 | opt->is_changed = 1; |
| 2155 | |
| 2156 | ip_send_check(iph); |
| 2157 | |
| 2158 | return 0; |
| 2159 | } |
| 2160 | |
| 2161 | /** |
Paul Moore | 446fda4 | 2006-08-03 16:48:06 -0700 | [diff] [blame] | 2162 | * cipso_v4_skbuff_getattr - Get the security attributes from the CIPSO option |
| 2163 | * @skb: the packet |
| 2164 | * @secattr: the security attributes |
| 2165 | * |
| 2166 | * Description: |
| 2167 | * Parse the given packet's CIPSO option and return the security attributes. |
| 2168 | * Returns zero on success and negative values on failure. |
| 2169 | * |
| 2170 | */ |
| 2171 | int cipso_v4_skbuff_getattr(const struct sk_buff *skb, |
| 2172 | struct netlbl_lsm_secattr *secattr) |
| 2173 | { |
Paul Moore | 63d804ea | 2007-09-15 21:45:13 -0700 | [diff] [blame] | 2174 | return cipso_v4_getattr(CIPSO_V4_OPTPTR(skb), secattr); |
Paul Moore | 446fda4 | 2006-08-03 16:48:06 -0700 | [diff] [blame] | 2175 | } |
| 2176 | |
| 2177 | /* |
| 2178 | * Setup Functions |
| 2179 | */ |
| 2180 | |
| 2181 | /** |
| 2182 | * cipso_v4_init - Initialize the CIPSO module |
| 2183 | * |
| 2184 | * Description: |
| 2185 | * Initialize the CIPSO module and prepare it for use. Returns zero on success |
| 2186 | * and negative values on failure. |
| 2187 | * |
| 2188 | */ |
| 2189 | static int __init cipso_v4_init(void) |
| 2190 | { |
| 2191 | int ret_val; |
| 2192 | |
| 2193 | ret_val = cipso_v4_cache_init(); |
| 2194 | if (ret_val != 0) |
| 2195 | panic("Failed to initialize the CIPSO/IPv4 cache (%d)\n", |
| 2196 | ret_val); |
| 2197 | |
| 2198 | return 0; |
| 2199 | } |
| 2200 | |
| 2201 | subsys_initcall(cipso_v4_init); |