| 1 | |
| 2 | |
| 3 | * CALIPSO - Common Architecture Label IPv6 Security Option
|
| 4 | *
|
| 5 | * This is an implementation of the CALIPSO protocol as specified in
|
| 6 | * RFC 5570.
|
| 7 | *
|
| 8 | * Authors: Paul Moore <[email protected]>
|
| 9 | * Huw Davies <[email protected]>
|
| 10 |
|
| 11 |
|
| 12 | |
| 13 | * (c) Copyright Huw Davies <[email protected]>, 2015
|
| 14 |
|
| 15 |
|
| 16 | #include <linux/init.h>
|
| 17 | #include <linux/types.h>
|
| 18 | #include <linux/rcupdate.h>
|
| 19 | #include <linux/list.h>
|
| 20 | #include <linux/spinlock.h>
|
| 21 | #include <linux/string.h>
|
| 22 | #include <linux/jhash.h>
|
| 23 | #include <linux/audit.h>
|
| 24 | #include <linux/slab.h>
|
| 25 | #include <net/ip.h>
|
| 26 | #include <net/icmp.h>
|
| 27 | #include <net/tcp.h>
|
| 28 | #include <net/netlabel.h>
|
| 29 | #include <net/calipso.h>
|
| 30 | #include <linux/atomic.h>
|
| 31 | #include <linux/bug.h>
|
| 32 | #include <linux/unaligned.h>
|
| 33 | #include <linux/crc-ccitt.h>
|
| 34 |
|
| 35 | |
| 36 | * the two-byte TLV header.
|
| 37 |
|
| 38 | #define CALIPSO_OPT_LEN_MAX (2 + 252)
|
| 39 |
|
| 40 | |
| 41 | * the two-byte TLV header.
|
| 42 |
|
| 43 | #define CALIPSO_HDR_LEN (2 + 8)
|
| 44 |
|
| 45 | |
| 46 | * the two-byte TLV header and upto 3 bytes of
|
| 47 | * leading pad and 7 bytes of trailing pad.
|
| 48 |
|
| 49 | #define CALIPSO_OPT_LEN_MAX_WITH_PAD (3 + CALIPSO_OPT_LEN_MAX + 7)
|
| 50 |
|
| 51 | |
| 52 | * option. Max of 3 initial pad bytes starting from buffer + 3.
|
| 53 | * i.e. the worst case is when the previous tlv finishes on 4n + 3.
|
| 54 |
|
| 55 | #define CALIPSO_MAX_BUFFER (6 + CALIPSO_OPT_LEN_MAX)
|
| 56 |
|
| 57 |
|
| 58 | static DEFINE_SPINLOCK(calipso_doi_list_lock);
|
| 59 | static LIST_HEAD(calipso_doi_list);
|
| 60 |
|
| 61 |
|
| 62 | int calipso_cache_enabled = 1;
|
| 63 | int calipso_cache_bucketsize = 10;
|
| 64 | #define CALIPSO_CACHE_BUCKETBITS 7
|
| 65 | #define CALIPSO_CACHE_BUCKETS BIT(CALIPSO_CACHE_BUCKETBITS)
|
| 66 | #define CALIPSO_CACHE_REORDERLIMIT 10
|
| 67 | struct calipso_map_cache_bkt {
|
| 68 | spinlock_t lock;
|
| 69 | u32 size;
|
| 70 | struct list_head list;
|
| 71 | };
|
| 72 |
|
| 73 | struct calipso_map_cache_entry {
|
| 74 | u32 hash;
|
| 75 | unsigned char *key;
|
| 76 | size_t key_len;
|
| 77 |
|
| 78 | struct netlbl_lsm_cache *lsm_data;
|
| 79 |
|
| 80 | u32 activity;
|
| 81 | struct list_head list;
|
| 82 | };
|
| 83 |
|
| 84 | static struct calipso_map_cache_bkt *calipso_cache;
|
| 85 |
|
| 86 | static void calipso_cache_invalidate(void);
|
| 87 | static void calipso_doi_putdef(struct calipso_doi *doi_def);
|
| 88 |
|
| 89 | |
| 90 |
|
| 91 |
|
| 92 | |
| 93 | * calipso_cache_entry_free - Frees a cache entry
|
| 94 | * @entry: the entry to free
|
| 95 | *
|
| 96 | * Description:
|
| 97 | * This function frees the memory associated with a cache entry including the
|
| 98 | * LSM cache data if there are no longer any users, i.e. reference count == 0.
|
| 99 | *
|
| 100 |
|
| 101 | static void calipso_cache_entry_free(struct calipso_map_cache_entry *entry)
|
| 102 | {
|
| 103 | if (entry->lsm_data)
|
| 104 | netlbl_secattr_cache_free(entry->lsm_data);
|
| 105 | kfree(entry->key);
|
| 106 | kfree(entry);
|
| 107 | }
|
| 108 |
|
| 109 | |
| 110 | * calipso_map_cache_hash - Hashing function for the CALIPSO cache
|
| 111 | * @key: the hash key
|
| 112 | * @key_len: the length of the key in bytes
|
| 113 | *
|
| 114 | * Description:
|
| 115 | * The CALIPSO tag hashing function. Returns a 32-bit hash value.
|
| 116 | *
|
| 117 |
|
| 118 | static u32 calipso_map_cache_hash(const unsigned char *key, u32 key_len)
|
| 119 | {
|
| 120 | return jhash(key, key_len, 0);
|
| 121 | }
|
| 122 |
|
| 123 | |
| 124 | * calipso_cache_init - Initialize the CALIPSO cache
|
| 125 | *
|
| 126 | * Description:
|
| 127 | * Initializes the CALIPSO label mapping cache, this function should be called
|
| 128 | * before any of the other functions defined in this file. Returns zero on
|
| 129 | * success, negative values on error.
|
| 130 | *
|
| 131 |
|
| 132 | static int __init calipso_cache_init(void)
|
| 133 | {
|
| 134 | u32 iter;
|
| 135 |
|
| 136 | calipso_cache = kzalloc_objs(struct calipso_map_cache_bkt,
|
| 137 | CALIPSO_CACHE_BUCKETS);
|
| 138 | if (!calipso_cache)
|
| 139 | return -ENOMEM;
|
| 140 |
|
| 141 | for (iter = 0; iter < CALIPSO_CACHE_BUCKETS; iter++) {
|
| 142 | spin_lock_init(&calipso_cache[iter].lock);
|
| 143 | calipso_cache[iter].size = 0;
|
| 144 | INIT_LIST_HEAD(&calipso_cache[iter].list);
|
| 145 | }
|
| 146 |
|
| 147 | return 0;
|
| 148 | }
|
| 149 |
|
| 150 | |
| 151 | * calipso_cache_invalidate - Invalidates the current CALIPSO cache
|
| 152 | *
|
| 153 | * Description:
|
| 154 | * Invalidates and frees any entries in the CALIPSO cache. Returns zero on
|
| 155 | * success and negative values on failure.
|
| 156 | *
|
| 157 |
|
| 158 | static void calipso_cache_invalidate(void)
|
| 159 | {
|
| 160 | struct calipso_map_cache_entry *entry, *tmp_entry;
|
| 161 | u32 iter;
|
| 162 |
|
| 163 | for (iter = 0; iter < CALIPSO_CACHE_BUCKETS; iter++) {
|
| 164 | spin_lock_bh(&calipso_cache[iter].lock);
|
| 165 | list_for_each_entry_safe(entry,
|
| 166 | tmp_entry,
|
| 167 | &calipso_cache[iter].list, list) {
|
| 168 | list_del(&entry->list);
|
| 169 | calipso_cache_entry_free(entry);
|
| 170 | }
|
| 171 | calipso_cache[iter].size = 0;
|
| 172 | spin_unlock_bh(&calipso_cache[iter].lock);
|
| 173 | }
|
| 174 | }
|
| 175 |
|
| 176 | |
| 177 | * calipso_cache_check - Check the CALIPSO cache for a label mapping
|
| 178 | * @key: the buffer to check
|
| 179 | * @key_len: buffer length in bytes
|
| 180 | * @secattr: the security attribute struct to use
|
| 181 | *
|
| 182 | * Description:
|
| 183 | * This function checks the cache to see if a label mapping already exists for
|
| 184 | * the given key. If there is a match then the cache is adjusted and the
|
| 185 | * @secattr struct is populated with the correct LSM security attributes. The
|
| 186 | * cache is adjusted in the following manner if the entry is not already the
|
| 187 | * first in the cache bucket:
|
| 188 | *
|
| 189 | * 1. The cache entry's activity counter is incremented
|
| 190 | * 2. The previous (higher ranking) entry's activity counter is decremented
|
| 191 | * 3. If the difference between the two activity counters is geater than
|
| 192 | * CALIPSO_CACHE_REORDERLIMIT the two entries are swapped
|
| 193 | *
|
| 194 | * Returns zero on success, -ENOENT for a cache miss, and other negative values
|
| 195 | * on error.
|
| 196 | *
|
| 197 |
|
| 198 | static int calipso_cache_check(const unsigned char *key,
|
| 199 | u32 key_len,
|
| 200 | struct netlbl_lsm_secattr *secattr)
|
| 201 | {
|
| 202 | u32 bkt;
|
| 203 | struct calipso_map_cache_entry *entry;
|
| 204 | struct calipso_map_cache_entry *prev_entry = NULL;
|
| 205 | u32 hash;
|
| 206 |
|
| 207 | if (!calipso_cache_enabled)
|
| 208 | return -ENOENT;
|
| 209 |
|
| 210 | hash = calipso_map_cache_hash(key, key_len);
|
| 211 | bkt = hash & (CALIPSO_CACHE_BUCKETS - 1);
|
| 212 | spin_lock_bh(&calipso_cache[bkt].lock);
|
| 213 | list_for_each_entry(entry, &calipso_cache[bkt].list, list) {
|
| 214 | if (entry->hash == hash &&
|
| 215 | entry->key_len == key_len &&
|
| 216 | memcmp(entry->key, key, key_len) == 0) {
|
| 217 | entry->activity += 1;
|
| 218 | refcount_inc(&entry->lsm_data->refcount);
|
| 219 | secattr->cache = entry->lsm_data;
|
| 220 | secattr->flags |= NETLBL_SECATTR_CACHE;
|
| 221 | secattr->type = NETLBL_NLTYPE_CALIPSO;
|
| 222 | if (!prev_entry) {
|
| 223 | spin_unlock_bh(&calipso_cache[bkt].lock);
|
| 224 | return 0;
|
| 225 | }
|
| 226 |
|
| 227 | if (prev_entry->activity > 0)
|
| 228 | prev_entry->activity -= 1;
|
| 229 | if (entry->activity > prev_entry->activity &&
|
| 230 | entry->activity - prev_entry->activity >
|
| 231 | CALIPSO_CACHE_REORDERLIMIT) {
|
| 232 | __list_del(entry->list.prev, entry->list.next);
|
| 233 | __list_add(&entry->list,
|
| 234 | prev_entry->list.prev,
|
| 235 | &prev_entry->list);
|
| 236 | }
|
| 237 |
|
| 238 | spin_unlock_bh(&calipso_cache[bkt].lock);
|
| 239 | return 0;
|
| 240 | }
|
| 241 | prev_entry = entry;
|
| 242 | }
|
| 243 | spin_unlock_bh(&calipso_cache[bkt].lock);
|
| 244 |
|
| 245 | return -ENOENT;
|
| 246 | }
|
| 247 |
|
| 248 | |
| 249 | * calipso_cache_add - Add an entry to the CALIPSO cache
|
| 250 | * @calipso_ptr: the CALIPSO option
|
| 251 | * @secattr: the packet's security attributes
|
| 252 | *
|
| 253 | * Description:
|
| 254 | * Add a new entry into the CALIPSO label mapping cache. Add the new entry to
|
| 255 | * head of the cache bucket's list, if the cache bucket is out of room remove
|
| 256 | * the last entry in the list first. It is important to note that there is
|
| 257 | * currently no checking for duplicate keys. Returns zero on success,
|
| 258 | * negative values on failure. The key stored starts at calipso_ptr + 2,
|
| 259 | * i.e. the type and length bytes are not stored, this corresponds to
|
| 260 | * calipso_ptr[1] bytes of data.
|
| 261 | *
|
| 262 |
|
| 263 | static int calipso_cache_add(const unsigned char *calipso_ptr,
|
| 264 | const struct netlbl_lsm_secattr *secattr)
|
| 265 | {
|
| 266 | int ret_val = -EPERM;
|
| 267 | u32 bkt;
|
| 268 | struct calipso_map_cache_entry *entry = NULL;
|
| 269 | struct calipso_map_cache_entry *old_entry = NULL;
|
| 270 | u32 calipso_ptr_len;
|
| 271 |
|
| 272 | if (!calipso_cache_enabled || calipso_cache_bucketsize <= 0)
|
| 273 | return 0;
|
| 274 |
|
| 275 | calipso_ptr_len = calipso_ptr[1];
|
| 276 |
|
| 277 | entry = kzalloc_obj(*entry, GFP_ATOMIC);
|
| 278 | if (!entry)
|
| 279 | return -ENOMEM;
|
| 280 | entry->key = kmemdup(calipso_ptr + 2, calipso_ptr_len, GFP_ATOMIC);
|
| 281 | if (!entry->key) {
|
| 282 | ret_val = -ENOMEM;
|
| 283 | goto cache_add_failure;
|
| 284 | }
|
| 285 | entry->key_len = calipso_ptr_len;
|
| 286 | entry->hash = calipso_map_cache_hash(calipso_ptr, calipso_ptr_len);
|
| 287 | refcount_inc(&secattr->cache->refcount);
|
| 288 | entry->lsm_data = secattr->cache;
|
| 289 |
|
| 290 | bkt = entry->hash & (CALIPSO_CACHE_BUCKETS - 1);
|
| 291 | spin_lock_bh(&calipso_cache[bkt].lock);
|
| 292 | if (calipso_cache[bkt].size < calipso_cache_bucketsize) {
|
| 293 | list_add(&entry->list, &calipso_cache[bkt].list);
|
| 294 | calipso_cache[bkt].size += 1;
|
| 295 | } else {
|
| 296 | old_entry = list_entry(calipso_cache[bkt].list.prev,
|
| 297 | struct calipso_map_cache_entry, list);
|
| 298 | list_del(&old_entry->list);
|
| 299 | list_add(&entry->list, &calipso_cache[bkt].list);
|
| 300 | calipso_cache_entry_free(old_entry);
|
| 301 | }
|
| 302 | spin_unlock_bh(&calipso_cache[bkt].lock);
|
| 303 |
|
| 304 | return 0;
|
| 305 |
|
| 306 | cache_add_failure:
|
| 307 | if (entry)
|
| 308 | calipso_cache_entry_free(entry);
|
| 309 | return ret_val;
|
| 310 | }
|
| 311 |
|
| 312 | |
| 313 |
|
| 314 |
|
| 315 | |
| 316 | * calipso_doi_search - Searches for a DOI definition
|
| 317 | * @doi: the DOI to search for
|
| 318 | *
|
| 319 | * Description:
|
| 320 | * Search the DOI definition list for a DOI definition with a DOI value that
|
| 321 | * matches @doi. The caller is responsible for calling rcu_read_[un]lock().
|
| 322 | * Returns a pointer to the DOI definition on success and NULL on failure.
|
| 323 |
|
| 324 | static struct calipso_doi *calipso_doi_search(u32 doi)
|
| 325 | {
|
| 326 | struct calipso_doi *iter;
|
| 327 |
|
| 328 | list_for_each_entry_rcu(iter, &calipso_doi_list, list)
|
| 329 | if (iter->doi == doi && refcount_read(&iter->refcount))
|
| 330 | return iter;
|
| 331 | return NULL;
|
| 332 | }
|
| 333 |
|
| 334 | |
| 335 | * calipso_doi_add - Add a new DOI to the CALIPSO protocol engine
|
| 336 | * @doi_def: the DOI structure
|
| 337 | * @audit_info: NetLabel audit information
|
| 338 | *
|
| 339 | * Description:
|
| 340 | * The caller defines a new DOI for use by the CALIPSO engine and calls this
|
| 341 | * function to add it to the list of acceptable domains. The caller must
|
| 342 | * ensure that the mapping table specified in @doi_def->map meets all of the
|
| 343 | * requirements of the mapping type (see calipso.h for details). Returns
|
| 344 | * zero on success and non-zero on failure.
|
| 345 | *
|
| 346 |
|
| 347 | static int calipso_doi_add(struct calipso_doi *doi_def,
|
| 348 | struct netlbl_audit *audit_info)
|
| 349 | {
|
| 350 | int ret_val = -EINVAL;
|
| 351 | u32 doi;
|
| 352 | u32 doi_type;
|
| 353 | struct audit_buffer *audit_buf;
|
| 354 |
|
| 355 | doi = doi_def->doi;
|
| 356 | doi_type = doi_def->type;
|
| 357 |
|
| 358 | if (doi_def->doi == CALIPSO_DOI_UNKNOWN)
|
| 359 | goto doi_add_return;
|
| 360 |
|
| 361 | refcount_set(&doi_def->refcount, 1);
|
| 362 |
|
| 363 | spin_lock(&calipso_doi_list_lock);
|
| 364 | if (calipso_doi_search(doi_def->doi)) {
|
| 365 | spin_unlock(&calipso_doi_list_lock);
|
| 366 | ret_val = -EEXIST;
|
| 367 | goto doi_add_return;
|
| 368 | }
|
| 369 | list_add_tail_rcu(&doi_def->list, &calipso_doi_list);
|
| 370 | spin_unlock(&calipso_doi_list_lock);
|
| 371 | ret_val = 0;
|
| 372 |
|
| 373 | doi_add_return:
|
| 374 | audit_buf = netlbl_audit_start(AUDIT_MAC_CALIPSO_ADD, audit_info);
|
| 375 | if (audit_buf) {
|
| 376 | const char *type_str;
|
| 377 |
|
| 378 | switch (doi_type) {
|
| 379 | case CALIPSO_MAP_PASS:
|
| 380 | type_str = "pass";
|
| 381 | break;
|
| 382 | default:
|
| 383 | type_str = "(unknown)";
|
| 384 | }
|
| 385 | audit_log_format(audit_buf,
|
| 386 | " calipso_doi=%u calipso_type=%s res=%u",
|
| 387 | doi, type_str, ret_val == 0 ? 1 : 0);
|
| 388 | audit_log_end(audit_buf);
|
| 389 | }
|
| 390 |
|
| 391 | return ret_val;
|
| 392 | }
|
| 393 |
|
| 394 | |
| 395 | * calipso_doi_free - Frees a DOI definition
|
| 396 | * @doi_def: the DOI definition
|
| 397 | *
|
| 398 | * Description:
|
| 399 | * This function frees all of the memory associated with a DOI definition.
|
| 400 | *
|
| 401 |
|
| 402 | static void calipso_doi_free(struct calipso_doi *doi_def)
|
| 403 | {
|
| 404 | kfree(doi_def);
|
| 405 | }
|
| 406 |
|
| 407 | |
| 408 | * calipso_doi_free_rcu - Frees a DOI definition via the RCU pointer
|
| 409 | * @entry: the entry's RCU field
|
| 410 | *
|
| 411 | * Description:
|
| 412 | * This function is designed to be used as a callback to the call_rcu()
|
| 413 | * function so that the memory allocated to the DOI definition can be released
|
| 414 | * safely.
|
| 415 | *
|
| 416 |
|
| 417 | static void calipso_doi_free_rcu(struct rcu_head *entry)
|
| 418 | {
|
| 419 | struct calipso_doi *doi_def;
|
| 420 |
|
| 421 | doi_def = container_of(entry, struct calipso_doi, rcu);
|
| 422 | calipso_doi_free(doi_def);
|
| 423 | }
|
| 424 |
|
| 425 | |
| 426 | * calipso_doi_remove - Remove an existing DOI from the CALIPSO protocol engine
|
| 427 | * @doi: the DOI value
|
| 428 | * @audit_info: NetLabel audit information
|
| 429 | *
|
| 430 | * Description:
|
| 431 | * Removes a DOI definition from the CALIPSO engine. The NetLabel routines will
|
| 432 | * be called to release their own LSM domain mappings as well as our own
|
| 433 | * domain list. Returns zero on success and negative values on failure.
|
| 434 | *
|
| 435 |
|
| 436 | static int calipso_doi_remove(u32 doi, struct netlbl_audit *audit_info)
|
| 437 | {
|
| 438 | int ret_val;
|
| 439 | struct calipso_doi *doi_def;
|
| 440 | struct audit_buffer *audit_buf;
|
| 441 |
|
| 442 | spin_lock(&calipso_doi_list_lock);
|
| 443 | doi_def = calipso_doi_search(doi);
|
| 444 | if (!doi_def) {
|
| 445 | spin_unlock(&calipso_doi_list_lock);
|
| 446 | ret_val = -ENOENT;
|
| 447 | goto doi_remove_return;
|
| 448 | }
|
| 449 | list_del_rcu(&doi_def->list);
|
| 450 | spin_unlock(&calipso_doi_list_lock);
|
| 451 |
|
| 452 | calipso_doi_putdef(doi_def);
|
| 453 | ret_val = 0;
|
| 454 |
|
| 455 | doi_remove_return:
|
| 456 | audit_buf = netlbl_audit_start(AUDIT_MAC_CALIPSO_DEL, audit_info);
|
| 457 | if (audit_buf) {
|
| 458 | audit_log_format(audit_buf,
|
| 459 | " calipso_doi=%u res=%u",
|
| 460 | doi, ret_val == 0 ? 1 : 0);
|
| 461 | audit_log_end(audit_buf);
|
| 462 | }
|
| 463 |
|
| 464 | return ret_val;
|
| 465 | }
|
| 466 |
|
| 467 | |
| 468 | * calipso_doi_getdef - Returns a reference to a valid DOI definition
|
| 469 | * @doi: the DOI value
|
| 470 | *
|
| 471 | * Description:
|
| 472 | * Searches for a valid DOI definition and if one is found it is returned to
|
| 473 | * the caller. Otherwise NULL is returned. The caller must ensure that
|
| 474 | * calipso_doi_putdef() is called when the caller is done.
|
| 475 | *
|
| 476 |
|
| 477 | static struct calipso_doi *calipso_doi_getdef(u32 doi)
|
| 478 | {
|
| 479 | struct calipso_doi *doi_def;
|
| 480 |
|
| 481 | rcu_read_lock();
|
| 482 | doi_def = calipso_doi_search(doi);
|
| 483 | if (!doi_def)
|
| 484 | goto doi_getdef_return;
|
| 485 | if (!refcount_inc_not_zero(&doi_def->refcount))
|
| 486 | doi_def = NULL;
|
| 487 |
|
| 488 | doi_getdef_return:
|
| 489 | rcu_read_unlock();
|
| 490 | return doi_def;
|
| 491 | }
|
| 492 |
|
| 493 | |
| 494 | * calipso_doi_putdef - Releases a reference for the given DOI definition
|
| 495 | * @doi_def: the DOI definition
|
| 496 | *
|
| 497 | * Description:
|
| 498 | * Releases a DOI definition reference obtained from calipso_doi_getdef().
|
| 499 | *
|
| 500 |
|
| 501 | static void calipso_doi_putdef(struct calipso_doi *doi_def)
|
| 502 | {
|
| 503 | if (!doi_def)
|
| 504 | return;
|
| 505 |
|
| 506 | if (!refcount_dec_and_test(&doi_def->refcount))
|
| 507 | return;
|
| 508 |
|
| 509 | calipso_cache_invalidate();
|
| 510 | call_rcu(&doi_def->rcu, calipso_doi_free_rcu);
|
| 511 | }
|
| 512 |
|
| 513 | |
| 514 | * calipso_doi_walk - Iterate through the DOI definitions
|
| 515 | * @skip_cnt: skip past this number of DOI definitions, updated
|
| 516 | * @callback: callback for each DOI definition
|
| 517 | * @cb_arg: argument for the callback function
|
| 518 | *
|
| 519 | * Description:
|
| 520 | * Iterate over the DOI definition list, skipping the first @skip_cnt entries.
|
| 521 | * For each entry call @callback, if @callback returns a negative value stop
|
| 522 | * 'walking' through the list and return. Updates the value in @skip_cnt upon
|
| 523 | * return. Returns zero on success, negative values on failure.
|
| 524 | *
|
| 525 |
|
| 526 | static int calipso_doi_walk(u32 *skip_cnt,
|
| 527 | int (*callback)(struct calipso_doi *doi_def,
|
| 528 | void *arg),
|
| 529 | void *cb_arg)
|
| 530 | {
|
| 531 | int ret_val = -ENOENT;
|
| 532 | u32 doi_cnt = 0;
|
| 533 | struct calipso_doi *iter_doi;
|
| 534 |
|
| 535 | rcu_read_lock();
|
| 536 | list_for_each_entry_rcu(iter_doi, &calipso_doi_list, list)
|
| 537 | if (refcount_read(&iter_doi->refcount) > 0) {
|
| 538 | if (doi_cnt++ < *skip_cnt)
|
| 539 | continue;
|
| 540 | ret_val = callback(iter_doi, cb_arg);
|
| 541 | if (ret_val < 0) {
|
| 542 | doi_cnt--;
|
| 543 | goto doi_walk_return;
|
| 544 | }
|
| 545 | }
|
| 546 |
|
| 547 | doi_walk_return:
|
| 548 | rcu_read_unlock();
|
| 549 | *skip_cnt = doi_cnt;
|
| 550 | return ret_val;
|
| 551 | }
|
| 552 |
|
| 553 | |
| 554 | * calipso_validate - Validate a CALIPSO option
|
| 555 | * @skb: the packet
|
| 556 | * @option: the start of the option
|
| 557 | *
|
| 558 | * Description:
|
| 559 | * This routine is called to validate a CALIPSO option.
|
| 560 | * If the option is valid then %true is returned, otherwise
|
| 561 | * %false is returned.
|
| 562 | *
|
| 563 | * The caller should have already checked that the length of the
|
| 564 | * option (including the TLV header) is >= 10 and that the catmap
|
| 565 | * length is consistent with the option length.
|
| 566 | *
|
| 567 | * We leave checks on the level and categories to the socket layer.
|
| 568 |
|
| 569 | bool calipso_validate(const struct sk_buff *skb, const unsigned char *option)
|
| 570 | {
|
| 571 | struct calipso_doi *doi_def;
|
| 572 | bool ret_val;
|
| 573 | u16 crc, len = option[1] + 2;
|
| 574 | static const u8 zero[2];
|
| 575 |
|
| 576 | |
| 577 | * with the CRC-16 field (at offset 8) zeroed out.
|
| 578 | crc = crc_ccitt(0xffff, option, 8);
|
| 579 | crc = crc_ccitt(crc, zero, sizeof(zero));
|
| 580 | if (len > 10)
|
| 581 | crc = crc_ccitt(crc, option + 10, len - 10);
|
| 582 | crc = ~crc;
|
| 583 | if (option[8] != (crc & 0xff) || option[9] != ((crc >> 8) & 0xff))
|
| 584 | return false;
|
| 585 |
|
| 586 | rcu_read_lock();
|
| 587 | doi_def = calipso_doi_search(get_unaligned_be32(option + 2));
|
| 588 | ret_val = !!doi_def;
|
| 589 | rcu_read_unlock();
|
| 590 |
|
| 591 | return ret_val;
|
| 592 | }
|
| 593 |
|
| 594 | |
| 595 | * calipso_map_cat_hton - Perform a category mapping from host to network
|
| 596 | * @doi_def: the DOI definition
|
| 597 | * @secattr: the security attributes
|
| 598 | * @net_cat: the zero'd out category bitmap in network/CALIPSO format
|
| 599 | * @net_cat_len: the length of the CALIPSO bitmap in bytes
|
| 600 | *
|
| 601 | * Description:
|
| 602 | * Perform a label mapping to translate a local MLS category bitmap to the
|
| 603 | * correct CALIPSO bitmap using the given DOI definition. Returns the minimum
|
| 604 | * size in bytes of the network bitmap on success, negative values otherwise.
|
| 605 | *
|
| 606 |
|
| 607 | static int calipso_map_cat_hton(const struct calipso_doi *doi_def,
|
| 608 | const struct netlbl_lsm_secattr *secattr,
|
| 609 | unsigned char *net_cat,
|
| 610 | u32 net_cat_len)
|
| 611 | {
|
| 612 | int spot = -1;
|
| 613 | u32 net_spot_max = 0;
|
| 614 | u32 net_clen_bits = net_cat_len * 8;
|
| 615 |
|
| 616 | for (;;) {
|
| 617 | spot = netlbl_catmap_walk(secattr->attr.mls.cat,
|
| 618 | spot + 1);
|
| 619 | if (spot < 0)
|
| 620 | break;
|
| 621 | if (spot >= net_clen_bits)
|
| 622 | return -ENOSPC;
|
| 623 | netlbl_bitmap_setbit(net_cat, spot, 1);
|
| 624 |
|
| 625 | if (spot > net_spot_max)
|
| 626 | net_spot_max = spot;
|
| 627 | }
|
| 628 |
|
| 629 | return (net_spot_max / 32 + 1) * 4;
|
| 630 | }
|
| 631 |
|
| 632 | |
| 633 | * calipso_map_cat_ntoh - Perform a category mapping from network to host
|
| 634 | * @doi_def: the DOI definition
|
| 635 | * @net_cat: the category bitmap in network/CALIPSO format
|
| 636 | * @net_cat_len: the length of the CALIPSO bitmap in bytes
|
| 637 | * @secattr: the security attributes
|
| 638 | *
|
| 639 | * Description:
|
| 640 | * Perform a label mapping to translate a CALIPSO bitmap to the correct local
|
| 641 | * MLS category bitmap using the given DOI definition. Returns zero on
|
| 642 | * success, negative values on failure.
|
| 643 | *
|
| 644 |
|
| 645 | static int calipso_map_cat_ntoh(const struct calipso_doi *doi_def,
|
| 646 | const unsigned char *net_cat,
|
| 647 | u32 net_cat_len,
|
| 648 | struct netlbl_lsm_secattr *secattr)
|
| 649 | {
|
| 650 | int ret_val;
|
| 651 | int spot = -1;
|
| 652 | u32 net_clen_bits = net_cat_len * 8;
|
| 653 |
|
| 654 | for (;;) {
|
| 655 | spot = netlbl_bitmap_walk(net_cat,
|
| 656 | net_clen_bits,
|
| 657 | spot + 1,
|
| 658 | 1);
|
| 659 | if (spot < 0)
|
| 660 | return 0;
|
| 661 |
|
| 662 | ret_val = netlbl_catmap_setbit(&secattr->attr.mls.cat,
|
| 663 | spot,
|
| 664 | GFP_ATOMIC);
|
| 665 | if (ret_val != 0)
|
| 666 | return ret_val;
|
| 667 | }
|
| 668 |
|
| 669 | return -EINVAL;
|
| 670 | }
|
| 671 |
|
| 672 | |
| 673 | * calipso_pad_write - Writes pad bytes in TLV format
|
| 674 | * @buf: the buffer
|
| 675 | * @offset: offset from start of buffer to write padding
|
| 676 | * @count: number of pad bytes to write
|
| 677 | *
|
| 678 | * Description:
|
| 679 | * Write @count bytes of TLV padding into @buffer starting at offset @offset.
|
| 680 | * @count should be less than 8 - see RFC 4942.
|
| 681 | *
|
| 682 |
|
| 683 | static int calipso_pad_write(unsigned char *buf, unsigned int offset,
|
| 684 | unsigned int count)
|
| 685 | {
|
| 686 | if (WARN_ON_ONCE(count >= 8))
|
| 687 | return -EINVAL;
|
| 688 |
|
| 689 | switch (count) {
|
| 690 | case 0:
|
| 691 | break;
|
| 692 | case 1:
|
| 693 | buf[offset] = IPV6_TLV_PAD1;
|
| 694 | break;
|
| 695 | default:
|
| 696 | buf[offset] = IPV6_TLV_PADN;
|
| 697 | buf[offset + 1] = count - 2;
|
| 698 | if (count > 2)
|
| 699 | memset(buf + offset + 2, 0, count - 2);
|
| 700 | break;
|
| 701 | }
|
| 702 | return 0;
|
| 703 | }
|
| 704 |
|
| 705 | |
| 706 | * calipso_genopt - Generate a CALIPSO option
|
| 707 | * @buf: the option buffer
|
| 708 | * @start: offset from which to write
|
| 709 | * @buf_len: the size of opt_buf
|
| 710 | * @doi_def: the CALIPSO DOI to use
|
| 711 | * @secattr: the security attributes
|
| 712 | *
|
| 713 | * Description:
|
| 714 | * Generate a CALIPSO option using the DOI definition and security attributes
|
| 715 | * passed to the function. This also generates upto three bytes of leading
|
| 716 | * padding that ensures that the option is 4n + 2 aligned. It returns the
|
| 717 | * number of bytes written (including any initial padding).
|
| 718 |
|
| 719 | static int calipso_genopt(unsigned char *buf, u32 start, u32 buf_len,
|
| 720 | const struct calipso_doi *doi_def,
|
| 721 | const struct netlbl_lsm_secattr *secattr)
|
| 722 | {
|
| 723 | int ret_val;
|
| 724 | u32 len, pad;
|
| 725 | u16 crc;
|
| 726 | static const unsigned char padding[4] = {2, 1, 0, 3};
|
| 727 | unsigned char *calipso;
|
| 728 |
|
| 729 |
|
| 730 | pad = padding[start & 3];
|
| 731 | if (buf_len <= start + pad + CALIPSO_HDR_LEN)
|
| 732 | return -ENOSPC;
|
| 733 |
|
| 734 | if ((secattr->flags & NETLBL_SECATTR_MLS_LVL) == 0)
|
| 735 | return -EPERM;
|
| 736 |
|
| 737 | len = CALIPSO_HDR_LEN;
|
| 738 |
|
| 739 | if (secattr->flags & NETLBL_SECATTR_MLS_CAT) {
|
| 740 | ret_val = calipso_map_cat_hton(doi_def,
|
| 741 | secattr,
|
| 742 | buf + start + pad + len,
|
| 743 | buf_len - start - pad - len);
|
| 744 | if (ret_val < 0)
|
| 745 | return ret_val;
|
| 746 | len += ret_val;
|
| 747 | }
|
| 748 |
|
| 749 | calipso_pad_write(buf, start, pad);
|
| 750 | calipso = buf + start + pad;
|
| 751 |
|
| 752 | calipso[0] = IPV6_TLV_CALIPSO;
|
| 753 | calipso[1] = len - 2;
|
| 754 | *(__be32 *)(calipso + 2) = htonl(doi_def->doi);
|
| 755 | calipso[6] = (len - CALIPSO_HDR_LEN) / 4;
|
| 756 | calipso[7] = secattr->attr.mls.lvl;
|
| 757 | crc = ~crc_ccitt(0xffff, calipso, len);
|
| 758 | calipso[8] = crc & 0xff;
|
| 759 | calipso[9] = (crc >> 8) & 0xff;
|
| 760 | return pad + len;
|
| 761 | }
|
| 762 |
|
| 763 | |
| 764 |
|
| 765 |
|
| 766 | |
| 767 | * calipso_opt_update - Replaces socket's hop options with a new set
|
| 768 | * @sk: the socket
|
| 769 | * @hop: new hop options
|
| 770 | *
|
| 771 | * Description:
|
| 772 | * Replaces @sk's hop options with @hop. @hop may be NULL to leave
|
| 773 | * the socket with no hop options.
|
| 774 | *
|
| 775 |
|
| 776 | static int calipso_opt_update(struct sock *sk, struct ipv6_opt_hdr *hop)
|
| 777 | {
|
| 778 | struct ipv6_txoptions *old = txopt_get(inet6_sk(sk)), *txopts;
|
| 779 |
|
| 780 | txopts = ipv6_renew_options(sk, old, IPV6_HOPOPTS, hop);
|
| 781 | txopt_put(old);
|
| 782 | if (IS_ERR(txopts))
|
| 783 | return PTR_ERR(txopts);
|
| 784 |
|
| 785 | txopts = ipv6_update_options(sk, txopts);
|
| 786 | if (txopts) {
|
| 787 | atomic_sub(txopts->tot_len, &sk->sk_omem_alloc);
|
| 788 | txopt_put(txopts);
|
| 789 | }
|
| 790 |
|
| 791 | return 0;
|
| 792 | }
|
| 793 |
|
| 794 | |
| 795 | * calipso_tlv_len - Returns the length of the TLV
|
| 796 | * @opt: the option header
|
| 797 | * @offset: offset of the TLV within the header
|
| 798 | *
|
| 799 | * Description:
|
| 800 | * Returns the length of the TLV option at offset @offset within
|
| 801 | * the option header @opt. Checks that the entire TLV fits inside
|
| 802 | * the option header, returns a negative value if this is not the case.
|
| 803 |
|
| 804 | static int calipso_tlv_len(struct ipv6_opt_hdr *opt, unsigned int offset)
|
| 805 | {
|
| 806 | unsigned char *tlv = (unsigned char *)opt;
|
| 807 | unsigned int opt_len = ipv6_optlen(opt), tlv_len;
|
| 808 |
|
| 809 | if (offset < sizeof(*opt) || offset >= opt_len)
|
| 810 | return -EINVAL;
|
| 811 | if (tlv[offset] == IPV6_TLV_PAD1)
|
| 812 | return 1;
|
| 813 | if (offset + 1 >= opt_len)
|
| 814 | return -EINVAL;
|
| 815 | tlv_len = tlv[offset + 1] + 2;
|
| 816 | if (offset + tlv_len > opt_len)
|
| 817 | return -EINVAL;
|
| 818 | return tlv_len;
|
| 819 | }
|
| 820 |
|
| 821 | |
| 822 | * calipso_opt_find - Finds the CALIPSO option in an IPv6 hop options header
|
| 823 | * @hop: the hop options header
|
| 824 | * @start: on return holds the offset of any leading padding
|
| 825 | * @end: on return holds the offset of the first non-pad TLV after CALIPSO
|
| 826 | *
|
| 827 | * Description:
|
| 828 | * Finds the space occupied by a CALIPSO option (including any leading and
|
| 829 | * trailing padding).
|
| 830 | *
|
| 831 | * If a CALIPSO option exists set @start and @end to the
|
| 832 | * offsets within @hop of the start of padding before the first
|
| 833 | * CALIPSO option and the end of padding after the first CALIPSO
|
| 834 | * option. In this case the function returns 0.
|
| 835 | *
|
| 836 | * In the absence of a CALIPSO option, @start and @end will be
|
| 837 | * set to the start and end of any trailing padding in the header.
|
| 838 | * This is useful when appending a new option, as the caller may want
|
| 839 | * to overwrite some of this padding. In this case the function will
|
| 840 | * return -ENOENT.
|
| 841 |
|
| 842 | static int calipso_opt_find(struct ipv6_opt_hdr *hop, unsigned int *start,
|
| 843 | unsigned int *end)
|
| 844 | {
|
| 845 | int ret_val = -ENOENT, tlv_len;
|
| 846 | unsigned int opt_len, offset, offset_s = 0, offset_e = 0;
|
| 847 | unsigned char *opt = (unsigned char *)hop;
|
| 848 |
|
| 849 | opt_len = ipv6_optlen(hop);
|
| 850 | offset = sizeof(*hop);
|
| 851 |
|
| 852 | while (offset < opt_len) {
|
| 853 | tlv_len = calipso_tlv_len(hop, offset);
|
| 854 | if (tlv_len < 0)
|
| 855 | return tlv_len;
|
| 856 |
|
| 857 | switch (opt[offset]) {
|
| 858 | case IPV6_TLV_PAD1:
|
| 859 | case IPV6_TLV_PADN:
|
| 860 | if (offset_e)
|
| 861 | offset_e = offset;
|
| 862 | break;
|
| 863 | case IPV6_TLV_CALIPSO:
|
| 864 | ret_val = 0;
|
| 865 | offset_e = offset;
|
| 866 | break;
|
| 867 | default:
|
| 868 | if (offset_e == 0)
|
| 869 | offset_s = offset;
|
| 870 | else
|
| 871 | goto out;
|
| 872 | }
|
| 873 | offset += tlv_len;
|
| 874 | }
|
| 875 |
|
| 876 | out:
|
| 877 | if (offset_s)
|
| 878 | *start = offset_s + calipso_tlv_len(hop, offset_s);
|
| 879 | else
|
| 880 | *start = sizeof(*hop);
|
| 881 | if (offset_e)
|
| 882 | *end = offset_e + calipso_tlv_len(hop, offset_e);
|
| 883 | else
|
| 884 | *end = opt_len;
|
| 885 |
|
| 886 | return ret_val;
|
| 887 | }
|
| 888 |
|
| 889 | |
| 890 | * calipso_opt_insert - Inserts a CALIPSO option into an IPv6 hop opt hdr
|
| 891 | * @hop: the original hop options header
|
| 892 | * @doi_def: the CALIPSO DOI to use
|
| 893 | * @secattr: the specific security attributes of the socket
|
| 894 | *
|
| 895 | * Description:
|
| 896 | * Creates a new hop options header based on @hop with a
|
| 897 | * CALIPSO option added to it. If @hop already contains a CALIPSO
|
| 898 | * option this is overwritten, otherwise the new option is appended
|
| 899 | * after any existing options. If @hop is NULL then the new header
|
| 900 | * will contain just the CALIPSO option and any needed padding.
|
| 901 | *
|
| 902 |
|
| 903 | static struct ipv6_opt_hdr *
|
| 904 | calipso_opt_insert(struct ipv6_opt_hdr *hop,
|
| 905 | const struct calipso_doi *doi_def,
|
| 906 | const struct netlbl_lsm_secattr *secattr)
|
| 907 | {
|
| 908 | unsigned int start, end, buf_len, pad, hop_len;
|
| 909 | struct ipv6_opt_hdr *new;
|
| 910 | int ret_val;
|
| 911 |
|
| 912 | if (hop) {
|
| 913 | hop_len = ipv6_optlen(hop);
|
| 914 | ret_val = calipso_opt_find(hop, &start, &end);
|
| 915 | if (ret_val && ret_val != -ENOENT)
|
| 916 | return ERR_PTR(ret_val);
|
| 917 | } else {
|
| 918 | hop_len = 0;
|
| 919 | start = sizeof(*hop);
|
| 920 | end = 0;
|
| 921 | }
|
| 922 |
|
| 923 | buf_len = hop_len + start - end + CALIPSO_OPT_LEN_MAX_WITH_PAD;
|
| 924 | new = kzalloc(buf_len, GFP_ATOMIC);
|
| 925 | if (!new)
|
| 926 | return ERR_PTR(-ENOMEM);
|
| 927 |
|
| 928 | if (start > sizeof(*hop))
|
| 929 | memcpy(new, hop, start);
|
| 930 | ret_val = calipso_genopt((unsigned char *)new, start, buf_len, doi_def,
|
| 931 | secattr);
|
| 932 | if (ret_val < 0) {
|
| 933 | kfree(new);
|
| 934 | return ERR_PTR(ret_val);
|
| 935 | }
|
| 936 |
|
| 937 | buf_len = start + ret_val;
|
| 938 |
|
| 939 | pad = ((buf_len & 4) + (end & 7)) & 7;
|
| 940 | calipso_pad_write((unsigned char *)new, buf_len, pad);
|
| 941 | buf_len += pad;
|
| 942 |
|
| 943 | if (end != hop_len) {
|
| 944 | memcpy((char *)new + buf_len, (char *)hop + end, hop_len - end);
|
| 945 | buf_len += hop_len - end;
|
| 946 | }
|
| 947 | new->nexthdr = 0;
|
| 948 | new->hdrlen = buf_len / 8 - 1;
|
| 949 |
|
| 950 | return new;
|
| 951 | }
|
| 952 |
|
| 953 | |
| 954 | * calipso_opt_del - Removes the CALIPSO option from an option header
|
| 955 | * @hop: the original header
|
| 956 | * @new: the new header
|
| 957 | *
|
| 958 | * Description:
|
| 959 | * Creates a new header based on @hop without any CALIPSO option. If @hop
|
| 960 | * doesn't contain a CALIPSO option it returns -ENOENT. If @hop contains
|
| 961 | * no other non-padding options, it returns zero with @new set to NULL.
|
| 962 | * Otherwise it returns zero, creates a new header without the CALIPSO
|
| 963 | * option (and removing as much padding as possible) and returns with
|
| 964 | * @new set to that header.
|
| 965 | *
|
| 966 |
|
| 967 | static int calipso_opt_del(struct ipv6_opt_hdr *hop,
|
| 968 | struct ipv6_opt_hdr **new)
|
| 969 | {
|
| 970 | int ret_val;
|
| 971 | unsigned int start, end, delta, pad, hop_len;
|
| 972 |
|
| 973 | ret_val = calipso_opt_find(hop, &start, &end);
|
| 974 | if (ret_val)
|
| 975 | return ret_val;
|
| 976 |
|
| 977 | hop_len = ipv6_optlen(hop);
|
| 978 | if (start == sizeof(*hop) && end == hop_len) {
|
| 979 |
|
| 980 | *new = NULL;
|
| 981 | return 0;
|
| 982 | }
|
| 983 |
|
| 984 | delta = (end - start) & ~7;
|
| 985 | *new = kzalloc(hop_len - delta, GFP_ATOMIC);
|
| 986 | if (!*new)
|
| 987 | return -ENOMEM;
|
| 988 |
|
| 989 | memcpy(*new, hop, start);
|
| 990 | (*new)->hdrlen -= delta / 8;
|
| 991 | pad = (end - start) & 7;
|
| 992 | calipso_pad_write((unsigned char *)*new, start, pad);
|
| 993 | if (end != hop_len)
|
| 994 | memcpy((char *)*new + start + pad, (char *)hop + end,
|
| 995 | hop_len - end);
|
| 996 |
|
| 997 | return 0;
|
| 998 | }
|
| 999 |
|
| 1000 | |
| 1001 | * calipso_opt_getattr - Get the security attributes from a memory block
|
| 1002 | * @calipso: the CALIPSO option
|
| 1003 | * @secattr: the security attributes
|
| 1004 | *
|
| 1005 | * Description:
|
| 1006 | * Inspect @calipso and return the security attributes in @secattr.
|
| 1007 | * Returns zero on success and negative values on failure.
|
| 1008 | *
|
| 1009 |
|
| 1010 | static int calipso_opt_getattr(const unsigned char *calipso,
|
| 1011 | struct netlbl_lsm_secattr *secattr)
|
| 1012 | {
|
| 1013 | int ret_val = -ENOMSG;
|
| 1014 | u32 doi, len = calipso[1], cat_len = calipso[6] * 4;
|
| 1015 | struct calipso_doi *doi_def;
|
| 1016 |
|
| 1017 | if (cat_len + 8 > len)
|
| 1018 | return -EINVAL;
|
| 1019 |
|
| 1020 | if (calipso_cache_check(calipso + 2, calipso[1], secattr) == 0)
|
| 1021 | return 0;
|
| 1022 |
|
| 1023 | doi = get_unaligned_be32(calipso + 2);
|
| 1024 | rcu_read_lock();
|
| 1025 | doi_def = calipso_doi_search(doi);
|
| 1026 | if (!doi_def)
|
| 1027 | goto getattr_return;
|
| 1028 |
|
| 1029 | secattr->attr.mls.lvl = calipso[7];
|
| 1030 | secattr->flags |= NETLBL_SECATTR_MLS_LVL;
|
| 1031 |
|
| 1032 | if (cat_len) {
|
| 1033 | ret_val = calipso_map_cat_ntoh(doi_def,
|
| 1034 | calipso + 10,
|
| 1035 | cat_len,
|
| 1036 | secattr);
|
| 1037 | if (ret_val != 0) {
|
| 1038 | netlbl_catmap_free(secattr->attr.mls.cat);
|
| 1039 | goto getattr_return;
|
| 1040 | }
|
| 1041 |
|
| 1042 | if (secattr->attr.mls.cat)
|
| 1043 | secattr->flags |= NETLBL_SECATTR_MLS_CAT;
|
| 1044 | }
|
| 1045 |
|
| 1046 | secattr->type = NETLBL_NLTYPE_CALIPSO;
|
| 1047 |
|
| 1048 | getattr_return:
|
| 1049 | rcu_read_unlock();
|
| 1050 | return ret_val;
|
| 1051 | }
|
| 1052 |
|
| 1053 | |
| 1054 |
|
| 1055 |
|
| 1056 | |
| 1057 | * calipso_sock_getattr - Get the security attributes from a sock
|
| 1058 | * @sk: the sock
|
| 1059 | * @secattr: the security attributes
|
| 1060 | *
|
| 1061 | * Description:
|
| 1062 | * Query @sk to see if there is a CALIPSO option attached to the sock and if
|
| 1063 | * there is return the CALIPSO security attributes in @secattr. This function
|
| 1064 | * requires that @sk be locked, or privately held, but it does not do any
|
| 1065 | * locking itself. Returns zero on success and negative values on failure.
|
| 1066 | *
|
| 1067 |
|
| 1068 | static int calipso_sock_getattr(struct sock *sk,
|
| 1069 | struct netlbl_lsm_secattr *secattr)
|
| 1070 | {
|
| 1071 | struct ipv6_opt_hdr *hop;
|
| 1072 | int opt_len, len, ret_val = -ENOMSG, offset;
|
| 1073 | unsigned char *opt;
|
| 1074 | struct ipv6_pinfo *pinfo = inet6_sk(sk);
|
| 1075 | struct ipv6_txoptions *txopts;
|
| 1076 |
|
| 1077 | if (!pinfo)
|
| 1078 | return -EAFNOSUPPORT;
|
| 1079 |
|
| 1080 | txopts = txopt_get(pinfo);
|
| 1081 | if (!txopts || !txopts->hopopt)
|
| 1082 | goto done;
|
| 1083 |
|
| 1084 | hop = txopts->hopopt;
|
| 1085 | opt = (unsigned char *)hop;
|
| 1086 | opt_len = ipv6_optlen(hop);
|
| 1087 | offset = sizeof(*hop);
|
| 1088 | while (offset < opt_len) {
|
| 1089 | len = calipso_tlv_len(hop, offset);
|
| 1090 | if (len < 0) {
|
| 1091 | ret_val = len;
|
| 1092 | goto done;
|
| 1093 | }
|
| 1094 | switch (opt[offset]) {
|
| 1095 | case IPV6_TLV_CALIPSO:
|
| 1096 | if (len < CALIPSO_HDR_LEN)
|
| 1097 | ret_val = -EINVAL;
|
| 1098 | else
|
| 1099 | ret_val = calipso_opt_getattr(&opt[offset],
|
| 1100 | secattr);
|
| 1101 | goto done;
|
| 1102 | default:
|
| 1103 | offset += len;
|
| 1104 | break;
|
| 1105 | }
|
| 1106 | }
|
| 1107 | done:
|
| 1108 | txopt_put(txopts);
|
| 1109 | return ret_val;
|
| 1110 | }
|
| 1111 |
|
| 1112 | |
| 1113 | * calipso_sock_setattr - Add a CALIPSO option to a socket
|
| 1114 | * @sk: the socket
|
| 1115 | * @doi_def: the CALIPSO DOI to use
|
| 1116 | * @secattr: the specific security attributes of the socket
|
| 1117 | *
|
| 1118 | * Description:
|
| 1119 | * Set the CALIPSO option on the given socket using the DOI definition and
|
| 1120 | * security attributes passed to the function. This function requires
|
| 1121 | * exclusive access to @sk, which means it either needs to be in the
|
| 1122 | * process of being created or locked. Returns zero on success and negative
|
| 1123 | * values on failure.
|
| 1124 | *
|
| 1125 |
|
| 1126 | static int calipso_sock_setattr(struct sock *sk,
|
| 1127 | const struct calipso_doi *doi_def,
|
| 1128 | const struct netlbl_lsm_secattr *secattr)
|
| 1129 | {
|
| 1130 | int ret_val;
|
| 1131 | struct ipv6_opt_hdr *old, *new;
|
| 1132 | struct ipv6_pinfo *pinfo = inet6_sk(sk);
|
| 1133 | struct ipv6_txoptions *txopts;
|
| 1134 |
|
| 1135 | if (!pinfo)
|
| 1136 | return -EAFNOSUPPORT;
|
| 1137 |
|
| 1138 | txopts = txopt_get(pinfo);
|
| 1139 | old = NULL;
|
| 1140 | if (txopts)
|
| 1141 | old = txopts->hopopt;
|
| 1142 |
|
| 1143 | new = calipso_opt_insert(old, doi_def, secattr);
|
| 1144 | txopt_put(txopts);
|
| 1145 | if (IS_ERR(new))
|
| 1146 | return PTR_ERR(new);
|
| 1147 |
|
| 1148 | ret_val = calipso_opt_update(sk, new);
|
| 1149 |
|
| 1150 | kfree(new);
|
| 1151 | return ret_val;
|
| 1152 | }
|
| 1153 |
|
| 1154 | |
| 1155 | * calipso_sock_delattr - Delete the CALIPSO option from a socket
|
| 1156 | * @sk: the socket
|
| 1157 | *
|
| 1158 | * Description:
|
| 1159 | * Removes the CALIPSO option from a socket, if present.
|
| 1160 | *
|
| 1161 |
|
| 1162 | static void calipso_sock_delattr(struct sock *sk)
|
| 1163 | {
|
| 1164 | struct ipv6_opt_hdr *new_hop;
|
| 1165 | struct ipv6_pinfo *pinfo = inet6_sk(sk);
|
| 1166 | struct ipv6_txoptions *txopts;
|
| 1167 |
|
| 1168 | if (!pinfo)
|
| 1169 | return;
|
| 1170 |
|
| 1171 | txopts = txopt_get(pinfo);
|
| 1172 | if (!txopts || !txopts->hopopt)
|
| 1173 | goto done;
|
| 1174 |
|
| 1175 | if (calipso_opt_del(txopts->hopopt, &new_hop))
|
| 1176 | goto done;
|
| 1177 |
|
| 1178 | calipso_opt_update(sk, new_hop);
|
| 1179 | kfree(new_hop);
|
| 1180 |
|
| 1181 | done:
|
| 1182 | txopt_put(txopts);
|
| 1183 | }
|
| 1184 |
|
| 1185 | |
| 1186 |
|
| 1187 |
|
| 1188 | |
| 1189 | * calipso_req_setattr - Add a CALIPSO option to a connection request socket
|
| 1190 | * @req: the connection request socket
|
| 1191 | * @doi_def: the CALIPSO DOI to use
|
| 1192 | * @secattr: the specific security attributes of the socket
|
| 1193 | *
|
| 1194 | * Description:
|
| 1195 | * Set the CALIPSO option on the given socket using the DOI definition and
|
| 1196 | * security attributes passed to the function. Returns zero on success and
|
| 1197 | * negative values on failure.
|
| 1198 | *
|
| 1199 |
|
| 1200 | static int calipso_req_setattr(struct request_sock *req,
|
| 1201 | const struct calipso_doi *doi_def,
|
| 1202 | const struct netlbl_lsm_secattr *secattr)
|
| 1203 | {
|
| 1204 | struct ipv6_txoptions *txopts;
|
| 1205 | struct inet_request_sock *req_inet = inet_rsk(req);
|
| 1206 | struct ipv6_opt_hdr *old, *new;
|
| 1207 | struct sock *sk = sk_to_full_sk(req_to_sk(req));
|
| 1208 |
|
| 1209 |
|
| 1210 | if (!sk)
|
| 1211 | return -ENOMEM;
|
| 1212 |
|
| 1213 | if (req_inet->ipv6_opt && req_inet->ipv6_opt->hopopt)
|
| 1214 | old = req_inet->ipv6_opt->hopopt;
|
| 1215 | else
|
| 1216 | old = NULL;
|
| 1217 |
|
| 1218 | new = calipso_opt_insert(old, doi_def, secattr);
|
| 1219 | if (IS_ERR(new))
|
| 1220 | return PTR_ERR(new);
|
| 1221 |
|
| 1222 | txopts = ipv6_renew_options(sk, req_inet->ipv6_opt, IPV6_HOPOPTS, new);
|
| 1223 |
|
| 1224 | kfree(new);
|
| 1225 |
|
| 1226 | if (IS_ERR(txopts))
|
| 1227 | return PTR_ERR(txopts);
|
| 1228 |
|
| 1229 | txopts = xchg(&req_inet->ipv6_opt, txopts);
|
| 1230 | if (txopts) {
|
| 1231 | atomic_sub(txopts->tot_len, &sk->sk_omem_alloc);
|
| 1232 | txopt_put(txopts);
|
| 1233 | }
|
| 1234 |
|
| 1235 | return 0;
|
| 1236 | }
|
| 1237 |
|
| 1238 | |
| 1239 | * calipso_req_delattr - Delete the CALIPSO option from a request socket
|
| 1240 | * @req: the request socket
|
| 1241 | *
|
| 1242 | * Description:
|
| 1243 | * Removes the CALIPSO option from a request socket, if present.
|
| 1244 | *
|
| 1245 |
|
| 1246 | static void calipso_req_delattr(struct request_sock *req)
|
| 1247 | {
|
| 1248 | struct inet_request_sock *req_inet = inet_rsk(req);
|
| 1249 | struct ipv6_opt_hdr *new;
|
| 1250 | struct ipv6_txoptions *txopts;
|
| 1251 | struct sock *sk = sk_to_full_sk(req_to_sk(req));
|
| 1252 |
|
| 1253 |
|
| 1254 | if (!sk)
|
| 1255 | return;
|
| 1256 |
|
| 1257 | if (!req_inet->ipv6_opt || !req_inet->ipv6_opt->hopopt)
|
| 1258 | return;
|
| 1259 |
|
| 1260 | if (calipso_opt_del(req_inet->ipv6_opt->hopopt, &new))
|
| 1261 | return;
|
| 1262 |
|
| 1263 | txopts = ipv6_renew_options(sk, req_inet->ipv6_opt, IPV6_HOPOPTS, new);
|
| 1264 |
|
| 1265 | if (!IS_ERR(txopts)) {
|
| 1266 | txopts = xchg(&req_inet->ipv6_opt, txopts);
|
| 1267 | if (txopts) {
|
| 1268 | atomic_sub(txopts->tot_len, &sk->sk_omem_alloc);
|
| 1269 | txopt_put(txopts);
|
| 1270 | }
|
| 1271 | }
|
| 1272 | kfree(new);
|
| 1273 | }
|
| 1274 |
|
| 1275 | |
| 1276 |
|
| 1277 |
|
| 1278 | |
| 1279 | * calipso_skbuff_optptr - Find the CALIPSO option in the packet
|
| 1280 | * @skb: the packet
|
| 1281 | *
|
| 1282 | * Description:
|
| 1283 | * Parse the packet's IP header looking for a CALIPSO option. Returns a pointer
|
| 1284 | * to the start of the CALIPSO option on success, NULL if one if not found.
|
| 1285 | *
|
| 1286 |
|
| 1287 | static unsigned char *calipso_skbuff_optptr(const struct sk_buff *skb)
|
| 1288 | {
|
| 1289 | const struct ipv6hdr *ip6_hdr = ipv6_hdr(skb);
|
| 1290 | int offset;
|
| 1291 |
|
| 1292 | if (ip6_hdr->nexthdr != NEXTHDR_HOP)
|
| 1293 | return NULL;
|
| 1294 |
|
| 1295 | offset = ipv6_find_tlv(skb, sizeof(*ip6_hdr), IPV6_TLV_CALIPSO);
|
| 1296 | if (offset >= 0)
|
| 1297 | return (unsigned char *)ip6_hdr + offset;
|
| 1298 |
|
| 1299 | return NULL;
|
| 1300 | }
|
| 1301 |
|
| 1302 | |
| 1303 | * calipso_skbuff_setattr - Set the CALIPSO option on a packet
|
| 1304 | * @skb: the packet
|
| 1305 | * @doi_def: the CALIPSO DOI to use
|
| 1306 | * @secattr: the security attributes
|
| 1307 | *
|
| 1308 | * Description:
|
| 1309 | * Set the CALIPSO option on the given packet based on the security attributes.
|
| 1310 | * Returns a pointer to the IP header on success and NULL on failure.
|
| 1311 | *
|
| 1312 |
|
| 1313 | static int calipso_skbuff_setattr(struct sk_buff *skb,
|
| 1314 | const struct calipso_doi *doi_def,
|
| 1315 | const struct netlbl_lsm_secattr *secattr)
|
| 1316 | {
|
| 1317 | int ret_val;
|
| 1318 | struct ipv6hdr *ip6_hdr;
|
| 1319 | struct ipv6_opt_hdr *hop;
|
| 1320 | unsigned char buf[CALIPSO_MAX_BUFFER];
|
| 1321 | int len_delta, new_end, pad;
|
| 1322 | unsigned int start, end;
|
| 1323 |
|
| 1324 | ip6_hdr = ipv6_hdr(skb);
|
| 1325 | if (ip6_hdr->nexthdr == NEXTHDR_HOP) {
|
| 1326 | hop = (struct ipv6_opt_hdr *)(ip6_hdr + 1);
|
| 1327 | ret_val = calipso_opt_find(hop, &start, &end);
|
| 1328 | if (ret_val && ret_val != -ENOENT)
|
| 1329 | return ret_val;
|
| 1330 | } else {
|
| 1331 | start = 0;
|
| 1332 | end = 0;
|
| 1333 | }
|
| 1334 |
|
| 1335 | memset(buf, 0, sizeof(buf));
|
| 1336 | ret_val = calipso_genopt(buf, start & 3, sizeof(buf), doi_def, secattr);
|
| 1337 | if (ret_val < 0)
|
| 1338 | return ret_val;
|
| 1339 |
|
| 1340 | new_end = start + ret_val;
|
| 1341 |
|
| 1342 | pad = ((new_end & 4) + (end & 7)) & 7;
|
| 1343 | len_delta = new_end - (int)end + pad;
|
| 1344 | ret_val = skb_cow(skb,
|
| 1345 | skb_headroom(skb) + (len_delta > 0 ? len_delta : 0));
|
| 1346 | if (ret_val < 0)
|
| 1347 | return ret_val;
|
| 1348 |
|
| 1349 | ip6_hdr = ipv6_hdr(skb);
|
| 1350 |
|
| 1351 | if (len_delta) {
|
| 1352 | if (len_delta > 0)
|
| 1353 | skb_push(skb, len_delta);
|
| 1354 | else
|
| 1355 | skb_pull(skb, -len_delta);
|
| 1356 | memmove((char *)ip6_hdr - len_delta, ip6_hdr,
|
| 1357 | sizeof(*ip6_hdr) + start);
|
| 1358 | skb_reset_network_header(skb);
|
| 1359 | ip6_hdr = ipv6_hdr(skb);
|
| 1360 |
|
| 1361 | ipv6_set_payload_len(ip6_hdr, skb->len - sizeof(*ip6_hdr));
|
| 1362 | }
|
| 1363 |
|
| 1364 | hop = (struct ipv6_opt_hdr *)(ip6_hdr + 1);
|
| 1365 | if (start == 0) {
|
| 1366 | struct ipv6_opt_hdr *new_hop = (struct ipv6_opt_hdr *)buf;
|
| 1367 |
|
| 1368 | new_hop->nexthdr = ip6_hdr->nexthdr;
|
| 1369 | new_hop->hdrlen = len_delta / 8 - 1;
|
| 1370 | ip6_hdr->nexthdr = NEXTHDR_HOP;
|
| 1371 | } else {
|
| 1372 | hop->hdrlen += len_delta / 8;
|
| 1373 | }
|
| 1374 | memcpy((char *)hop + start, buf + (start & 3), new_end - start);
|
| 1375 | calipso_pad_write((unsigned char *)hop, new_end, pad);
|
| 1376 |
|
| 1377 | return 0;
|
| 1378 | }
|
| 1379 |
|
| 1380 | |
| 1381 | * calipso_skbuff_delattr - Delete any CALIPSO options from a packet
|
| 1382 | * @skb: the packet
|
| 1383 | *
|
| 1384 | * Description:
|
| 1385 | * Removes any and all CALIPSO options from the given packet. Returns zero on
|
| 1386 | * success, negative values on failure.
|
| 1387 | *
|
| 1388 |
|
| 1389 | static int calipso_skbuff_delattr(struct sk_buff *skb)
|
| 1390 | {
|
| 1391 | int ret_val;
|
| 1392 | struct ipv6hdr *ip6_hdr;
|
| 1393 | struct ipv6_opt_hdr *old_hop;
|
| 1394 | u32 old_hop_len, start = 0, end = 0, delta, size, pad;
|
| 1395 |
|
| 1396 | if (!calipso_skbuff_optptr(skb))
|
| 1397 | return 0;
|
| 1398 |
|
| 1399 |
|
| 1400 | ret_val = skb_cow(skb, skb_headroom(skb));
|
| 1401 | if (ret_val < 0)
|
| 1402 | return ret_val;
|
| 1403 |
|
| 1404 | ip6_hdr = ipv6_hdr(skb);
|
| 1405 | old_hop = (struct ipv6_opt_hdr *)(ip6_hdr + 1);
|
| 1406 | old_hop_len = ipv6_optlen(old_hop);
|
| 1407 |
|
| 1408 | ret_val = calipso_opt_find(old_hop, &start, &end);
|
| 1409 | if (ret_val)
|
| 1410 | return ret_val;
|
| 1411 |
|
| 1412 | if (start == sizeof(*old_hop) && end == old_hop_len) {
|
| 1413 | |
| 1414 | * the whole thing.
|
| 1415 | delta = old_hop_len;
|
| 1416 | size = sizeof(*ip6_hdr);
|
| 1417 | ip6_hdr->nexthdr = old_hop->nexthdr;
|
| 1418 | } else {
|
| 1419 | delta = (end - start) & ~7;
|
| 1420 | if (delta)
|
| 1421 | old_hop->hdrlen -= delta / 8;
|
| 1422 | pad = (end - start) & 7;
|
| 1423 | size = sizeof(*ip6_hdr) + start + pad;
|
| 1424 | calipso_pad_write((unsigned char *)old_hop, start, pad);
|
| 1425 | }
|
| 1426 |
|
| 1427 | if (delta) {
|
| 1428 | skb_pull(skb, delta);
|
| 1429 | memmove((char *)ip6_hdr + delta, ip6_hdr, size);
|
| 1430 | skb_reset_network_header(skb);
|
| 1431 | }
|
| 1432 |
|
| 1433 | return 0;
|
| 1434 | }
|
| 1435 |
|
| 1436 | static const struct netlbl_calipso_ops ops = {
|
| 1437 | .doi_add = calipso_doi_add,
|
| 1438 | .doi_free = calipso_doi_free,
|
| 1439 | .doi_remove = calipso_doi_remove,
|
| 1440 | .doi_getdef = calipso_doi_getdef,
|
| 1441 | .doi_putdef = calipso_doi_putdef,
|
| 1442 | .doi_walk = calipso_doi_walk,
|
| 1443 | .sock_getattr = calipso_sock_getattr,
|
| 1444 | .sock_setattr = calipso_sock_setattr,
|
| 1445 | .sock_delattr = calipso_sock_delattr,
|
| 1446 | .req_setattr = calipso_req_setattr,
|
| 1447 | .req_delattr = calipso_req_delattr,
|
| 1448 | .opt_getattr = calipso_opt_getattr,
|
| 1449 | .skbuff_optptr = calipso_skbuff_optptr,
|
| 1450 | .skbuff_setattr = calipso_skbuff_setattr,
|
| 1451 | .skbuff_delattr = calipso_skbuff_delattr,
|
| 1452 | .cache_invalidate = calipso_cache_invalidate,
|
| 1453 | .cache_add = calipso_cache_add
|
| 1454 | };
|
| 1455 |
|
| 1456 | |
| 1457 | * calipso_init - Initialize the CALIPSO module
|
| 1458 | *
|
| 1459 | * Description:
|
| 1460 | * Initialize the CALIPSO module and prepare it for use. Returns zero on
|
| 1461 | * success and negative values on failure.
|
| 1462 | *
|
| 1463 |
|
| 1464 | int __init calipso_init(void)
|
| 1465 | {
|
| 1466 | int ret_val;
|
| 1467 |
|
| 1468 | ret_val = calipso_cache_init();
|
| 1469 | if (!ret_val)
|
| 1470 | netlbl_calipso_ops_register(&ops);
|
| 1471 | return ret_val;
|
| 1472 | }
|
| 1473 |
|
| 1474 | void calipso_exit(void)
|
| 1475 | {
|
| 1476 | netlbl_calipso_ops_register(NULL);
|
| 1477 | calipso_cache_invalidate();
|
| 1478 | kfree(calipso_cache);
|
| 1479 | }
|