1446fda4fSPaul Moore /* 2446fda4fSPaul Moore * CIPSO - Commercial IP Security Option 3446fda4fSPaul Moore * 4446fda4fSPaul Moore * This is an implementation of the CIPSO 2.2 protocol as specified in 5446fda4fSPaul Moore * draft-ietf-cipso-ipsecurity-01.txt with additional tag types as found in 6586c2500SPaul Moore * FIPS-188. While CIPSO never became a full IETF RFC standard many vendors 7446fda4fSPaul Moore * have chosen to adopt the protocol and over the years it has become a 8446fda4fSPaul Moore * de-facto standard for labeled networking. 9446fda4fSPaul Moore * 10586c2500SPaul Moore * The CIPSO draft specification can be found in the kernel's Documentation 11586c2500SPaul Moore * directory as well as the following URL: 12631dd1a8SJustin P. Mattock * http://tools.ietf.org/id/draft-ietf-cipso-ipsecurity-01.txt 13586c2500SPaul Moore * The FIPS-188 specification can be found at the following URL: 14586c2500SPaul Moore * http://www.itl.nist.gov/fipspubs/fip188.htm 15586c2500SPaul Moore * 16446fda4fSPaul Moore * Author: Paul Moore <paul.moore@hp.com> 17446fda4fSPaul Moore * 18446fda4fSPaul Moore */ 19446fda4fSPaul Moore 20446fda4fSPaul Moore /* 21948bf85cSPaul Moore * (c) Copyright Hewlett-Packard Development Company, L.P., 2006, 2008 22446fda4fSPaul Moore * 23446fda4fSPaul Moore * This program is free software; you can redistribute it and/or modify 24446fda4fSPaul Moore * it under the terms of the GNU General Public License as published by 25446fda4fSPaul Moore * the Free Software Foundation; either version 2 of the License, or 26446fda4fSPaul Moore * (at your option) any later version. 27446fda4fSPaul Moore * 28446fda4fSPaul Moore * This program is distributed in the hope that it will be useful, 29446fda4fSPaul Moore * but WITHOUT ANY WARRANTY; without even the implied warranty of 30446fda4fSPaul Moore * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See 31446fda4fSPaul Moore * the GNU General Public License for more details. 32446fda4fSPaul Moore * 33446fda4fSPaul Moore * You should have received a copy of the GNU General Public License 34*a99421d9SJeff Kirsher * along with this program; if not, see <http://www.gnu.org/licenses/>. 35446fda4fSPaul Moore * 36446fda4fSPaul Moore */ 37446fda4fSPaul Moore 38446fda4fSPaul Moore #include <linux/init.h> 39446fda4fSPaul Moore #include <linux/types.h> 40446fda4fSPaul Moore #include <linux/rcupdate.h> 41446fda4fSPaul Moore #include <linux/list.h> 42446fda4fSPaul Moore #include <linux/spinlock.h> 43446fda4fSPaul Moore #include <linux/string.h> 44446fda4fSPaul Moore #include <linux/jhash.h> 456c2e8ac0SPaul Moore #include <linux/audit.h> 465a0e3ad6STejun Heo #include <linux/slab.h> 47446fda4fSPaul Moore #include <net/ip.h> 48446fda4fSPaul Moore #include <net/icmp.h> 49446fda4fSPaul Moore #include <net/tcp.h> 50446fda4fSPaul Moore #include <net/netlabel.h> 51446fda4fSPaul Moore #include <net/cipso_ipv4.h> 5260063497SArun Sharma #include <linux/atomic.h> 53446fda4fSPaul Moore #include <asm/bug.h> 5450e5d35cSPaul Moore #include <asm/unaligned.h> 55446fda4fSPaul Moore 56446fda4fSPaul Moore /* List of available DOI definitions */ 57446fda4fSPaul Moore /* XXX - This currently assumes a minimal number of different DOIs in use, 58446fda4fSPaul Moore * if in practice there are a lot of different DOIs this list should 59446fda4fSPaul Moore * probably be turned into a hash table or something similar so we 60446fda4fSPaul Moore * can do quick lookups. */ 618ce11e6aSAdrian Bunk static DEFINE_SPINLOCK(cipso_v4_doi_list_lock); 621596c97aSDenis Cheng static LIST_HEAD(cipso_v4_doi_list); 63446fda4fSPaul Moore 64446fda4fSPaul Moore /* Label mapping cache */ 65446fda4fSPaul Moore int cipso_v4_cache_enabled = 1; 66446fda4fSPaul Moore int cipso_v4_cache_bucketsize = 10; 67446fda4fSPaul Moore #define CIPSO_V4_CACHE_BUCKETBITS 7 68446fda4fSPaul Moore #define CIPSO_V4_CACHE_BUCKETS (1 << CIPSO_V4_CACHE_BUCKETBITS) 69446fda4fSPaul Moore #define CIPSO_V4_CACHE_REORDERLIMIT 10 70446fda4fSPaul Moore struct cipso_v4_map_cache_bkt { 71446fda4fSPaul Moore spinlock_t lock; 72446fda4fSPaul Moore u32 size; 73446fda4fSPaul Moore struct list_head list; 74446fda4fSPaul Moore }; 75446fda4fSPaul Moore struct cipso_v4_map_cache_entry { 76446fda4fSPaul Moore u32 hash; 77446fda4fSPaul Moore unsigned char *key; 78446fda4fSPaul Moore size_t key_len; 79446fda4fSPaul Moore 80ffb733c6Spaul.moore@hp.com struct netlbl_lsm_cache *lsm_data; 81446fda4fSPaul Moore 82446fda4fSPaul Moore u32 activity; 83446fda4fSPaul Moore struct list_head list; 84446fda4fSPaul Moore }; 85446fda4fSPaul Moore static struct cipso_v4_map_cache_bkt *cipso_v4_cache = NULL; 86446fda4fSPaul Moore 87446fda4fSPaul Moore /* Restricted bitmap (tag #1) flags */ 88446fda4fSPaul Moore int cipso_v4_rbm_optfmt = 0; 89446fda4fSPaul Moore int cipso_v4_rbm_strictvalid = 1; 90446fda4fSPaul Moore 91446fda4fSPaul Moore /* 92f998e8cbSPaul Moore * Protocol Constants 93f998e8cbSPaul Moore */ 94f998e8cbSPaul Moore 95f998e8cbSPaul Moore /* Maximum size of the CIPSO IP option, derived from the fact that the maximum 96f998e8cbSPaul Moore * IPv4 header size is 60 bytes and the base IPv4 header is 20 bytes long. */ 97f998e8cbSPaul Moore #define CIPSO_V4_OPT_LEN_MAX 40 98f998e8cbSPaul Moore 99f998e8cbSPaul Moore /* Length of the base CIPSO option, this includes the option type (1 byte), the 100f998e8cbSPaul Moore * option length (1 byte), and the DOI (4 bytes). */ 101f998e8cbSPaul Moore #define CIPSO_V4_HDR_LEN 6 102f998e8cbSPaul Moore 103f998e8cbSPaul Moore /* Base length of the restrictive category bitmap tag (tag #1). */ 104f998e8cbSPaul Moore #define CIPSO_V4_TAG_RBM_BLEN 4 105f998e8cbSPaul Moore 106f998e8cbSPaul Moore /* Base length of the enumerated category tag (tag #2). */ 107f998e8cbSPaul Moore #define CIPSO_V4_TAG_ENUM_BLEN 4 108f998e8cbSPaul Moore 109f998e8cbSPaul Moore /* Base length of the ranged categories bitmap tag (tag #5). */ 110f998e8cbSPaul Moore #define CIPSO_V4_TAG_RNG_BLEN 4 111f998e8cbSPaul Moore /* The maximum number of category ranges permitted in the ranged category tag 112f998e8cbSPaul Moore * (tag #5). You may note that the IETF draft states that the maximum number 113f998e8cbSPaul Moore * of category ranges is 7, but if the low end of the last category range is 11425985edcSLucas De Marchi * zero then it is possible to fit 8 category ranges because the zero should 115f998e8cbSPaul Moore * be omitted. */ 116f998e8cbSPaul Moore #define CIPSO_V4_TAG_RNG_CAT_MAX 8 117f998e8cbSPaul Moore 11815c45f7bSPaul Moore /* Base length of the local tag (non-standard tag). 11915c45f7bSPaul Moore * Tag definition (may change between kernel versions) 12015c45f7bSPaul Moore * 12115c45f7bSPaul Moore * 0 8 16 24 32 12215c45f7bSPaul Moore * +----------+----------+----------+----------+ 12315c45f7bSPaul Moore * | 10000000 | 00000110 | 32-bit secid value | 12415c45f7bSPaul Moore * +----------+----------+----------+----------+ 12515c45f7bSPaul Moore * | in (host byte order)| 12615c45f7bSPaul Moore * +----------+----------+ 12715c45f7bSPaul Moore * 12815c45f7bSPaul Moore */ 12915c45f7bSPaul Moore #define CIPSO_V4_TAG_LOC_BLEN 6 13015c45f7bSPaul Moore 131f998e8cbSPaul Moore /* 132446fda4fSPaul Moore * Helper Functions 133446fda4fSPaul Moore */ 134446fda4fSPaul Moore 135446fda4fSPaul Moore /** 136446fda4fSPaul Moore * cipso_v4_bitmap_walk - Walk a bitmap looking for a bit 137446fda4fSPaul Moore * @bitmap: the bitmap 138446fda4fSPaul Moore * @bitmap_len: length in bits 139446fda4fSPaul Moore * @offset: starting offset 140446fda4fSPaul Moore * @state: if non-zero, look for a set (1) bit else look for a cleared (0) bit 141446fda4fSPaul Moore * 142446fda4fSPaul Moore * Description: 143446fda4fSPaul Moore * Starting at @offset, walk the bitmap from left to right until either the 144446fda4fSPaul Moore * desired bit is found or we reach the end. Return the bit offset, -1 if 145446fda4fSPaul Moore * not found, or -2 if error. 146446fda4fSPaul Moore */ 147446fda4fSPaul Moore static int cipso_v4_bitmap_walk(const unsigned char *bitmap, 148446fda4fSPaul Moore u32 bitmap_len, 149446fda4fSPaul Moore u32 offset, 150446fda4fSPaul Moore u8 state) 151446fda4fSPaul Moore { 152446fda4fSPaul Moore u32 bit_spot; 153446fda4fSPaul Moore u32 byte_offset; 154446fda4fSPaul Moore unsigned char bitmask; 155446fda4fSPaul Moore unsigned char byte; 156446fda4fSPaul Moore 157446fda4fSPaul Moore /* gcc always rounds to zero when doing integer division */ 158446fda4fSPaul Moore byte_offset = offset / 8; 159446fda4fSPaul Moore byte = bitmap[byte_offset]; 160446fda4fSPaul Moore bit_spot = offset; 161446fda4fSPaul Moore bitmask = 0x80 >> (offset % 8); 162446fda4fSPaul Moore 163446fda4fSPaul Moore while (bit_spot < bitmap_len) { 164446fda4fSPaul Moore if ((state && (byte & bitmask) == bitmask) || 165446fda4fSPaul Moore (state == 0 && (byte & bitmask) == 0)) 166446fda4fSPaul Moore return bit_spot; 167446fda4fSPaul Moore 168446fda4fSPaul Moore bit_spot++; 169446fda4fSPaul Moore bitmask >>= 1; 170446fda4fSPaul Moore if (bitmask == 0) { 171446fda4fSPaul Moore byte = bitmap[++byte_offset]; 172446fda4fSPaul Moore bitmask = 0x80; 173446fda4fSPaul Moore } 174446fda4fSPaul Moore } 175446fda4fSPaul Moore 176446fda4fSPaul Moore return -1; 177446fda4fSPaul Moore } 178446fda4fSPaul Moore 179446fda4fSPaul Moore /** 180446fda4fSPaul Moore * cipso_v4_bitmap_setbit - Sets a single bit in a bitmap 181446fda4fSPaul Moore * @bitmap: the bitmap 182446fda4fSPaul Moore * @bit: the bit 183446fda4fSPaul Moore * @state: if non-zero, set the bit (1) else clear the bit (0) 184446fda4fSPaul Moore * 185446fda4fSPaul Moore * Description: 186446fda4fSPaul Moore * Set a single bit in the bitmask. Returns zero on success, negative values 187446fda4fSPaul Moore * on error. 188446fda4fSPaul Moore */ 189446fda4fSPaul Moore static void cipso_v4_bitmap_setbit(unsigned char *bitmap, 190446fda4fSPaul Moore u32 bit, 191446fda4fSPaul Moore u8 state) 192446fda4fSPaul Moore { 193446fda4fSPaul Moore u32 byte_spot; 194446fda4fSPaul Moore u8 bitmask; 195446fda4fSPaul Moore 196446fda4fSPaul Moore /* gcc always rounds to zero when doing integer division */ 197446fda4fSPaul Moore byte_spot = bit / 8; 198446fda4fSPaul Moore bitmask = 0x80 >> (bit % 8); 199446fda4fSPaul Moore if (state) 200446fda4fSPaul Moore bitmap[byte_spot] |= bitmask; 201446fda4fSPaul Moore else 202446fda4fSPaul Moore bitmap[byte_spot] &= ~bitmask; 203446fda4fSPaul Moore } 204446fda4fSPaul Moore 205446fda4fSPaul Moore /** 206446fda4fSPaul Moore * cipso_v4_cache_entry_free - Frees a cache entry 207446fda4fSPaul Moore * @entry: the entry to free 208446fda4fSPaul Moore * 209446fda4fSPaul Moore * Description: 210ffb733c6Spaul.moore@hp.com * This function frees the memory associated with a cache entry including the 211ffb733c6Spaul.moore@hp.com * LSM cache data if there are no longer any users, i.e. reference count == 0. 212446fda4fSPaul Moore * 213446fda4fSPaul Moore */ 214446fda4fSPaul Moore static void cipso_v4_cache_entry_free(struct cipso_v4_map_cache_entry *entry) 215446fda4fSPaul Moore { 216ffb733c6Spaul.moore@hp.com if (entry->lsm_data) 217ffb733c6Spaul.moore@hp.com netlbl_secattr_cache_free(entry->lsm_data); 218446fda4fSPaul Moore kfree(entry->key); 219446fda4fSPaul Moore kfree(entry); 220446fda4fSPaul Moore } 221446fda4fSPaul Moore 222446fda4fSPaul Moore /** 223446fda4fSPaul Moore * cipso_v4_map_cache_hash - Hashing function for the CIPSO cache 224446fda4fSPaul Moore * @key: the hash key 225446fda4fSPaul Moore * @key_len: the length of the key in bytes 226446fda4fSPaul Moore * 227446fda4fSPaul Moore * Description: 228446fda4fSPaul Moore * The CIPSO tag hashing function. Returns a 32-bit hash value. 229446fda4fSPaul Moore * 230446fda4fSPaul Moore */ 231446fda4fSPaul Moore static u32 cipso_v4_map_cache_hash(const unsigned char *key, u32 key_len) 232446fda4fSPaul Moore { 233446fda4fSPaul Moore return jhash(key, key_len, 0); 234446fda4fSPaul Moore } 235446fda4fSPaul Moore 236446fda4fSPaul Moore /* 237446fda4fSPaul Moore * Label Mapping Cache Functions 238446fda4fSPaul Moore */ 239446fda4fSPaul Moore 240446fda4fSPaul Moore /** 241446fda4fSPaul Moore * cipso_v4_cache_init - Initialize the CIPSO cache 242446fda4fSPaul Moore * 243446fda4fSPaul Moore * Description: 244446fda4fSPaul Moore * Initializes the CIPSO label mapping cache, this function should be called 245446fda4fSPaul Moore * before any of the other functions defined in this file. Returns zero on 246446fda4fSPaul Moore * success, negative values on error. 247446fda4fSPaul Moore * 248446fda4fSPaul Moore */ 249446fda4fSPaul Moore static int cipso_v4_cache_init(void) 250446fda4fSPaul Moore { 251446fda4fSPaul Moore u32 iter; 252446fda4fSPaul Moore 253446fda4fSPaul Moore cipso_v4_cache = kcalloc(CIPSO_V4_CACHE_BUCKETS, 254446fda4fSPaul Moore sizeof(struct cipso_v4_map_cache_bkt), 255446fda4fSPaul Moore GFP_KERNEL); 256446fda4fSPaul Moore if (cipso_v4_cache == NULL) 257446fda4fSPaul Moore return -ENOMEM; 258446fda4fSPaul Moore 259446fda4fSPaul Moore for (iter = 0; iter < CIPSO_V4_CACHE_BUCKETS; iter++) { 260446fda4fSPaul Moore spin_lock_init(&cipso_v4_cache[iter].lock); 261446fda4fSPaul Moore cipso_v4_cache[iter].size = 0; 262446fda4fSPaul Moore INIT_LIST_HEAD(&cipso_v4_cache[iter].list); 263446fda4fSPaul Moore } 264446fda4fSPaul Moore 265446fda4fSPaul Moore return 0; 266446fda4fSPaul Moore } 267446fda4fSPaul Moore 268446fda4fSPaul Moore /** 269446fda4fSPaul Moore * cipso_v4_cache_invalidate - Invalidates the current CIPSO cache 270446fda4fSPaul Moore * 271446fda4fSPaul Moore * Description: 272446fda4fSPaul Moore * Invalidates and frees any entries in the CIPSO cache. Returns zero on 273446fda4fSPaul Moore * success and negative values on failure. 274446fda4fSPaul Moore * 275446fda4fSPaul Moore */ 276446fda4fSPaul Moore void cipso_v4_cache_invalidate(void) 277446fda4fSPaul Moore { 278446fda4fSPaul Moore struct cipso_v4_map_cache_entry *entry, *tmp_entry; 279446fda4fSPaul Moore u32 iter; 280446fda4fSPaul Moore 281446fda4fSPaul Moore for (iter = 0; iter < CIPSO_V4_CACHE_BUCKETS; iter++) { 282609c92feSPaul Moore spin_lock_bh(&cipso_v4_cache[iter].lock); 283446fda4fSPaul Moore list_for_each_entry_safe(entry, 284446fda4fSPaul Moore tmp_entry, 285446fda4fSPaul Moore &cipso_v4_cache[iter].list, list) { 286446fda4fSPaul Moore list_del(&entry->list); 287446fda4fSPaul Moore cipso_v4_cache_entry_free(entry); 288446fda4fSPaul Moore } 289446fda4fSPaul Moore cipso_v4_cache[iter].size = 0; 290609c92feSPaul Moore spin_unlock_bh(&cipso_v4_cache[iter].lock); 291446fda4fSPaul Moore } 292446fda4fSPaul Moore } 293446fda4fSPaul Moore 294446fda4fSPaul Moore /** 295446fda4fSPaul Moore * cipso_v4_cache_check - Check the CIPSO cache for a label mapping 296446fda4fSPaul Moore * @key: the buffer to check 297446fda4fSPaul Moore * @key_len: buffer length in bytes 298446fda4fSPaul Moore * @secattr: the security attribute struct to use 299446fda4fSPaul Moore * 300446fda4fSPaul Moore * Description: 301446fda4fSPaul Moore * This function checks the cache to see if a label mapping already exists for 302446fda4fSPaul Moore * the given key. If there is a match then the cache is adjusted and the 303446fda4fSPaul Moore * @secattr struct is populated with the correct LSM security attributes. The 304446fda4fSPaul Moore * cache is adjusted in the following manner if the entry is not already the 305446fda4fSPaul Moore * first in the cache bucket: 306446fda4fSPaul Moore * 307446fda4fSPaul Moore * 1. The cache entry's activity counter is incremented 308446fda4fSPaul Moore * 2. The previous (higher ranking) entry's activity counter is decremented 309446fda4fSPaul Moore * 3. If the difference between the two activity counters is geater than 310446fda4fSPaul Moore * CIPSO_V4_CACHE_REORDERLIMIT the two entries are swapped 311446fda4fSPaul Moore * 312446fda4fSPaul Moore * Returns zero on success, -ENOENT for a cache miss, and other negative values 313446fda4fSPaul Moore * on error. 314446fda4fSPaul Moore * 315446fda4fSPaul Moore */ 316446fda4fSPaul Moore static int cipso_v4_cache_check(const unsigned char *key, 317446fda4fSPaul Moore u32 key_len, 318446fda4fSPaul Moore struct netlbl_lsm_secattr *secattr) 319446fda4fSPaul Moore { 320446fda4fSPaul Moore u32 bkt; 321446fda4fSPaul Moore struct cipso_v4_map_cache_entry *entry; 322446fda4fSPaul Moore struct cipso_v4_map_cache_entry *prev_entry = NULL; 323446fda4fSPaul Moore u32 hash; 324446fda4fSPaul Moore 325446fda4fSPaul Moore if (!cipso_v4_cache_enabled) 326446fda4fSPaul Moore return -ENOENT; 327446fda4fSPaul Moore 328446fda4fSPaul Moore hash = cipso_v4_map_cache_hash(key, key_len); 3295e0f8923SPavel Emelyanov bkt = hash & (CIPSO_V4_CACHE_BUCKETS - 1); 330609c92feSPaul Moore spin_lock_bh(&cipso_v4_cache[bkt].lock); 331446fda4fSPaul Moore list_for_each_entry(entry, &cipso_v4_cache[bkt].list, list) { 332446fda4fSPaul Moore if (entry->hash == hash && 333446fda4fSPaul Moore entry->key_len == key_len && 334446fda4fSPaul Moore memcmp(entry->key, key, key_len) == 0) { 335446fda4fSPaul Moore entry->activity += 1; 336ffb733c6Spaul.moore@hp.com atomic_inc(&entry->lsm_data->refcount); 337ffb733c6Spaul.moore@hp.com secattr->cache = entry->lsm_data; 338701a90baSPaul Moore secattr->flags |= NETLBL_SECATTR_CACHE; 33916efd454SPaul Moore secattr->type = NETLBL_NLTYPE_CIPSOV4; 340446fda4fSPaul Moore if (prev_entry == NULL) { 341609c92feSPaul Moore spin_unlock_bh(&cipso_v4_cache[bkt].lock); 342446fda4fSPaul Moore return 0; 343446fda4fSPaul Moore } 344446fda4fSPaul Moore 345446fda4fSPaul Moore if (prev_entry->activity > 0) 346446fda4fSPaul Moore prev_entry->activity -= 1; 347446fda4fSPaul Moore if (entry->activity > prev_entry->activity && 348446fda4fSPaul Moore entry->activity - prev_entry->activity > 349446fda4fSPaul Moore CIPSO_V4_CACHE_REORDERLIMIT) { 350446fda4fSPaul Moore __list_del(entry->list.prev, entry->list.next); 351446fda4fSPaul Moore __list_add(&entry->list, 352446fda4fSPaul Moore prev_entry->list.prev, 353446fda4fSPaul Moore &prev_entry->list); 354446fda4fSPaul Moore } 355446fda4fSPaul Moore 356609c92feSPaul Moore spin_unlock_bh(&cipso_v4_cache[bkt].lock); 357446fda4fSPaul Moore return 0; 358446fda4fSPaul Moore } 359446fda4fSPaul Moore prev_entry = entry; 360446fda4fSPaul Moore } 361609c92feSPaul Moore spin_unlock_bh(&cipso_v4_cache[bkt].lock); 362446fda4fSPaul Moore 363446fda4fSPaul Moore return -ENOENT; 364446fda4fSPaul Moore } 365446fda4fSPaul Moore 366446fda4fSPaul Moore /** 367446fda4fSPaul Moore * cipso_v4_cache_add - Add an entry to the CIPSO cache 368446fda4fSPaul Moore * @skb: the packet 369446fda4fSPaul Moore * @secattr: the packet's security attributes 370446fda4fSPaul Moore * 371446fda4fSPaul Moore * Description: 372446fda4fSPaul Moore * Add a new entry into the CIPSO label mapping cache. Add the new entry to 373446fda4fSPaul Moore * head of the cache bucket's list, if the cache bucket is out of room remove 374446fda4fSPaul Moore * the last entry in the list first. It is important to note that there is 375446fda4fSPaul Moore * currently no checking for duplicate keys. Returns zero on success, 376446fda4fSPaul Moore * negative values on failure. 377446fda4fSPaul Moore * 378446fda4fSPaul Moore */ 379446fda4fSPaul Moore int cipso_v4_cache_add(const struct sk_buff *skb, 380446fda4fSPaul Moore const struct netlbl_lsm_secattr *secattr) 381446fda4fSPaul Moore { 382446fda4fSPaul Moore int ret_val = -EPERM; 383446fda4fSPaul Moore u32 bkt; 384446fda4fSPaul Moore struct cipso_v4_map_cache_entry *entry = NULL; 385446fda4fSPaul Moore struct cipso_v4_map_cache_entry *old_entry = NULL; 386446fda4fSPaul Moore unsigned char *cipso_ptr; 387446fda4fSPaul Moore u32 cipso_ptr_len; 388446fda4fSPaul Moore 389446fda4fSPaul Moore if (!cipso_v4_cache_enabled || cipso_v4_cache_bucketsize <= 0) 390446fda4fSPaul Moore return 0; 391446fda4fSPaul Moore 392446fda4fSPaul Moore cipso_ptr = CIPSO_V4_OPTPTR(skb); 393446fda4fSPaul Moore cipso_ptr_len = cipso_ptr[1]; 394446fda4fSPaul Moore 395446fda4fSPaul Moore entry = kzalloc(sizeof(*entry), GFP_ATOMIC); 396446fda4fSPaul Moore if (entry == NULL) 397446fda4fSPaul Moore return -ENOMEM; 398fac5d731SArnaldo Carvalho de Melo entry->key = kmemdup(cipso_ptr, cipso_ptr_len, GFP_ATOMIC); 399446fda4fSPaul Moore if (entry->key == NULL) { 400446fda4fSPaul Moore ret_val = -ENOMEM; 401446fda4fSPaul Moore goto cache_add_failure; 402446fda4fSPaul Moore } 403446fda4fSPaul Moore entry->key_len = cipso_ptr_len; 404446fda4fSPaul Moore entry->hash = cipso_v4_map_cache_hash(cipso_ptr, cipso_ptr_len); 405ffb733c6Spaul.moore@hp.com atomic_inc(&secattr->cache->refcount); 406ffb733c6Spaul.moore@hp.com entry->lsm_data = secattr->cache; 407446fda4fSPaul Moore 4085e0f8923SPavel Emelyanov bkt = entry->hash & (CIPSO_V4_CACHE_BUCKETS - 1); 409609c92feSPaul Moore spin_lock_bh(&cipso_v4_cache[bkt].lock); 410446fda4fSPaul Moore if (cipso_v4_cache[bkt].size < cipso_v4_cache_bucketsize) { 411446fda4fSPaul Moore list_add(&entry->list, &cipso_v4_cache[bkt].list); 412446fda4fSPaul Moore cipso_v4_cache[bkt].size += 1; 413446fda4fSPaul Moore } else { 414446fda4fSPaul Moore old_entry = list_entry(cipso_v4_cache[bkt].list.prev, 415446fda4fSPaul Moore struct cipso_v4_map_cache_entry, list); 416446fda4fSPaul Moore list_del(&old_entry->list); 417446fda4fSPaul Moore list_add(&entry->list, &cipso_v4_cache[bkt].list); 418446fda4fSPaul Moore cipso_v4_cache_entry_free(old_entry); 419446fda4fSPaul Moore } 420609c92feSPaul Moore spin_unlock_bh(&cipso_v4_cache[bkt].lock); 421446fda4fSPaul Moore 422446fda4fSPaul Moore return 0; 423446fda4fSPaul Moore 424446fda4fSPaul Moore cache_add_failure: 425446fda4fSPaul Moore if (entry) 426446fda4fSPaul Moore cipso_v4_cache_entry_free(entry); 427446fda4fSPaul Moore return ret_val; 428446fda4fSPaul Moore } 429446fda4fSPaul Moore 430446fda4fSPaul Moore /* 431446fda4fSPaul Moore * DOI List Functions 432446fda4fSPaul Moore */ 433446fda4fSPaul Moore 434446fda4fSPaul Moore /** 435446fda4fSPaul Moore * cipso_v4_doi_search - Searches for a DOI definition 436446fda4fSPaul Moore * @doi: the DOI to search for 437446fda4fSPaul Moore * 438446fda4fSPaul Moore * Description: 439446fda4fSPaul Moore * Search the DOI definition list for a DOI definition with a DOI value that 44025985edcSLucas De Marchi * matches @doi. The caller is responsible for calling rcu_read_[un]lock(). 441446fda4fSPaul Moore * Returns a pointer to the DOI definition on success and NULL on failure. 442446fda4fSPaul Moore */ 443446fda4fSPaul Moore static struct cipso_v4_doi *cipso_v4_doi_search(u32 doi) 444446fda4fSPaul Moore { 445446fda4fSPaul Moore struct cipso_v4_doi *iter; 446446fda4fSPaul Moore 447446fda4fSPaul Moore list_for_each_entry_rcu(iter, &cipso_v4_doi_list, list) 448b1edeb10SPaul Moore if (iter->doi == doi && atomic_read(&iter->refcount)) 449446fda4fSPaul Moore return iter; 450446fda4fSPaul Moore return NULL; 451446fda4fSPaul Moore } 452446fda4fSPaul Moore 453446fda4fSPaul Moore /** 454446fda4fSPaul Moore * cipso_v4_doi_add - Add a new DOI to the CIPSO protocol engine 455446fda4fSPaul Moore * @doi_def: the DOI structure 4566c2e8ac0SPaul Moore * @audit_info: NetLabel audit information 457446fda4fSPaul Moore * 458446fda4fSPaul Moore * Description: 459446fda4fSPaul Moore * The caller defines a new DOI for use by the CIPSO engine and calls this 460446fda4fSPaul Moore * function to add it to the list of acceptable domains. The caller must 461446fda4fSPaul Moore * ensure that the mapping table specified in @doi_def->map meets all of the 462446fda4fSPaul Moore * requirements of the mapping type (see cipso_ipv4.h for details). Returns 463446fda4fSPaul Moore * zero on success and non-zero on failure. 464446fda4fSPaul Moore * 465446fda4fSPaul Moore */ 4666c2e8ac0SPaul Moore int cipso_v4_doi_add(struct cipso_v4_doi *doi_def, 4676c2e8ac0SPaul Moore struct netlbl_audit *audit_info) 468446fda4fSPaul Moore { 4696c2e8ac0SPaul Moore int ret_val = -EINVAL; 4706ce61a7cSPaul Moore u32 iter; 4716c2e8ac0SPaul Moore u32 doi; 4726c2e8ac0SPaul Moore u32 doi_type; 4736c2e8ac0SPaul Moore struct audit_buffer *audit_buf; 4746c2e8ac0SPaul Moore 4756c2e8ac0SPaul Moore doi = doi_def->doi; 4766c2e8ac0SPaul Moore doi_type = doi_def->type; 4776ce61a7cSPaul Moore 47856755924SDan Carpenter if (doi_def->doi == CIPSO_V4_DOI_UNKNOWN) 4796c2e8ac0SPaul Moore goto doi_add_return; 4806ce61a7cSPaul Moore for (iter = 0; iter < CIPSO_V4_TAG_MAXCNT; iter++) { 4816ce61a7cSPaul Moore switch (doi_def->tags[iter]) { 4826ce61a7cSPaul Moore case CIPSO_V4_TAG_RBITMAP: 4836ce61a7cSPaul Moore break; 484484b3669SPaul Moore case CIPSO_V4_TAG_RANGE: 485654bbc2aSPaul Moore case CIPSO_V4_TAG_ENUM: 486654bbc2aSPaul Moore if (doi_def->type != CIPSO_V4_MAP_PASS) 4876c2e8ac0SPaul Moore goto doi_add_return; 488654bbc2aSPaul Moore break; 48915c45f7bSPaul Moore case CIPSO_V4_TAG_LOCAL: 49015c45f7bSPaul Moore if (doi_def->type != CIPSO_V4_MAP_LOCAL) 4916c2e8ac0SPaul Moore goto doi_add_return; 4926c2e8ac0SPaul Moore break; 4936c2e8ac0SPaul Moore case CIPSO_V4_TAG_INVALID: 4946c2e8ac0SPaul Moore if (iter == 0) 4956c2e8ac0SPaul Moore goto doi_add_return; 49615c45f7bSPaul Moore break; 4976ce61a7cSPaul Moore default: 4986c2e8ac0SPaul Moore goto doi_add_return; 4996ce61a7cSPaul Moore } 5006ce61a7cSPaul Moore } 501446fda4fSPaul Moore 502b1edeb10SPaul Moore atomic_set(&doi_def->refcount, 1); 503446fda4fSPaul Moore 504446fda4fSPaul Moore spin_lock(&cipso_v4_doi_list_lock); 5056c2e8ac0SPaul Moore if (cipso_v4_doi_search(doi_def->doi) != NULL) { 5066c2e8ac0SPaul Moore spin_unlock(&cipso_v4_doi_list_lock); 5076c2e8ac0SPaul Moore ret_val = -EEXIST; 5086c2e8ac0SPaul Moore goto doi_add_return; 5096c2e8ac0SPaul Moore } 510446fda4fSPaul Moore list_add_tail_rcu(&doi_def->list, &cipso_v4_doi_list); 511446fda4fSPaul Moore spin_unlock(&cipso_v4_doi_list_lock); 5126c2e8ac0SPaul Moore ret_val = 0; 513446fda4fSPaul Moore 5146c2e8ac0SPaul Moore doi_add_return: 5156c2e8ac0SPaul Moore audit_buf = netlbl_audit_start(AUDIT_MAC_CIPSOV4_ADD, audit_info); 5166c2e8ac0SPaul Moore if (audit_buf != NULL) { 5176c2e8ac0SPaul Moore const char *type_str; 5186c2e8ac0SPaul Moore switch (doi_type) { 5196c2e8ac0SPaul Moore case CIPSO_V4_MAP_TRANS: 5206c2e8ac0SPaul Moore type_str = "trans"; 5216c2e8ac0SPaul Moore break; 5226c2e8ac0SPaul Moore case CIPSO_V4_MAP_PASS: 5236c2e8ac0SPaul Moore type_str = "pass"; 5246c2e8ac0SPaul Moore break; 5256c2e8ac0SPaul Moore case CIPSO_V4_MAP_LOCAL: 5266c2e8ac0SPaul Moore type_str = "local"; 5276c2e8ac0SPaul Moore break; 5286c2e8ac0SPaul Moore default: 5296c2e8ac0SPaul Moore type_str = "(unknown)"; 5306c2e8ac0SPaul Moore } 5316c2e8ac0SPaul Moore audit_log_format(audit_buf, 5326c2e8ac0SPaul Moore " cipso_doi=%u cipso_type=%s res=%u", 5336c2e8ac0SPaul Moore doi, type_str, ret_val == 0 ? 1 : 0); 5346c2e8ac0SPaul Moore audit_log_end(audit_buf); 5356c2e8ac0SPaul Moore } 536446fda4fSPaul Moore 5376c2e8ac0SPaul Moore return ret_val; 538446fda4fSPaul Moore } 539446fda4fSPaul Moore 540446fda4fSPaul Moore /** 541b1edeb10SPaul Moore * cipso_v4_doi_free - Frees a DOI definition 542b1edeb10SPaul Moore * @entry: the entry's RCU field 543446fda4fSPaul Moore * 544446fda4fSPaul Moore * Description: 545b1edeb10SPaul Moore * This function frees all of the memory associated with a DOI definition. 546446fda4fSPaul Moore * 547446fda4fSPaul Moore */ 548b1edeb10SPaul Moore void cipso_v4_doi_free(struct cipso_v4_doi *doi_def) 549446fda4fSPaul Moore { 550b1edeb10SPaul Moore if (doi_def == NULL) 551b1edeb10SPaul Moore return; 552446fda4fSPaul Moore 553b1edeb10SPaul Moore switch (doi_def->type) { 55415c45f7bSPaul Moore case CIPSO_V4_MAP_TRANS: 555b1edeb10SPaul Moore kfree(doi_def->map.std->lvl.cipso); 556b1edeb10SPaul Moore kfree(doi_def->map.std->lvl.local); 557b1edeb10SPaul Moore kfree(doi_def->map.std->cat.cipso); 558b1edeb10SPaul Moore kfree(doi_def->map.std->cat.local); 559b1edeb10SPaul Moore break; 560446fda4fSPaul Moore } 561b1edeb10SPaul Moore kfree(doi_def); 562446fda4fSPaul Moore } 563446fda4fSPaul Moore 564446fda4fSPaul Moore /** 565b1edeb10SPaul Moore * cipso_v4_doi_free_rcu - Frees a DOI definition via the RCU pointer 566b1edeb10SPaul Moore * @entry: the entry's RCU field 567b1edeb10SPaul Moore * 568b1edeb10SPaul Moore * Description: 569b1edeb10SPaul Moore * This function is designed to be used as a callback to the call_rcu() 570b1edeb10SPaul Moore * function so that the memory allocated to the DOI definition can be released 571b1edeb10SPaul Moore * safely. 572b1edeb10SPaul Moore * 573b1edeb10SPaul Moore */ 574b1edeb10SPaul Moore static void cipso_v4_doi_free_rcu(struct rcu_head *entry) 575b1edeb10SPaul Moore { 576b1edeb10SPaul Moore struct cipso_v4_doi *doi_def; 577b1edeb10SPaul Moore 578b1edeb10SPaul Moore doi_def = container_of(entry, struct cipso_v4_doi, rcu); 579b1edeb10SPaul Moore cipso_v4_doi_free(doi_def); 580b1edeb10SPaul Moore } 581b1edeb10SPaul Moore 582b1edeb10SPaul Moore /** 583b1edeb10SPaul Moore * cipso_v4_doi_remove - Remove an existing DOI from the CIPSO protocol engine 584b1edeb10SPaul Moore * @doi: the DOI value 585b1edeb10SPaul Moore * @audit_secid: the LSM secid to use in the audit message 586b1edeb10SPaul Moore * 587b1edeb10SPaul Moore * Description: 588b1edeb10SPaul Moore * Removes a DOI definition from the CIPSO engine. The NetLabel routines will 589b1edeb10SPaul Moore * be called to release their own LSM domain mappings as well as our own 590b1edeb10SPaul Moore * domain list. Returns zero on success and negative values on failure. 591b1edeb10SPaul Moore * 592b1edeb10SPaul Moore */ 593b1edeb10SPaul Moore int cipso_v4_doi_remove(u32 doi, struct netlbl_audit *audit_info) 594b1edeb10SPaul Moore { 5956c2e8ac0SPaul Moore int ret_val; 596b1edeb10SPaul Moore struct cipso_v4_doi *doi_def; 5976c2e8ac0SPaul Moore struct audit_buffer *audit_buf; 598b1edeb10SPaul Moore 599b1edeb10SPaul Moore spin_lock(&cipso_v4_doi_list_lock); 600b1edeb10SPaul Moore doi_def = cipso_v4_doi_search(doi); 601b1edeb10SPaul Moore if (doi_def == NULL) { 602b1edeb10SPaul Moore spin_unlock(&cipso_v4_doi_list_lock); 6036c2e8ac0SPaul Moore ret_val = -ENOENT; 6046c2e8ac0SPaul Moore goto doi_remove_return; 605b1edeb10SPaul Moore } 606b1edeb10SPaul Moore if (!atomic_dec_and_test(&doi_def->refcount)) { 607b1edeb10SPaul Moore spin_unlock(&cipso_v4_doi_list_lock); 6086c2e8ac0SPaul Moore ret_val = -EBUSY; 6096c2e8ac0SPaul Moore goto doi_remove_return; 610b1edeb10SPaul Moore } 611b1edeb10SPaul Moore list_del_rcu(&doi_def->list); 612b1edeb10SPaul Moore spin_unlock(&cipso_v4_doi_list_lock); 613b1edeb10SPaul Moore 614b1edeb10SPaul Moore cipso_v4_cache_invalidate(); 615b1edeb10SPaul Moore call_rcu(&doi_def->rcu, cipso_v4_doi_free_rcu); 6166c2e8ac0SPaul Moore ret_val = 0; 617b1edeb10SPaul Moore 6186c2e8ac0SPaul Moore doi_remove_return: 6196c2e8ac0SPaul Moore audit_buf = netlbl_audit_start(AUDIT_MAC_CIPSOV4_DEL, audit_info); 6206c2e8ac0SPaul Moore if (audit_buf != NULL) { 6216c2e8ac0SPaul Moore audit_log_format(audit_buf, 6226c2e8ac0SPaul Moore " cipso_doi=%u res=%u", 6236c2e8ac0SPaul Moore doi, ret_val == 0 ? 1 : 0); 6246c2e8ac0SPaul Moore audit_log_end(audit_buf); 6256c2e8ac0SPaul Moore } 6266c2e8ac0SPaul Moore 6276c2e8ac0SPaul Moore return ret_val; 628b1edeb10SPaul Moore } 629b1edeb10SPaul Moore 630b1edeb10SPaul Moore /** 631b1edeb10SPaul Moore * cipso_v4_doi_getdef - Returns a reference to a valid DOI definition 632446fda4fSPaul Moore * @doi: the DOI value 633446fda4fSPaul Moore * 634446fda4fSPaul Moore * Description: 635446fda4fSPaul Moore * Searches for a valid DOI definition and if one is found it is returned to 636446fda4fSPaul Moore * the caller. Otherwise NULL is returned. The caller must ensure that 637b1edeb10SPaul Moore * rcu_read_lock() is held while accessing the returned definition and the DOI 638b1edeb10SPaul Moore * definition reference count is decremented when the caller is done. 639446fda4fSPaul Moore * 640446fda4fSPaul Moore */ 641446fda4fSPaul Moore struct cipso_v4_doi *cipso_v4_doi_getdef(u32 doi) 642446fda4fSPaul Moore { 643b1edeb10SPaul Moore struct cipso_v4_doi *doi_def; 644b1edeb10SPaul Moore 645b1edeb10SPaul Moore rcu_read_lock(); 646b1edeb10SPaul Moore doi_def = cipso_v4_doi_search(doi); 647b1edeb10SPaul Moore if (doi_def == NULL) 648b1edeb10SPaul Moore goto doi_getdef_return; 649b1edeb10SPaul Moore if (!atomic_inc_not_zero(&doi_def->refcount)) 650b1edeb10SPaul Moore doi_def = NULL; 651b1edeb10SPaul Moore 652b1edeb10SPaul Moore doi_getdef_return: 653b1edeb10SPaul Moore rcu_read_unlock(); 654b1edeb10SPaul Moore return doi_def; 655b1edeb10SPaul Moore } 656b1edeb10SPaul Moore 657b1edeb10SPaul Moore /** 658b1edeb10SPaul Moore * cipso_v4_doi_putdef - Releases a reference for the given DOI definition 659b1edeb10SPaul Moore * @doi_def: the DOI definition 660b1edeb10SPaul Moore * 661b1edeb10SPaul Moore * Description: 662b1edeb10SPaul Moore * Releases a DOI definition reference obtained from cipso_v4_doi_getdef(). 663b1edeb10SPaul Moore * 664b1edeb10SPaul Moore */ 665b1edeb10SPaul Moore void cipso_v4_doi_putdef(struct cipso_v4_doi *doi_def) 666b1edeb10SPaul Moore { 667b1edeb10SPaul Moore if (doi_def == NULL) 668b1edeb10SPaul Moore return; 669b1edeb10SPaul Moore 670b1edeb10SPaul Moore if (!atomic_dec_and_test(&doi_def->refcount)) 671b1edeb10SPaul Moore return; 672b1edeb10SPaul Moore spin_lock(&cipso_v4_doi_list_lock); 673b1edeb10SPaul Moore list_del_rcu(&doi_def->list); 674b1edeb10SPaul Moore spin_unlock(&cipso_v4_doi_list_lock); 675b1edeb10SPaul Moore 676b1edeb10SPaul Moore cipso_v4_cache_invalidate(); 677b1edeb10SPaul Moore call_rcu(&doi_def->rcu, cipso_v4_doi_free_rcu); 678446fda4fSPaul Moore } 679446fda4fSPaul Moore 680446fda4fSPaul Moore /** 681fcd48280SPaul Moore * cipso_v4_doi_walk - Iterate through the DOI definitions 682fcd48280SPaul Moore * @skip_cnt: skip past this number of DOI definitions, updated 683fcd48280SPaul Moore * @callback: callback for each DOI definition 684fcd48280SPaul Moore * @cb_arg: argument for the callback function 685446fda4fSPaul Moore * 686446fda4fSPaul Moore * Description: 687fcd48280SPaul Moore * Iterate over the DOI definition list, skipping the first @skip_cnt entries. 688fcd48280SPaul Moore * For each entry call @callback, if @callback returns a negative value stop 689fcd48280SPaul Moore * 'walking' through the list and return. Updates the value in @skip_cnt upon 690fcd48280SPaul Moore * return. Returns zero on success, negative values on failure. 691446fda4fSPaul Moore * 692446fda4fSPaul Moore */ 693fcd48280SPaul Moore int cipso_v4_doi_walk(u32 *skip_cnt, 694fcd48280SPaul Moore int (*callback) (struct cipso_v4_doi *doi_def, void *arg), 695fcd48280SPaul Moore void *cb_arg) 696446fda4fSPaul Moore { 697fcd48280SPaul Moore int ret_val = -ENOENT; 698446fda4fSPaul Moore u32 doi_cnt = 0; 699fcd48280SPaul Moore struct cipso_v4_doi *iter_doi; 700446fda4fSPaul Moore 701446fda4fSPaul Moore rcu_read_lock(); 702fcd48280SPaul Moore list_for_each_entry_rcu(iter_doi, &cipso_v4_doi_list, list) 703b1edeb10SPaul Moore if (atomic_read(&iter_doi->refcount) > 0) { 704fcd48280SPaul Moore if (doi_cnt++ < *skip_cnt) 705fcd48280SPaul Moore continue; 706fcd48280SPaul Moore ret_val = callback(iter_doi, cb_arg); 707fcd48280SPaul Moore if (ret_val < 0) { 708fcd48280SPaul Moore doi_cnt--; 709fcd48280SPaul Moore goto doi_walk_return; 710446fda4fSPaul Moore } 711446fda4fSPaul Moore } 712446fda4fSPaul Moore 713fcd48280SPaul Moore doi_walk_return: 714446fda4fSPaul Moore rcu_read_unlock(); 715fcd48280SPaul Moore *skip_cnt = doi_cnt; 716fcd48280SPaul Moore return ret_val; 717446fda4fSPaul Moore } 718446fda4fSPaul Moore 719446fda4fSPaul Moore /* 720446fda4fSPaul Moore * Label Mapping Functions 721446fda4fSPaul Moore */ 722446fda4fSPaul Moore 723446fda4fSPaul Moore /** 724446fda4fSPaul Moore * cipso_v4_map_lvl_valid - Checks to see if the given level is understood 725446fda4fSPaul Moore * @doi_def: the DOI definition 726446fda4fSPaul Moore * @level: the level to check 727446fda4fSPaul Moore * 728446fda4fSPaul Moore * Description: 729446fda4fSPaul Moore * Checks the given level against the given DOI definition and returns a 730446fda4fSPaul Moore * negative value if the level does not have a valid mapping and a zero value 731446fda4fSPaul Moore * if the level is defined by the DOI. 732446fda4fSPaul Moore * 733446fda4fSPaul Moore */ 734446fda4fSPaul Moore static int cipso_v4_map_lvl_valid(const struct cipso_v4_doi *doi_def, u8 level) 735446fda4fSPaul Moore { 736446fda4fSPaul Moore switch (doi_def->type) { 737446fda4fSPaul Moore case CIPSO_V4_MAP_PASS: 738446fda4fSPaul Moore return 0; 73915c45f7bSPaul Moore case CIPSO_V4_MAP_TRANS: 740446fda4fSPaul Moore if (doi_def->map.std->lvl.cipso[level] < CIPSO_V4_INV_LVL) 741446fda4fSPaul Moore return 0; 742446fda4fSPaul Moore break; 743446fda4fSPaul Moore } 744446fda4fSPaul Moore 745446fda4fSPaul Moore return -EFAULT; 746446fda4fSPaul Moore } 747446fda4fSPaul Moore 748446fda4fSPaul Moore /** 749446fda4fSPaul Moore * cipso_v4_map_lvl_hton - Perform a level mapping from the host to the network 750446fda4fSPaul Moore * @doi_def: the DOI definition 751446fda4fSPaul Moore * @host_lvl: the host MLS level 752446fda4fSPaul Moore * @net_lvl: the network/CIPSO MLS level 753446fda4fSPaul Moore * 754446fda4fSPaul Moore * Description: 755446fda4fSPaul Moore * Perform a label mapping to translate a local MLS level to the correct 756446fda4fSPaul Moore * CIPSO level using the given DOI definition. Returns zero on success, 757446fda4fSPaul Moore * negative values otherwise. 758446fda4fSPaul Moore * 759446fda4fSPaul Moore */ 760446fda4fSPaul Moore static int cipso_v4_map_lvl_hton(const struct cipso_v4_doi *doi_def, 761446fda4fSPaul Moore u32 host_lvl, 762446fda4fSPaul Moore u32 *net_lvl) 763446fda4fSPaul Moore { 764446fda4fSPaul Moore switch (doi_def->type) { 765446fda4fSPaul Moore case CIPSO_V4_MAP_PASS: 766446fda4fSPaul Moore *net_lvl = host_lvl; 767446fda4fSPaul Moore return 0; 76815c45f7bSPaul Moore case CIPSO_V4_MAP_TRANS: 769c6387a86SPaul Moore if (host_lvl < doi_def->map.std->lvl.local_size && 770c6387a86SPaul Moore doi_def->map.std->lvl.local[host_lvl] < CIPSO_V4_INV_LVL) { 771446fda4fSPaul Moore *net_lvl = doi_def->map.std->lvl.local[host_lvl]; 772446fda4fSPaul Moore return 0; 773446fda4fSPaul Moore } 774c6387a86SPaul Moore return -EPERM; 775446fda4fSPaul Moore } 776446fda4fSPaul Moore 777446fda4fSPaul Moore return -EINVAL; 778446fda4fSPaul Moore } 779446fda4fSPaul Moore 780446fda4fSPaul Moore /** 781446fda4fSPaul Moore * cipso_v4_map_lvl_ntoh - Perform a level mapping from the network to the host 782446fda4fSPaul Moore * @doi_def: the DOI definition 783446fda4fSPaul Moore * @net_lvl: the network/CIPSO MLS level 784446fda4fSPaul Moore * @host_lvl: the host MLS level 785446fda4fSPaul Moore * 786446fda4fSPaul Moore * Description: 787446fda4fSPaul Moore * Perform a label mapping to translate a CIPSO level to the correct local MLS 788446fda4fSPaul Moore * level using the given DOI definition. Returns zero on success, negative 789446fda4fSPaul Moore * values otherwise. 790446fda4fSPaul Moore * 791446fda4fSPaul Moore */ 792446fda4fSPaul Moore static int cipso_v4_map_lvl_ntoh(const struct cipso_v4_doi *doi_def, 793446fda4fSPaul Moore u32 net_lvl, 794446fda4fSPaul Moore u32 *host_lvl) 795446fda4fSPaul Moore { 796446fda4fSPaul Moore struct cipso_v4_std_map_tbl *map_tbl; 797446fda4fSPaul Moore 798446fda4fSPaul Moore switch (doi_def->type) { 799446fda4fSPaul Moore case CIPSO_V4_MAP_PASS: 800446fda4fSPaul Moore *host_lvl = net_lvl; 801446fda4fSPaul Moore return 0; 80215c45f7bSPaul Moore case CIPSO_V4_MAP_TRANS: 803446fda4fSPaul Moore map_tbl = doi_def->map.std; 804446fda4fSPaul Moore if (net_lvl < map_tbl->lvl.cipso_size && 805446fda4fSPaul Moore map_tbl->lvl.cipso[net_lvl] < CIPSO_V4_INV_LVL) { 806446fda4fSPaul Moore *host_lvl = doi_def->map.std->lvl.cipso[net_lvl]; 807446fda4fSPaul Moore return 0; 808446fda4fSPaul Moore } 809c6387a86SPaul Moore return -EPERM; 810446fda4fSPaul Moore } 811446fda4fSPaul Moore 812446fda4fSPaul Moore return -EINVAL; 813446fda4fSPaul Moore } 814446fda4fSPaul Moore 815446fda4fSPaul Moore /** 816446fda4fSPaul Moore * cipso_v4_map_cat_rbm_valid - Checks to see if the category bitmap is valid 817446fda4fSPaul Moore * @doi_def: the DOI definition 818446fda4fSPaul Moore * @bitmap: category bitmap 819446fda4fSPaul Moore * @bitmap_len: bitmap length in bytes 820446fda4fSPaul Moore * 821446fda4fSPaul Moore * Description: 822446fda4fSPaul Moore * Checks the given category bitmap against the given DOI definition and 823446fda4fSPaul Moore * returns a negative value if any of the categories in the bitmap do not have 824446fda4fSPaul Moore * a valid mapping and a zero value if all of the categories are valid. 825446fda4fSPaul Moore * 826446fda4fSPaul Moore */ 827446fda4fSPaul Moore static int cipso_v4_map_cat_rbm_valid(const struct cipso_v4_doi *doi_def, 828446fda4fSPaul Moore const unsigned char *bitmap, 829446fda4fSPaul Moore u32 bitmap_len) 830446fda4fSPaul Moore { 831446fda4fSPaul Moore int cat = -1; 832446fda4fSPaul Moore u32 bitmap_len_bits = bitmap_len * 8; 833044a68edSPaul Moore u32 cipso_cat_size; 834044a68edSPaul Moore u32 *cipso_array; 835446fda4fSPaul Moore 836446fda4fSPaul Moore switch (doi_def->type) { 837446fda4fSPaul Moore case CIPSO_V4_MAP_PASS: 838446fda4fSPaul Moore return 0; 83915c45f7bSPaul Moore case CIPSO_V4_MAP_TRANS: 840044a68edSPaul Moore cipso_cat_size = doi_def->map.std->cat.cipso_size; 841044a68edSPaul Moore cipso_array = doi_def->map.std->cat.cipso; 842446fda4fSPaul Moore for (;;) { 843446fda4fSPaul Moore cat = cipso_v4_bitmap_walk(bitmap, 844446fda4fSPaul Moore bitmap_len_bits, 845446fda4fSPaul Moore cat + 1, 846446fda4fSPaul Moore 1); 847446fda4fSPaul Moore if (cat < 0) 848446fda4fSPaul Moore break; 849446fda4fSPaul Moore if (cat >= cipso_cat_size || 850446fda4fSPaul Moore cipso_array[cat] >= CIPSO_V4_INV_CAT) 851446fda4fSPaul Moore return -EFAULT; 852446fda4fSPaul Moore } 853446fda4fSPaul Moore 854446fda4fSPaul Moore if (cat == -1) 855446fda4fSPaul Moore return 0; 856446fda4fSPaul Moore break; 857446fda4fSPaul Moore } 858446fda4fSPaul Moore 859446fda4fSPaul Moore return -EFAULT; 860446fda4fSPaul Moore } 861446fda4fSPaul Moore 862446fda4fSPaul Moore /** 863446fda4fSPaul Moore * cipso_v4_map_cat_rbm_hton - Perform a category mapping from host to network 864446fda4fSPaul Moore * @doi_def: the DOI definition 86502752760SPaul Moore * @secattr: the security attributes 866446fda4fSPaul Moore * @net_cat: the zero'd out category bitmap in network/CIPSO format 867446fda4fSPaul Moore * @net_cat_len: the length of the CIPSO bitmap in bytes 868446fda4fSPaul Moore * 869446fda4fSPaul Moore * Description: 870446fda4fSPaul Moore * Perform a label mapping to translate a local MLS category bitmap to the 871446fda4fSPaul Moore * correct CIPSO bitmap using the given DOI definition. Returns the minimum 872446fda4fSPaul Moore * size in bytes of the network bitmap on success, negative values otherwise. 873446fda4fSPaul Moore * 874446fda4fSPaul Moore */ 875446fda4fSPaul Moore static int cipso_v4_map_cat_rbm_hton(const struct cipso_v4_doi *doi_def, 87602752760SPaul Moore const struct netlbl_lsm_secattr *secattr, 877446fda4fSPaul Moore unsigned char *net_cat, 878446fda4fSPaul Moore u32 net_cat_len) 879446fda4fSPaul Moore { 880446fda4fSPaul Moore int host_spot = -1; 88102752760SPaul Moore u32 net_spot = CIPSO_V4_INV_CAT; 882446fda4fSPaul Moore u32 net_spot_max = 0; 883446fda4fSPaul Moore u32 net_clen_bits = net_cat_len * 8; 88402752760SPaul Moore u32 host_cat_size = 0; 88502752760SPaul Moore u32 *host_cat_array = NULL; 88602752760SPaul Moore 88715c45f7bSPaul Moore if (doi_def->type == CIPSO_V4_MAP_TRANS) { 88802752760SPaul Moore host_cat_size = doi_def->map.std->cat.local_size; 88902752760SPaul Moore host_cat_array = doi_def->map.std->cat.local; 89002752760SPaul Moore } 89102752760SPaul Moore 89202752760SPaul Moore for (;;) { 89316efd454SPaul Moore host_spot = netlbl_secattr_catmap_walk(secattr->attr.mls.cat, 89402752760SPaul Moore host_spot + 1); 89502752760SPaul Moore if (host_spot < 0) 89602752760SPaul Moore break; 897446fda4fSPaul Moore 898446fda4fSPaul Moore switch (doi_def->type) { 899446fda4fSPaul Moore case CIPSO_V4_MAP_PASS: 90002752760SPaul Moore net_spot = host_spot; 901446fda4fSPaul Moore break; 90215c45f7bSPaul Moore case CIPSO_V4_MAP_TRANS: 903446fda4fSPaul Moore if (host_spot >= host_cat_size) 904446fda4fSPaul Moore return -EPERM; 905446fda4fSPaul Moore net_spot = host_cat_array[host_spot]; 9069fade4bfSPaul Moore if (net_spot >= CIPSO_V4_INV_CAT) 9079fade4bfSPaul Moore return -EPERM; 90802752760SPaul Moore break; 90902752760SPaul Moore } 910446fda4fSPaul Moore if (net_spot >= net_clen_bits) 911446fda4fSPaul Moore return -ENOSPC; 912446fda4fSPaul Moore cipso_v4_bitmap_setbit(net_cat, net_spot, 1); 913446fda4fSPaul Moore 914446fda4fSPaul Moore if (net_spot > net_spot_max) 915446fda4fSPaul Moore net_spot_max = net_spot; 916446fda4fSPaul Moore } 917446fda4fSPaul Moore 918446fda4fSPaul Moore if (++net_spot_max % 8) 919446fda4fSPaul Moore return net_spot_max / 8 + 1; 920446fda4fSPaul Moore return net_spot_max / 8; 921446fda4fSPaul Moore } 922446fda4fSPaul Moore 923446fda4fSPaul Moore /** 924446fda4fSPaul Moore * cipso_v4_map_cat_rbm_ntoh - Perform a category mapping from network to host 925446fda4fSPaul Moore * @doi_def: the DOI definition 926446fda4fSPaul Moore * @net_cat: the category bitmap in network/CIPSO format 927446fda4fSPaul Moore * @net_cat_len: the length of the CIPSO bitmap in bytes 92802752760SPaul Moore * @secattr: the security attributes 929446fda4fSPaul Moore * 930446fda4fSPaul Moore * Description: 931446fda4fSPaul Moore * Perform a label mapping to translate a CIPSO bitmap to the correct local 93202752760SPaul Moore * MLS category bitmap using the given DOI definition. Returns zero on 93302752760SPaul Moore * success, negative values on failure. 934446fda4fSPaul Moore * 935446fda4fSPaul Moore */ 936446fda4fSPaul Moore static int cipso_v4_map_cat_rbm_ntoh(const struct cipso_v4_doi *doi_def, 937446fda4fSPaul Moore const unsigned char *net_cat, 938446fda4fSPaul Moore u32 net_cat_len, 93902752760SPaul Moore struct netlbl_lsm_secattr *secattr) 940446fda4fSPaul Moore { 94102752760SPaul Moore int ret_val; 942446fda4fSPaul Moore int net_spot = -1; 94302752760SPaul Moore u32 host_spot = CIPSO_V4_INV_CAT; 944446fda4fSPaul Moore u32 net_clen_bits = net_cat_len * 8; 94502752760SPaul Moore u32 net_cat_size = 0; 94602752760SPaul Moore u32 *net_cat_array = NULL; 947446fda4fSPaul Moore 94815c45f7bSPaul Moore if (doi_def->type == CIPSO_V4_MAP_TRANS) { 949044a68edSPaul Moore net_cat_size = doi_def->map.std->cat.cipso_size; 950044a68edSPaul Moore net_cat_array = doi_def->map.std->cat.cipso; 95102752760SPaul Moore } 95202752760SPaul Moore 953446fda4fSPaul Moore for (;;) { 954446fda4fSPaul Moore net_spot = cipso_v4_bitmap_walk(net_cat, 955446fda4fSPaul Moore net_clen_bits, 956446fda4fSPaul Moore net_spot + 1, 957446fda4fSPaul Moore 1); 95802752760SPaul Moore if (net_spot < 0) { 95902752760SPaul Moore if (net_spot == -2) 96002752760SPaul Moore return -EFAULT; 96102752760SPaul Moore return 0; 96202752760SPaul Moore } 963446fda4fSPaul Moore 96402752760SPaul Moore switch (doi_def->type) { 96502752760SPaul Moore case CIPSO_V4_MAP_PASS: 96602752760SPaul Moore host_spot = net_spot; 96702752760SPaul Moore break; 96815c45f7bSPaul Moore case CIPSO_V4_MAP_TRANS: 96902752760SPaul Moore if (net_spot >= net_cat_size) 97002752760SPaul Moore return -EPERM; 971446fda4fSPaul Moore host_spot = net_cat_array[net_spot]; 9729fade4bfSPaul Moore if (host_spot >= CIPSO_V4_INV_CAT) 9739fade4bfSPaul Moore return -EPERM; 97402752760SPaul Moore break; 975446fda4fSPaul Moore } 97616efd454SPaul Moore ret_val = netlbl_secattr_catmap_setbit(secattr->attr.mls.cat, 97702752760SPaul Moore host_spot, 97802752760SPaul Moore GFP_ATOMIC); 97902752760SPaul Moore if (ret_val != 0) 98002752760SPaul Moore return ret_val; 981446fda4fSPaul Moore } 982446fda4fSPaul Moore 983446fda4fSPaul Moore return -EINVAL; 984446fda4fSPaul Moore } 985446fda4fSPaul Moore 986654bbc2aSPaul Moore /** 987654bbc2aSPaul Moore * cipso_v4_map_cat_enum_valid - Checks to see if the categories are valid 988654bbc2aSPaul Moore * @doi_def: the DOI definition 989654bbc2aSPaul Moore * @enumcat: category list 990654bbc2aSPaul Moore * @enumcat_len: length of the category list in bytes 991654bbc2aSPaul Moore * 992654bbc2aSPaul Moore * Description: 993654bbc2aSPaul Moore * Checks the given categories against the given DOI definition and returns a 994654bbc2aSPaul Moore * negative value if any of the categories do not have a valid mapping and a 995654bbc2aSPaul Moore * zero value if all of the categories are valid. 996654bbc2aSPaul Moore * 997654bbc2aSPaul Moore */ 998654bbc2aSPaul Moore static int cipso_v4_map_cat_enum_valid(const struct cipso_v4_doi *doi_def, 999654bbc2aSPaul Moore const unsigned char *enumcat, 1000654bbc2aSPaul Moore u32 enumcat_len) 1001654bbc2aSPaul Moore { 1002654bbc2aSPaul Moore u16 cat; 1003654bbc2aSPaul Moore int cat_prev = -1; 1004654bbc2aSPaul Moore u32 iter; 1005654bbc2aSPaul Moore 1006654bbc2aSPaul Moore if (doi_def->type != CIPSO_V4_MAP_PASS || enumcat_len & 0x01) 1007654bbc2aSPaul Moore return -EFAULT; 1008654bbc2aSPaul Moore 1009654bbc2aSPaul Moore for (iter = 0; iter < enumcat_len; iter += 2) { 1010d3e2ce3bSHarvey Harrison cat = get_unaligned_be16(&enumcat[iter]); 1011654bbc2aSPaul Moore if (cat <= cat_prev) 1012654bbc2aSPaul Moore return -EFAULT; 1013654bbc2aSPaul Moore cat_prev = cat; 1014654bbc2aSPaul Moore } 1015654bbc2aSPaul Moore 1016654bbc2aSPaul Moore return 0; 1017654bbc2aSPaul Moore } 1018654bbc2aSPaul Moore 1019654bbc2aSPaul Moore /** 1020654bbc2aSPaul Moore * cipso_v4_map_cat_enum_hton - Perform a category mapping from host to network 1021654bbc2aSPaul Moore * @doi_def: the DOI definition 1022654bbc2aSPaul Moore * @secattr: the security attributes 1023654bbc2aSPaul Moore * @net_cat: the zero'd out category list in network/CIPSO format 1024654bbc2aSPaul Moore * @net_cat_len: the length of the CIPSO category list in bytes 1025654bbc2aSPaul Moore * 1026654bbc2aSPaul Moore * Description: 1027654bbc2aSPaul Moore * Perform a label mapping to translate a local MLS category bitmap to the 1028654bbc2aSPaul Moore * correct CIPSO category list using the given DOI definition. Returns the 1029654bbc2aSPaul Moore * size in bytes of the network category bitmap on success, negative values 1030654bbc2aSPaul Moore * otherwise. 1031654bbc2aSPaul Moore * 1032654bbc2aSPaul Moore */ 1033654bbc2aSPaul Moore static int cipso_v4_map_cat_enum_hton(const struct cipso_v4_doi *doi_def, 1034654bbc2aSPaul Moore const struct netlbl_lsm_secattr *secattr, 1035654bbc2aSPaul Moore unsigned char *net_cat, 1036654bbc2aSPaul Moore u32 net_cat_len) 1037654bbc2aSPaul Moore { 1038654bbc2aSPaul Moore int cat = -1; 1039654bbc2aSPaul Moore u32 cat_iter = 0; 1040654bbc2aSPaul Moore 1041654bbc2aSPaul Moore for (;;) { 104216efd454SPaul Moore cat = netlbl_secattr_catmap_walk(secattr->attr.mls.cat, 104316efd454SPaul Moore cat + 1); 1044654bbc2aSPaul Moore if (cat < 0) 1045654bbc2aSPaul Moore break; 1046654bbc2aSPaul Moore if ((cat_iter + 2) > net_cat_len) 1047654bbc2aSPaul Moore return -ENOSPC; 1048654bbc2aSPaul Moore 1049654bbc2aSPaul Moore *((__be16 *)&net_cat[cat_iter]) = htons(cat); 1050654bbc2aSPaul Moore cat_iter += 2; 1051654bbc2aSPaul Moore } 1052654bbc2aSPaul Moore 1053654bbc2aSPaul Moore return cat_iter; 1054654bbc2aSPaul Moore } 1055654bbc2aSPaul Moore 1056654bbc2aSPaul Moore /** 1057654bbc2aSPaul Moore * cipso_v4_map_cat_enum_ntoh - Perform a category mapping from network to host 1058654bbc2aSPaul Moore * @doi_def: the DOI definition 1059654bbc2aSPaul Moore * @net_cat: the category list in network/CIPSO format 1060654bbc2aSPaul Moore * @net_cat_len: the length of the CIPSO bitmap in bytes 1061654bbc2aSPaul Moore * @secattr: the security attributes 1062654bbc2aSPaul Moore * 1063654bbc2aSPaul Moore * Description: 1064654bbc2aSPaul Moore * Perform a label mapping to translate a CIPSO category list to the correct 1065654bbc2aSPaul Moore * local MLS category bitmap using the given DOI definition. Returns zero on 1066654bbc2aSPaul Moore * success, negative values on failure. 1067654bbc2aSPaul Moore * 1068654bbc2aSPaul Moore */ 1069654bbc2aSPaul Moore static int cipso_v4_map_cat_enum_ntoh(const struct cipso_v4_doi *doi_def, 1070654bbc2aSPaul Moore const unsigned char *net_cat, 1071654bbc2aSPaul Moore u32 net_cat_len, 1072654bbc2aSPaul Moore struct netlbl_lsm_secattr *secattr) 1073654bbc2aSPaul Moore { 1074654bbc2aSPaul Moore int ret_val; 1075654bbc2aSPaul Moore u32 iter; 1076654bbc2aSPaul Moore 1077654bbc2aSPaul Moore for (iter = 0; iter < net_cat_len; iter += 2) { 107816efd454SPaul Moore ret_val = netlbl_secattr_catmap_setbit(secattr->attr.mls.cat, 1079d3e2ce3bSHarvey Harrison get_unaligned_be16(&net_cat[iter]), 1080654bbc2aSPaul Moore GFP_ATOMIC); 1081654bbc2aSPaul Moore if (ret_val != 0) 1082654bbc2aSPaul Moore return ret_val; 1083654bbc2aSPaul Moore } 1084654bbc2aSPaul Moore 1085654bbc2aSPaul Moore return 0; 1086654bbc2aSPaul Moore } 1087654bbc2aSPaul Moore 1088484b3669SPaul Moore /** 1089484b3669SPaul Moore * cipso_v4_map_cat_rng_valid - Checks to see if the categories are valid 1090484b3669SPaul Moore * @doi_def: the DOI definition 1091484b3669SPaul Moore * @rngcat: category list 1092484b3669SPaul Moore * @rngcat_len: length of the category list in bytes 1093484b3669SPaul Moore * 1094484b3669SPaul Moore * Description: 1095484b3669SPaul Moore * Checks the given categories against the given DOI definition and returns a 1096484b3669SPaul Moore * negative value if any of the categories do not have a valid mapping and a 1097484b3669SPaul Moore * zero value if all of the categories are valid. 1098484b3669SPaul Moore * 1099484b3669SPaul Moore */ 1100484b3669SPaul Moore static int cipso_v4_map_cat_rng_valid(const struct cipso_v4_doi *doi_def, 1101484b3669SPaul Moore const unsigned char *rngcat, 1102484b3669SPaul Moore u32 rngcat_len) 1103484b3669SPaul Moore { 1104484b3669SPaul Moore u16 cat_high; 1105484b3669SPaul Moore u16 cat_low; 1106484b3669SPaul Moore u32 cat_prev = CIPSO_V4_MAX_REM_CATS + 1; 1107484b3669SPaul Moore u32 iter; 1108484b3669SPaul Moore 1109484b3669SPaul Moore if (doi_def->type != CIPSO_V4_MAP_PASS || rngcat_len & 0x01) 1110484b3669SPaul Moore return -EFAULT; 1111484b3669SPaul Moore 1112484b3669SPaul Moore for (iter = 0; iter < rngcat_len; iter += 4) { 1113d3e2ce3bSHarvey Harrison cat_high = get_unaligned_be16(&rngcat[iter]); 1114484b3669SPaul Moore if ((iter + 4) <= rngcat_len) 1115d3e2ce3bSHarvey Harrison cat_low = get_unaligned_be16(&rngcat[iter + 2]); 1116484b3669SPaul Moore else 1117484b3669SPaul Moore cat_low = 0; 1118484b3669SPaul Moore 1119484b3669SPaul Moore if (cat_high > cat_prev) 1120484b3669SPaul Moore return -EFAULT; 1121484b3669SPaul Moore 1122484b3669SPaul Moore cat_prev = cat_low; 1123484b3669SPaul Moore } 1124484b3669SPaul Moore 1125484b3669SPaul Moore return 0; 1126484b3669SPaul Moore } 1127484b3669SPaul Moore 1128484b3669SPaul Moore /** 1129484b3669SPaul Moore * cipso_v4_map_cat_rng_hton - Perform a category mapping from host to network 1130484b3669SPaul Moore * @doi_def: the DOI definition 1131484b3669SPaul Moore * @secattr: the security attributes 1132484b3669SPaul Moore * @net_cat: the zero'd out category list in network/CIPSO format 1133484b3669SPaul Moore * @net_cat_len: the length of the CIPSO category list in bytes 1134484b3669SPaul Moore * 1135484b3669SPaul Moore * Description: 1136484b3669SPaul Moore * Perform a label mapping to translate a local MLS category bitmap to the 1137484b3669SPaul Moore * correct CIPSO category list using the given DOI definition. Returns the 1138484b3669SPaul Moore * size in bytes of the network category bitmap on success, negative values 1139484b3669SPaul Moore * otherwise. 1140484b3669SPaul Moore * 1141484b3669SPaul Moore */ 1142484b3669SPaul Moore static int cipso_v4_map_cat_rng_hton(const struct cipso_v4_doi *doi_def, 1143484b3669SPaul Moore const struct netlbl_lsm_secattr *secattr, 1144484b3669SPaul Moore unsigned char *net_cat, 1145484b3669SPaul Moore u32 net_cat_len) 1146484b3669SPaul Moore { 1147484b3669SPaul Moore int iter = -1; 1148f998e8cbSPaul Moore u16 array[CIPSO_V4_TAG_RNG_CAT_MAX * 2]; 1149484b3669SPaul Moore u32 array_cnt = 0; 1150484b3669SPaul Moore u32 cat_size = 0; 1151484b3669SPaul Moore 1152f998e8cbSPaul Moore /* make sure we don't overflow the 'array[]' variable */ 1153128c6b6cSPaul Moore if (net_cat_len > 1154128c6b6cSPaul Moore (CIPSO_V4_OPT_LEN_MAX - CIPSO_V4_HDR_LEN - CIPSO_V4_TAG_RNG_BLEN)) 1155128c6b6cSPaul Moore return -ENOSPC; 1156484b3669SPaul Moore 1157484b3669SPaul Moore for (;;) { 115816efd454SPaul Moore iter = netlbl_secattr_catmap_walk(secattr->attr.mls.cat, 115916efd454SPaul Moore iter + 1); 1160484b3669SPaul Moore if (iter < 0) 1161484b3669SPaul Moore break; 1162484b3669SPaul Moore cat_size += (iter == 0 ? 0 : sizeof(u16)); 1163484b3669SPaul Moore if (cat_size > net_cat_len) 1164484b3669SPaul Moore return -ENOSPC; 1165484b3669SPaul Moore array[array_cnt++] = iter; 1166484b3669SPaul Moore 116716efd454SPaul Moore iter = netlbl_secattr_catmap_walk_rng(secattr->attr.mls.cat, 116816efd454SPaul Moore iter); 1169484b3669SPaul Moore if (iter < 0) 1170484b3669SPaul Moore return -EFAULT; 1171484b3669SPaul Moore cat_size += sizeof(u16); 1172484b3669SPaul Moore if (cat_size > net_cat_len) 1173484b3669SPaul Moore return -ENOSPC; 1174484b3669SPaul Moore array[array_cnt++] = iter; 1175484b3669SPaul Moore } 1176484b3669SPaul Moore 1177484b3669SPaul Moore for (iter = 0; array_cnt > 0;) { 1178484b3669SPaul Moore *((__be16 *)&net_cat[iter]) = htons(array[--array_cnt]); 1179484b3669SPaul Moore iter += 2; 1180484b3669SPaul Moore array_cnt--; 1181484b3669SPaul Moore if (array[array_cnt] != 0) { 1182484b3669SPaul Moore *((__be16 *)&net_cat[iter]) = htons(array[array_cnt]); 1183484b3669SPaul Moore iter += 2; 1184484b3669SPaul Moore } 1185484b3669SPaul Moore } 1186484b3669SPaul Moore 1187484b3669SPaul Moore return cat_size; 1188484b3669SPaul Moore } 1189484b3669SPaul Moore 1190484b3669SPaul Moore /** 1191484b3669SPaul Moore * cipso_v4_map_cat_rng_ntoh - Perform a category mapping from network to host 1192484b3669SPaul Moore * @doi_def: the DOI definition 1193484b3669SPaul Moore * @net_cat: the category list in network/CIPSO format 1194484b3669SPaul Moore * @net_cat_len: the length of the CIPSO bitmap in bytes 1195484b3669SPaul Moore * @secattr: the security attributes 1196484b3669SPaul Moore * 1197484b3669SPaul Moore * Description: 1198484b3669SPaul Moore * Perform a label mapping to translate a CIPSO category list to the correct 1199484b3669SPaul Moore * local MLS category bitmap using the given DOI definition. Returns zero on 1200484b3669SPaul Moore * success, negative values on failure. 1201484b3669SPaul Moore * 1202484b3669SPaul Moore */ 1203484b3669SPaul Moore static int cipso_v4_map_cat_rng_ntoh(const struct cipso_v4_doi *doi_def, 1204484b3669SPaul Moore const unsigned char *net_cat, 1205484b3669SPaul Moore u32 net_cat_len, 1206484b3669SPaul Moore struct netlbl_lsm_secattr *secattr) 1207484b3669SPaul Moore { 1208484b3669SPaul Moore int ret_val; 1209484b3669SPaul Moore u32 net_iter; 1210484b3669SPaul Moore u16 cat_low; 1211484b3669SPaul Moore u16 cat_high; 1212484b3669SPaul Moore 1213484b3669SPaul Moore for (net_iter = 0; net_iter < net_cat_len; net_iter += 4) { 1214d3e2ce3bSHarvey Harrison cat_high = get_unaligned_be16(&net_cat[net_iter]); 1215484b3669SPaul Moore if ((net_iter + 4) <= net_cat_len) 1216d3e2ce3bSHarvey Harrison cat_low = get_unaligned_be16(&net_cat[net_iter + 2]); 1217484b3669SPaul Moore else 1218484b3669SPaul Moore cat_low = 0; 1219484b3669SPaul Moore 122016efd454SPaul Moore ret_val = netlbl_secattr_catmap_setrng(secattr->attr.mls.cat, 1221484b3669SPaul Moore cat_low, 1222484b3669SPaul Moore cat_high, 1223484b3669SPaul Moore GFP_ATOMIC); 1224484b3669SPaul Moore if (ret_val != 0) 1225484b3669SPaul Moore return ret_val; 1226484b3669SPaul Moore } 1227484b3669SPaul Moore 1228484b3669SPaul Moore return 0; 1229484b3669SPaul Moore } 1230484b3669SPaul Moore 1231446fda4fSPaul Moore /* 1232446fda4fSPaul Moore * Protocol Handling Functions 1233446fda4fSPaul Moore */ 1234446fda4fSPaul Moore 1235446fda4fSPaul Moore /** 1236446fda4fSPaul Moore * cipso_v4_gentag_hdr - Generate a CIPSO option header 1237446fda4fSPaul Moore * @doi_def: the DOI definition 123891b1ed0aSPaul Moore * @len: the total tag length in bytes, not including this header 1239446fda4fSPaul Moore * @buf: the CIPSO option buffer 1240446fda4fSPaul Moore * 1241446fda4fSPaul Moore * Description: 124291b1ed0aSPaul Moore * Write a CIPSO header into the beginning of @buffer. 1243446fda4fSPaul Moore * 1244446fda4fSPaul Moore */ 124591b1ed0aSPaul Moore static void cipso_v4_gentag_hdr(const struct cipso_v4_doi *doi_def, 124691b1ed0aSPaul Moore unsigned char *buf, 124791b1ed0aSPaul Moore u32 len) 1248446fda4fSPaul Moore { 1249446fda4fSPaul Moore buf[0] = IPOPT_CIPSO; 1250446fda4fSPaul Moore buf[1] = CIPSO_V4_HDR_LEN + len; 1251714e85beSAl Viro *(__be32 *)&buf[2] = htonl(doi_def->doi); 1252446fda4fSPaul Moore } 1253446fda4fSPaul Moore 1254446fda4fSPaul Moore /** 1255446fda4fSPaul Moore * cipso_v4_gentag_rbm - Generate a CIPSO restricted bitmap tag (type #1) 1256446fda4fSPaul Moore * @doi_def: the DOI definition 1257446fda4fSPaul Moore * @secattr: the security attributes 1258446fda4fSPaul Moore * @buffer: the option buffer 1259446fda4fSPaul Moore * @buffer_len: length of buffer in bytes 1260446fda4fSPaul Moore * 1261446fda4fSPaul Moore * Description: 1262446fda4fSPaul Moore * Generate a CIPSO option using the restricted bitmap tag, tag type #1. The 1263446fda4fSPaul Moore * actual buffer length may be larger than the indicated size due to 126491b1ed0aSPaul Moore * translation between host and network category bitmaps. Returns the size of 126591b1ed0aSPaul Moore * the tag on success, negative values on failure. 1266446fda4fSPaul Moore * 1267446fda4fSPaul Moore */ 1268446fda4fSPaul Moore static int cipso_v4_gentag_rbm(const struct cipso_v4_doi *doi_def, 1269446fda4fSPaul Moore const struct netlbl_lsm_secattr *secattr, 127091b1ed0aSPaul Moore unsigned char *buffer, 127191b1ed0aSPaul Moore u32 buffer_len) 1272446fda4fSPaul Moore { 1273701a90baSPaul Moore int ret_val; 127491b1ed0aSPaul Moore u32 tag_len; 1275446fda4fSPaul Moore u32 level; 1276446fda4fSPaul Moore 1277701a90baSPaul Moore if ((secattr->flags & NETLBL_SECATTR_MLS_LVL) == 0) 1278701a90baSPaul Moore return -EPERM; 1279701a90baSPaul Moore 128016efd454SPaul Moore ret_val = cipso_v4_map_lvl_hton(doi_def, 128116efd454SPaul Moore secattr->attr.mls.lvl, 128216efd454SPaul Moore &level); 128391b1ed0aSPaul Moore if (ret_val != 0) 128491b1ed0aSPaul Moore return ret_val; 1285446fda4fSPaul Moore 128691b1ed0aSPaul Moore if (secattr->flags & NETLBL_SECATTR_MLS_CAT) { 1287446fda4fSPaul Moore ret_val = cipso_v4_map_cat_rbm_hton(doi_def, 128802752760SPaul Moore secattr, 128991b1ed0aSPaul Moore &buffer[4], 129091b1ed0aSPaul Moore buffer_len - 4); 1291446fda4fSPaul Moore if (ret_val < 0) 129291b1ed0aSPaul Moore return ret_val; 1293446fda4fSPaul Moore 1294446fda4fSPaul Moore /* This will send packets using the "optimized" format when 129525985edcSLucas De Marchi * possible as specified in section 3.4.2.6 of the 1296446fda4fSPaul Moore * CIPSO draft. */ 1297701a90baSPaul Moore if (cipso_v4_rbm_optfmt && ret_val > 0 && ret_val <= 10) 129891b1ed0aSPaul Moore tag_len = 14; 1299701a90baSPaul Moore else 130091b1ed0aSPaul Moore tag_len = 4 + ret_val; 130191b1ed0aSPaul Moore } else 130291b1ed0aSPaul Moore tag_len = 4; 1303446fda4fSPaul Moore 130415c45f7bSPaul Moore buffer[0] = CIPSO_V4_TAG_RBITMAP; 130591b1ed0aSPaul Moore buffer[1] = tag_len; 130691b1ed0aSPaul Moore buffer[3] = level; 1307446fda4fSPaul Moore 130891b1ed0aSPaul Moore return tag_len; 1309446fda4fSPaul Moore } 1310446fda4fSPaul Moore 1311446fda4fSPaul Moore /** 1312446fda4fSPaul Moore * cipso_v4_parsetag_rbm - Parse a CIPSO restricted bitmap tag 1313446fda4fSPaul Moore * @doi_def: the DOI definition 1314446fda4fSPaul Moore * @tag: the CIPSO tag 1315446fda4fSPaul Moore * @secattr: the security attributes 1316446fda4fSPaul Moore * 1317446fda4fSPaul Moore * Description: 1318446fda4fSPaul Moore * Parse a CIPSO restricted bitmap tag (tag type #1) and return the security 1319446fda4fSPaul Moore * attributes in @secattr. Return zero on success, negatives values on 1320446fda4fSPaul Moore * failure. 1321446fda4fSPaul Moore * 1322446fda4fSPaul Moore */ 1323446fda4fSPaul Moore static int cipso_v4_parsetag_rbm(const struct cipso_v4_doi *doi_def, 1324446fda4fSPaul Moore const unsigned char *tag, 1325446fda4fSPaul Moore struct netlbl_lsm_secattr *secattr) 1326446fda4fSPaul Moore { 1327446fda4fSPaul Moore int ret_val; 1328446fda4fSPaul Moore u8 tag_len = tag[1]; 1329446fda4fSPaul Moore u32 level; 1330446fda4fSPaul Moore 1331446fda4fSPaul Moore ret_val = cipso_v4_map_lvl_ntoh(doi_def, tag[3], &level); 1332446fda4fSPaul Moore if (ret_val != 0) 1333446fda4fSPaul Moore return ret_val; 133416efd454SPaul Moore secattr->attr.mls.lvl = level; 1335701a90baSPaul Moore secattr->flags |= NETLBL_SECATTR_MLS_LVL; 1336446fda4fSPaul Moore 1337446fda4fSPaul Moore if (tag_len > 4) { 133816efd454SPaul Moore secattr->attr.mls.cat = 133916efd454SPaul Moore netlbl_secattr_catmap_alloc(GFP_ATOMIC); 134016efd454SPaul Moore if (secattr->attr.mls.cat == NULL) 1341446fda4fSPaul Moore return -ENOMEM; 1342446fda4fSPaul Moore 1343446fda4fSPaul Moore ret_val = cipso_v4_map_cat_rbm_ntoh(doi_def, 1344446fda4fSPaul Moore &tag[4], 1345446fda4fSPaul Moore tag_len - 4, 134602752760SPaul Moore secattr); 134702752760SPaul Moore if (ret_val != 0) { 134816efd454SPaul Moore netlbl_secattr_catmap_free(secattr->attr.mls.cat); 1349446fda4fSPaul Moore return ret_val; 1350701a90baSPaul Moore } 135102752760SPaul Moore 135202752760SPaul Moore secattr->flags |= NETLBL_SECATTR_MLS_CAT; 1353446fda4fSPaul Moore } 1354446fda4fSPaul Moore 1355446fda4fSPaul Moore return 0; 1356446fda4fSPaul Moore } 1357446fda4fSPaul Moore 1358446fda4fSPaul Moore /** 1359654bbc2aSPaul Moore * cipso_v4_gentag_enum - Generate a CIPSO enumerated tag (type #2) 1360654bbc2aSPaul Moore * @doi_def: the DOI definition 1361654bbc2aSPaul Moore * @secattr: the security attributes 1362654bbc2aSPaul Moore * @buffer: the option buffer 1363654bbc2aSPaul Moore * @buffer_len: length of buffer in bytes 1364654bbc2aSPaul Moore * 1365654bbc2aSPaul Moore * Description: 1366654bbc2aSPaul Moore * Generate a CIPSO option using the enumerated tag, tag type #2. Returns the 1367654bbc2aSPaul Moore * size of the tag on success, negative values on failure. 1368654bbc2aSPaul Moore * 1369654bbc2aSPaul Moore */ 1370654bbc2aSPaul Moore static int cipso_v4_gentag_enum(const struct cipso_v4_doi *doi_def, 1371654bbc2aSPaul Moore const struct netlbl_lsm_secattr *secattr, 1372654bbc2aSPaul Moore unsigned char *buffer, 1373654bbc2aSPaul Moore u32 buffer_len) 1374654bbc2aSPaul Moore { 1375654bbc2aSPaul Moore int ret_val; 1376654bbc2aSPaul Moore u32 tag_len; 1377654bbc2aSPaul Moore u32 level; 1378654bbc2aSPaul Moore 1379654bbc2aSPaul Moore if (!(secattr->flags & NETLBL_SECATTR_MLS_LVL)) 1380654bbc2aSPaul Moore return -EPERM; 1381654bbc2aSPaul Moore 138216efd454SPaul Moore ret_val = cipso_v4_map_lvl_hton(doi_def, 138316efd454SPaul Moore secattr->attr.mls.lvl, 138416efd454SPaul Moore &level); 1385654bbc2aSPaul Moore if (ret_val != 0) 1386654bbc2aSPaul Moore return ret_val; 1387654bbc2aSPaul Moore 1388654bbc2aSPaul Moore if (secattr->flags & NETLBL_SECATTR_MLS_CAT) { 1389654bbc2aSPaul Moore ret_val = cipso_v4_map_cat_enum_hton(doi_def, 1390654bbc2aSPaul Moore secattr, 1391654bbc2aSPaul Moore &buffer[4], 1392654bbc2aSPaul Moore buffer_len - 4); 1393654bbc2aSPaul Moore if (ret_val < 0) 1394654bbc2aSPaul Moore return ret_val; 1395654bbc2aSPaul Moore 1396654bbc2aSPaul Moore tag_len = 4 + ret_val; 1397654bbc2aSPaul Moore } else 1398654bbc2aSPaul Moore tag_len = 4; 1399654bbc2aSPaul Moore 140015c45f7bSPaul Moore buffer[0] = CIPSO_V4_TAG_ENUM; 1401654bbc2aSPaul Moore buffer[1] = tag_len; 1402654bbc2aSPaul Moore buffer[3] = level; 1403654bbc2aSPaul Moore 1404654bbc2aSPaul Moore return tag_len; 1405654bbc2aSPaul Moore } 1406654bbc2aSPaul Moore 1407654bbc2aSPaul Moore /** 1408654bbc2aSPaul Moore * cipso_v4_parsetag_enum - Parse a CIPSO enumerated tag 1409654bbc2aSPaul Moore * @doi_def: the DOI definition 1410654bbc2aSPaul Moore * @tag: the CIPSO tag 1411654bbc2aSPaul Moore * @secattr: the security attributes 1412654bbc2aSPaul Moore * 1413654bbc2aSPaul Moore * Description: 1414654bbc2aSPaul Moore * Parse a CIPSO enumerated tag (tag type #2) and return the security 1415654bbc2aSPaul Moore * attributes in @secattr. Return zero on success, negatives values on 1416654bbc2aSPaul Moore * failure. 1417654bbc2aSPaul Moore * 1418654bbc2aSPaul Moore */ 1419654bbc2aSPaul Moore static int cipso_v4_parsetag_enum(const struct cipso_v4_doi *doi_def, 1420654bbc2aSPaul Moore const unsigned char *tag, 1421654bbc2aSPaul Moore struct netlbl_lsm_secattr *secattr) 1422654bbc2aSPaul Moore { 1423654bbc2aSPaul Moore int ret_val; 1424654bbc2aSPaul Moore u8 tag_len = tag[1]; 1425654bbc2aSPaul Moore u32 level; 1426654bbc2aSPaul Moore 1427654bbc2aSPaul Moore ret_val = cipso_v4_map_lvl_ntoh(doi_def, tag[3], &level); 1428654bbc2aSPaul Moore if (ret_val != 0) 1429654bbc2aSPaul Moore return ret_val; 143016efd454SPaul Moore secattr->attr.mls.lvl = level; 1431654bbc2aSPaul Moore secattr->flags |= NETLBL_SECATTR_MLS_LVL; 1432654bbc2aSPaul Moore 1433654bbc2aSPaul Moore if (tag_len > 4) { 143416efd454SPaul Moore secattr->attr.mls.cat = 143516efd454SPaul Moore netlbl_secattr_catmap_alloc(GFP_ATOMIC); 143616efd454SPaul Moore if (secattr->attr.mls.cat == NULL) 1437654bbc2aSPaul Moore return -ENOMEM; 1438654bbc2aSPaul Moore 1439654bbc2aSPaul Moore ret_val = cipso_v4_map_cat_enum_ntoh(doi_def, 1440654bbc2aSPaul Moore &tag[4], 1441654bbc2aSPaul Moore tag_len - 4, 1442654bbc2aSPaul Moore secattr); 1443654bbc2aSPaul Moore if (ret_val != 0) { 144416efd454SPaul Moore netlbl_secattr_catmap_free(secattr->attr.mls.cat); 1445654bbc2aSPaul Moore return ret_val; 1446654bbc2aSPaul Moore } 1447654bbc2aSPaul Moore 1448654bbc2aSPaul Moore secattr->flags |= NETLBL_SECATTR_MLS_CAT; 1449654bbc2aSPaul Moore } 1450654bbc2aSPaul Moore 1451654bbc2aSPaul Moore return 0; 1452654bbc2aSPaul Moore } 1453654bbc2aSPaul Moore 1454654bbc2aSPaul Moore /** 1455484b3669SPaul Moore * cipso_v4_gentag_rng - Generate a CIPSO ranged tag (type #5) 1456484b3669SPaul Moore * @doi_def: the DOI definition 1457484b3669SPaul Moore * @secattr: the security attributes 1458484b3669SPaul Moore * @buffer: the option buffer 1459484b3669SPaul Moore * @buffer_len: length of buffer in bytes 1460484b3669SPaul Moore * 1461484b3669SPaul Moore * Description: 1462484b3669SPaul Moore * Generate a CIPSO option using the ranged tag, tag type #5. Returns the 1463484b3669SPaul Moore * size of the tag on success, negative values on failure. 1464484b3669SPaul Moore * 1465484b3669SPaul Moore */ 1466484b3669SPaul Moore static int cipso_v4_gentag_rng(const struct cipso_v4_doi *doi_def, 1467484b3669SPaul Moore const struct netlbl_lsm_secattr *secattr, 1468484b3669SPaul Moore unsigned char *buffer, 1469484b3669SPaul Moore u32 buffer_len) 1470484b3669SPaul Moore { 1471484b3669SPaul Moore int ret_val; 1472484b3669SPaul Moore u32 tag_len; 1473484b3669SPaul Moore u32 level; 1474484b3669SPaul Moore 1475484b3669SPaul Moore if (!(secattr->flags & NETLBL_SECATTR_MLS_LVL)) 1476484b3669SPaul Moore return -EPERM; 1477484b3669SPaul Moore 147816efd454SPaul Moore ret_val = cipso_v4_map_lvl_hton(doi_def, 147916efd454SPaul Moore secattr->attr.mls.lvl, 148016efd454SPaul Moore &level); 1481484b3669SPaul Moore if (ret_val != 0) 1482484b3669SPaul Moore return ret_val; 1483484b3669SPaul Moore 1484484b3669SPaul Moore if (secattr->flags & NETLBL_SECATTR_MLS_CAT) { 1485484b3669SPaul Moore ret_val = cipso_v4_map_cat_rng_hton(doi_def, 1486484b3669SPaul Moore secattr, 1487484b3669SPaul Moore &buffer[4], 1488484b3669SPaul Moore buffer_len - 4); 1489484b3669SPaul Moore if (ret_val < 0) 1490484b3669SPaul Moore return ret_val; 1491484b3669SPaul Moore 1492484b3669SPaul Moore tag_len = 4 + ret_val; 1493484b3669SPaul Moore } else 1494484b3669SPaul Moore tag_len = 4; 1495484b3669SPaul Moore 149615c45f7bSPaul Moore buffer[0] = CIPSO_V4_TAG_RANGE; 1497484b3669SPaul Moore buffer[1] = tag_len; 1498484b3669SPaul Moore buffer[3] = level; 1499484b3669SPaul Moore 1500484b3669SPaul Moore return tag_len; 1501484b3669SPaul Moore } 1502484b3669SPaul Moore 1503484b3669SPaul Moore /** 1504484b3669SPaul Moore * cipso_v4_parsetag_rng - Parse a CIPSO ranged tag 1505484b3669SPaul Moore * @doi_def: the DOI definition 1506484b3669SPaul Moore * @tag: the CIPSO tag 1507484b3669SPaul Moore * @secattr: the security attributes 1508484b3669SPaul Moore * 1509484b3669SPaul Moore * Description: 1510484b3669SPaul Moore * Parse a CIPSO ranged tag (tag type #5) and return the security attributes 1511484b3669SPaul Moore * in @secattr. Return zero on success, negatives values on failure. 1512484b3669SPaul Moore * 1513484b3669SPaul Moore */ 1514484b3669SPaul Moore static int cipso_v4_parsetag_rng(const struct cipso_v4_doi *doi_def, 1515484b3669SPaul Moore const unsigned char *tag, 1516484b3669SPaul Moore struct netlbl_lsm_secattr *secattr) 1517484b3669SPaul Moore { 1518484b3669SPaul Moore int ret_val; 1519484b3669SPaul Moore u8 tag_len = tag[1]; 1520484b3669SPaul Moore u32 level; 1521484b3669SPaul Moore 1522484b3669SPaul Moore ret_val = cipso_v4_map_lvl_ntoh(doi_def, tag[3], &level); 1523484b3669SPaul Moore if (ret_val != 0) 1524484b3669SPaul Moore return ret_val; 152516efd454SPaul Moore secattr->attr.mls.lvl = level; 1526484b3669SPaul Moore secattr->flags |= NETLBL_SECATTR_MLS_LVL; 1527484b3669SPaul Moore 1528484b3669SPaul Moore if (tag_len > 4) { 152916efd454SPaul Moore secattr->attr.mls.cat = 153016efd454SPaul Moore netlbl_secattr_catmap_alloc(GFP_ATOMIC); 153116efd454SPaul Moore if (secattr->attr.mls.cat == NULL) 1532484b3669SPaul Moore return -ENOMEM; 1533484b3669SPaul Moore 1534484b3669SPaul Moore ret_val = cipso_v4_map_cat_rng_ntoh(doi_def, 1535484b3669SPaul Moore &tag[4], 1536484b3669SPaul Moore tag_len - 4, 1537484b3669SPaul Moore secattr); 1538484b3669SPaul Moore if (ret_val != 0) { 153916efd454SPaul Moore netlbl_secattr_catmap_free(secattr->attr.mls.cat); 1540484b3669SPaul Moore return ret_val; 1541484b3669SPaul Moore } 1542484b3669SPaul Moore 1543484b3669SPaul Moore secattr->flags |= NETLBL_SECATTR_MLS_CAT; 1544484b3669SPaul Moore } 1545484b3669SPaul Moore 1546484b3669SPaul Moore return 0; 1547484b3669SPaul Moore } 1548484b3669SPaul Moore 1549484b3669SPaul Moore /** 155015c45f7bSPaul Moore * cipso_v4_gentag_loc - Generate a CIPSO local tag (non-standard) 155115c45f7bSPaul Moore * @doi_def: the DOI definition 155215c45f7bSPaul Moore * @secattr: the security attributes 155315c45f7bSPaul Moore * @buffer: the option buffer 155415c45f7bSPaul Moore * @buffer_len: length of buffer in bytes 155515c45f7bSPaul Moore * 155615c45f7bSPaul Moore * Description: 155715c45f7bSPaul Moore * Generate a CIPSO option using the local tag. Returns the size of the tag 155815c45f7bSPaul Moore * on success, negative values on failure. 155915c45f7bSPaul Moore * 156015c45f7bSPaul Moore */ 156115c45f7bSPaul Moore static int cipso_v4_gentag_loc(const struct cipso_v4_doi *doi_def, 156215c45f7bSPaul Moore const struct netlbl_lsm_secattr *secattr, 156315c45f7bSPaul Moore unsigned char *buffer, 156415c45f7bSPaul Moore u32 buffer_len) 156515c45f7bSPaul Moore { 156615c45f7bSPaul Moore if (!(secattr->flags & NETLBL_SECATTR_SECID)) 156715c45f7bSPaul Moore return -EPERM; 156815c45f7bSPaul Moore 156915c45f7bSPaul Moore buffer[0] = CIPSO_V4_TAG_LOCAL; 157015c45f7bSPaul Moore buffer[1] = CIPSO_V4_TAG_LOC_BLEN; 157115c45f7bSPaul Moore *(u32 *)&buffer[2] = secattr->attr.secid; 157215c45f7bSPaul Moore 157315c45f7bSPaul Moore return CIPSO_V4_TAG_LOC_BLEN; 157415c45f7bSPaul Moore } 157515c45f7bSPaul Moore 157615c45f7bSPaul Moore /** 157715c45f7bSPaul Moore * cipso_v4_parsetag_loc - Parse a CIPSO local tag 157815c45f7bSPaul Moore * @doi_def: the DOI definition 157915c45f7bSPaul Moore * @tag: the CIPSO tag 158015c45f7bSPaul Moore * @secattr: the security attributes 158115c45f7bSPaul Moore * 158215c45f7bSPaul Moore * Description: 158315c45f7bSPaul Moore * Parse a CIPSO local tag and return the security attributes in @secattr. 158415c45f7bSPaul Moore * Return zero on success, negatives values on failure. 158515c45f7bSPaul Moore * 158615c45f7bSPaul Moore */ 158715c45f7bSPaul Moore static int cipso_v4_parsetag_loc(const struct cipso_v4_doi *doi_def, 158815c45f7bSPaul Moore const unsigned char *tag, 158915c45f7bSPaul Moore struct netlbl_lsm_secattr *secattr) 159015c45f7bSPaul Moore { 159115c45f7bSPaul Moore secattr->attr.secid = *(u32 *)&tag[2]; 159215c45f7bSPaul Moore secattr->flags |= NETLBL_SECATTR_SECID; 159315c45f7bSPaul Moore 159415c45f7bSPaul Moore return 0; 159515c45f7bSPaul Moore } 159615c45f7bSPaul Moore 159715c45f7bSPaul Moore /** 1598446fda4fSPaul Moore * cipso_v4_validate - Validate a CIPSO option 1599446fda4fSPaul Moore * @option: the start of the option, on error it is set to point to the error 1600446fda4fSPaul Moore * 1601446fda4fSPaul Moore * Description: 1602446fda4fSPaul Moore * This routine is called to validate a CIPSO option, it checks all of the 1603446fda4fSPaul Moore * fields to ensure that they are at least valid, see the draft snippet below 1604446fda4fSPaul Moore * for details. If the option is valid then a zero value is returned and 1605446fda4fSPaul Moore * the value of @option is unchanged. If the option is invalid then a 1606446fda4fSPaul Moore * non-zero value is returned and @option is adjusted to point to the 1607446fda4fSPaul Moore * offending portion of the option. From the IETF draft ... 1608446fda4fSPaul Moore * 1609446fda4fSPaul Moore * "If any field within the CIPSO options, such as the DOI identifier, is not 1610446fda4fSPaul Moore * recognized the IP datagram is discarded and an ICMP 'parameter problem' 1611446fda4fSPaul Moore * (type 12) is generated and returned. The ICMP code field is set to 'bad 1612446fda4fSPaul Moore * parameter' (code 0) and the pointer is set to the start of the CIPSO field 1613446fda4fSPaul Moore * that is unrecognized." 1614446fda4fSPaul Moore * 1615446fda4fSPaul Moore */ 161615c45f7bSPaul Moore int cipso_v4_validate(const struct sk_buff *skb, unsigned char **option) 1617446fda4fSPaul Moore { 1618446fda4fSPaul Moore unsigned char *opt = *option; 1619446fda4fSPaul Moore unsigned char *tag; 1620446fda4fSPaul Moore unsigned char opt_iter; 1621446fda4fSPaul Moore unsigned char err_offset = 0; 1622446fda4fSPaul Moore u8 opt_len; 1623446fda4fSPaul Moore u8 tag_len; 1624446fda4fSPaul Moore struct cipso_v4_doi *doi_def = NULL; 1625446fda4fSPaul Moore u32 tag_iter; 1626446fda4fSPaul Moore 1627446fda4fSPaul Moore /* caller already checks for length values that are too large */ 1628446fda4fSPaul Moore opt_len = opt[1]; 1629446fda4fSPaul Moore if (opt_len < 8) { 1630446fda4fSPaul Moore err_offset = 1; 1631446fda4fSPaul Moore goto validate_return; 1632446fda4fSPaul Moore } 1633446fda4fSPaul Moore 1634446fda4fSPaul Moore rcu_read_lock(); 1635d3e2ce3bSHarvey Harrison doi_def = cipso_v4_doi_search(get_unaligned_be32(&opt[2])); 1636446fda4fSPaul Moore if (doi_def == NULL) { 1637446fda4fSPaul Moore err_offset = 2; 1638446fda4fSPaul Moore goto validate_return_locked; 1639446fda4fSPaul Moore } 1640446fda4fSPaul Moore 164115c45f7bSPaul Moore opt_iter = CIPSO_V4_HDR_LEN; 1642446fda4fSPaul Moore tag = opt + opt_iter; 1643446fda4fSPaul Moore while (opt_iter < opt_len) { 1644446fda4fSPaul Moore for (tag_iter = 0; doi_def->tags[tag_iter] != tag[0];) 1645446fda4fSPaul Moore if (doi_def->tags[tag_iter] == CIPSO_V4_TAG_INVALID || 1646446fda4fSPaul Moore ++tag_iter == CIPSO_V4_TAG_MAXCNT) { 1647446fda4fSPaul Moore err_offset = opt_iter; 1648446fda4fSPaul Moore goto validate_return_locked; 1649446fda4fSPaul Moore } 1650446fda4fSPaul Moore 1651446fda4fSPaul Moore tag_len = tag[1]; 1652446fda4fSPaul Moore if (tag_len > (opt_len - opt_iter)) { 1653446fda4fSPaul Moore err_offset = opt_iter + 1; 1654446fda4fSPaul Moore goto validate_return_locked; 1655446fda4fSPaul Moore } 1656446fda4fSPaul Moore 1657446fda4fSPaul Moore switch (tag[0]) { 1658446fda4fSPaul Moore case CIPSO_V4_TAG_RBITMAP: 165915c45f7bSPaul Moore if (tag_len < CIPSO_V4_TAG_RBM_BLEN) { 1660446fda4fSPaul Moore err_offset = opt_iter + 1; 1661446fda4fSPaul Moore goto validate_return_locked; 1662446fda4fSPaul Moore } 1663446fda4fSPaul Moore 1664446fda4fSPaul Moore /* We are already going to do all the verification 1665446fda4fSPaul Moore * necessary at the socket layer so from our point of 1666446fda4fSPaul Moore * view it is safe to turn these checks off (and less 1667446fda4fSPaul Moore * work), however, the CIPSO draft says we should do 1668446fda4fSPaul Moore * all the CIPSO validations here but it doesn't 1669446fda4fSPaul Moore * really specify _exactly_ what we need to validate 1670446fda4fSPaul Moore * ... so, just make it a sysctl tunable. */ 1671446fda4fSPaul Moore if (cipso_v4_rbm_strictvalid) { 1672446fda4fSPaul Moore if (cipso_v4_map_lvl_valid(doi_def, 1673446fda4fSPaul Moore tag[3]) < 0) { 1674446fda4fSPaul Moore err_offset = opt_iter + 3; 1675446fda4fSPaul Moore goto validate_return_locked; 1676446fda4fSPaul Moore } 167715c45f7bSPaul Moore if (tag_len > CIPSO_V4_TAG_RBM_BLEN && 1678446fda4fSPaul Moore cipso_v4_map_cat_rbm_valid(doi_def, 1679446fda4fSPaul Moore &tag[4], 1680446fda4fSPaul Moore tag_len - 4) < 0) { 1681446fda4fSPaul Moore err_offset = opt_iter + 4; 1682446fda4fSPaul Moore goto validate_return_locked; 1683446fda4fSPaul Moore } 1684446fda4fSPaul Moore } 1685446fda4fSPaul Moore break; 1686654bbc2aSPaul Moore case CIPSO_V4_TAG_ENUM: 168715c45f7bSPaul Moore if (tag_len < CIPSO_V4_TAG_ENUM_BLEN) { 1688654bbc2aSPaul Moore err_offset = opt_iter + 1; 1689654bbc2aSPaul Moore goto validate_return_locked; 1690654bbc2aSPaul Moore } 1691654bbc2aSPaul Moore 1692654bbc2aSPaul Moore if (cipso_v4_map_lvl_valid(doi_def, 1693654bbc2aSPaul Moore tag[3]) < 0) { 1694654bbc2aSPaul Moore err_offset = opt_iter + 3; 1695654bbc2aSPaul Moore goto validate_return_locked; 1696654bbc2aSPaul Moore } 169715c45f7bSPaul Moore if (tag_len > CIPSO_V4_TAG_ENUM_BLEN && 1698654bbc2aSPaul Moore cipso_v4_map_cat_enum_valid(doi_def, 1699654bbc2aSPaul Moore &tag[4], 1700654bbc2aSPaul Moore tag_len - 4) < 0) { 1701654bbc2aSPaul Moore err_offset = opt_iter + 4; 1702654bbc2aSPaul Moore goto validate_return_locked; 1703654bbc2aSPaul Moore } 1704654bbc2aSPaul Moore break; 1705484b3669SPaul Moore case CIPSO_V4_TAG_RANGE: 170615c45f7bSPaul Moore if (tag_len < CIPSO_V4_TAG_RNG_BLEN) { 1707484b3669SPaul Moore err_offset = opt_iter + 1; 1708484b3669SPaul Moore goto validate_return_locked; 1709484b3669SPaul Moore } 1710484b3669SPaul Moore 1711484b3669SPaul Moore if (cipso_v4_map_lvl_valid(doi_def, 1712484b3669SPaul Moore tag[3]) < 0) { 1713484b3669SPaul Moore err_offset = opt_iter + 3; 1714484b3669SPaul Moore goto validate_return_locked; 1715484b3669SPaul Moore } 171615c45f7bSPaul Moore if (tag_len > CIPSO_V4_TAG_RNG_BLEN && 1717484b3669SPaul Moore cipso_v4_map_cat_rng_valid(doi_def, 1718484b3669SPaul Moore &tag[4], 1719484b3669SPaul Moore tag_len - 4) < 0) { 1720484b3669SPaul Moore err_offset = opt_iter + 4; 1721484b3669SPaul Moore goto validate_return_locked; 1722484b3669SPaul Moore } 1723484b3669SPaul Moore break; 172415c45f7bSPaul Moore case CIPSO_V4_TAG_LOCAL: 172515c45f7bSPaul Moore /* This is a non-standard tag that we only allow for 172615c45f7bSPaul Moore * local connections, so if the incoming interface is 172789d7ae34SPaul Moore * not the loopback device drop the packet. Further, 172889d7ae34SPaul Moore * there is no legitimate reason for setting this from 172989d7ae34SPaul Moore * userspace so reject it if skb is NULL. */ 173089d7ae34SPaul Moore if (skb == NULL || !(skb->dev->flags & IFF_LOOPBACK)) { 173115c45f7bSPaul Moore err_offset = opt_iter; 173215c45f7bSPaul Moore goto validate_return_locked; 173315c45f7bSPaul Moore } 173415c45f7bSPaul Moore if (tag_len != CIPSO_V4_TAG_LOC_BLEN) { 173515c45f7bSPaul Moore err_offset = opt_iter + 1; 173615c45f7bSPaul Moore goto validate_return_locked; 173715c45f7bSPaul Moore } 173815c45f7bSPaul Moore break; 1739446fda4fSPaul Moore default: 1740446fda4fSPaul Moore err_offset = opt_iter; 1741446fda4fSPaul Moore goto validate_return_locked; 1742446fda4fSPaul Moore } 1743446fda4fSPaul Moore 1744446fda4fSPaul Moore tag += tag_len; 1745446fda4fSPaul Moore opt_iter += tag_len; 1746446fda4fSPaul Moore } 1747446fda4fSPaul Moore 1748446fda4fSPaul Moore validate_return_locked: 1749446fda4fSPaul Moore rcu_read_unlock(); 1750446fda4fSPaul Moore validate_return: 1751446fda4fSPaul Moore *option = opt + err_offset; 1752446fda4fSPaul Moore return err_offset; 1753446fda4fSPaul Moore } 1754446fda4fSPaul Moore 1755446fda4fSPaul Moore /** 175625985edcSLucas De Marchi * cipso_v4_error - Send the correct response for a bad packet 1757446fda4fSPaul Moore * @skb: the packet 1758446fda4fSPaul Moore * @error: the error code 1759446fda4fSPaul Moore * @gateway: CIPSO gateway flag 1760446fda4fSPaul Moore * 1761446fda4fSPaul Moore * Description: 1762446fda4fSPaul Moore * Based on the error code given in @error, send an ICMP error message back to 1763446fda4fSPaul Moore * the originating host. From the IETF draft ... 1764446fda4fSPaul Moore * 1765446fda4fSPaul Moore * "If the contents of the CIPSO [option] are valid but the security label is 1766446fda4fSPaul Moore * outside of the configured host or port label range, the datagram is 1767446fda4fSPaul Moore * discarded and an ICMP 'destination unreachable' (type 3) is generated and 1768446fda4fSPaul Moore * returned. The code field of the ICMP is set to 'communication with 1769446fda4fSPaul Moore * destination network administratively prohibited' (code 9) or to 1770446fda4fSPaul Moore * 'communication with destination host administratively prohibited' 1771446fda4fSPaul Moore * (code 10). The value of the code is dependent on whether the originator 1772446fda4fSPaul Moore * of the ICMP message is acting as a CIPSO host or a CIPSO gateway. The 1773446fda4fSPaul Moore * recipient of the ICMP message MUST be able to handle either value. The 1774446fda4fSPaul Moore * same procedure is performed if a CIPSO [option] can not be added to an 1775446fda4fSPaul Moore * IP packet because it is too large to fit in the IP options area." 1776446fda4fSPaul Moore * 1777446fda4fSPaul Moore * "If the error is triggered by receipt of an ICMP message, the message is 1778446fda4fSPaul Moore * discarded and no response is permitted (consistent with general ICMP 1779446fda4fSPaul Moore * processing rules)." 1780446fda4fSPaul Moore * 1781446fda4fSPaul Moore */ 1782446fda4fSPaul Moore void cipso_v4_error(struct sk_buff *skb, int error, u32 gateway) 1783446fda4fSPaul Moore { 1784eddc9ec5SArnaldo Carvalho de Melo if (ip_hdr(skb)->protocol == IPPROTO_ICMP || error != -EACCES) 1785446fda4fSPaul Moore return; 1786446fda4fSPaul Moore 1787446fda4fSPaul Moore if (gateway) 1788446fda4fSPaul Moore icmp_send(skb, ICMP_DEST_UNREACH, ICMP_NET_ANO, 0); 1789446fda4fSPaul Moore else 1790446fda4fSPaul Moore icmp_send(skb, ICMP_DEST_UNREACH, ICMP_HOST_ANO, 0); 1791446fda4fSPaul Moore } 1792446fda4fSPaul Moore 1793446fda4fSPaul Moore /** 1794948bf85cSPaul Moore * cipso_v4_genopt - Generate a CIPSO option 1795948bf85cSPaul Moore * @buf: the option buffer 1796948bf85cSPaul Moore * @buf_len: the size of opt_buf 1797446fda4fSPaul Moore * @doi_def: the CIPSO DOI to use 1798948bf85cSPaul Moore * @secattr: the security attributes 1799446fda4fSPaul Moore * 1800446fda4fSPaul Moore * Description: 1801948bf85cSPaul Moore * Generate a CIPSO option using the DOI definition and security attributes 1802948bf85cSPaul Moore * passed to the function. Returns the length of the option on success and 1803948bf85cSPaul Moore * negative values on failure. 1804446fda4fSPaul Moore * 1805446fda4fSPaul Moore */ 1806948bf85cSPaul Moore static int cipso_v4_genopt(unsigned char *buf, u32 buf_len, 1807446fda4fSPaul Moore const struct cipso_v4_doi *doi_def, 1808446fda4fSPaul Moore const struct netlbl_lsm_secattr *secattr) 1809446fda4fSPaul Moore { 1810948bf85cSPaul Moore int ret_val; 1811446fda4fSPaul Moore u32 iter; 1812446fda4fSPaul Moore 1813948bf85cSPaul Moore if (buf_len <= CIPSO_V4_HDR_LEN) 1814948bf85cSPaul Moore return -ENOSPC; 181591b1ed0aSPaul Moore 1816446fda4fSPaul Moore /* XXX - This code assumes only one tag per CIPSO option which isn't 1817446fda4fSPaul Moore * really a good assumption to make but since we only support the MAC 1818446fda4fSPaul Moore * tags right now it is a safe assumption. */ 1819446fda4fSPaul Moore iter = 0; 1820446fda4fSPaul Moore do { 182191b1ed0aSPaul Moore memset(buf, 0, buf_len); 1822446fda4fSPaul Moore switch (doi_def->tags[iter]) { 1823446fda4fSPaul Moore case CIPSO_V4_TAG_RBITMAP: 1824446fda4fSPaul Moore ret_val = cipso_v4_gentag_rbm(doi_def, 1825446fda4fSPaul Moore secattr, 182691b1ed0aSPaul Moore &buf[CIPSO_V4_HDR_LEN], 182791b1ed0aSPaul Moore buf_len - CIPSO_V4_HDR_LEN); 1828446fda4fSPaul Moore break; 1829654bbc2aSPaul Moore case CIPSO_V4_TAG_ENUM: 1830654bbc2aSPaul Moore ret_val = cipso_v4_gentag_enum(doi_def, 1831654bbc2aSPaul Moore secattr, 1832654bbc2aSPaul Moore &buf[CIPSO_V4_HDR_LEN], 1833654bbc2aSPaul Moore buf_len - CIPSO_V4_HDR_LEN); 1834654bbc2aSPaul Moore break; 1835484b3669SPaul Moore case CIPSO_V4_TAG_RANGE: 1836484b3669SPaul Moore ret_val = cipso_v4_gentag_rng(doi_def, 1837484b3669SPaul Moore secattr, 1838484b3669SPaul Moore &buf[CIPSO_V4_HDR_LEN], 1839484b3669SPaul Moore buf_len - CIPSO_V4_HDR_LEN); 1840484b3669SPaul Moore break; 184115c45f7bSPaul Moore case CIPSO_V4_TAG_LOCAL: 184215c45f7bSPaul Moore ret_val = cipso_v4_gentag_loc(doi_def, 184315c45f7bSPaul Moore secattr, 184415c45f7bSPaul Moore &buf[CIPSO_V4_HDR_LEN], 184515c45f7bSPaul Moore buf_len - CIPSO_V4_HDR_LEN); 184615c45f7bSPaul Moore break; 1847446fda4fSPaul Moore default: 1848948bf85cSPaul Moore return -EPERM; 1849446fda4fSPaul Moore } 1850446fda4fSPaul Moore 1851446fda4fSPaul Moore iter++; 185291b1ed0aSPaul Moore } while (ret_val < 0 && 1853446fda4fSPaul Moore iter < CIPSO_V4_TAG_MAXCNT && 1854446fda4fSPaul Moore doi_def->tags[iter] != CIPSO_V4_TAG_INVALID); 185591b1ed0aSPaul Moore if (ret_val < 0) 1856948bf85cSPaul Moore return ret_val; 185791b1ed0aSPaul Moore cipso_v4_gentag_hdr(doi_def, buf, ret_val); 1858948bf85cSPaul Moore return CIPSO_V4_HDR_LEN + ret_val; 1859948bf85cSPaul Moore } 1860948bf85cSPaul Moore 1861948bf85cSPaul Moore /** 1862948bf85cSPaul Moore * cipso_v4_sock_setattr - Add a CIPSO option to a socket 1863948bf85cSPaul Moore * @sk: the socket 1864948bf85cSPaul Moore * @doi_def: the CIPSO DOI to use 1865948bf85cSPaul Moore * @secattr: the specific security attributes of the socket 1866948bf85cSPaul Moore * 1867948bf85cSPaul Moore * Description: 1868948bf85cSPaul Moore * Set the CIPSO option on the given socket using the DOI definition and 1869948bf85cSPaul Moore * security attributes passed to the function. This function requires 1870948bf85cSPaul Moore * exclusive access to @sk, which means it either needs to be in the 1871948bf85cSPaul Moore * process of being created or locked. Returns zero on success and negative 1872948bf85cSPaul Moore * values on failure. 1873948bf85cSPaul Moore * 1874948bf85cSPaul Moore */ 1875948bf85cSPaul Moore int cipso_v4_sock_setattr(struct sock *sk, 1876948bf85cSPaul Moore const struct cipso_v4_doi *doi_def, 1877948bf85cSPaul Moore const struct netlbl_lsm_secattr *secattr) 1878948bf85cSPaul Moore { 1879948bf85cSPaul Moore int ret_val = -EPERM; 1880948bf85cSPaul Moore unsigned char *buf = NULL; 1881948bf85cSPaul Moore u32 buf_len; 1882948bf85cSPaul Moore u32 opt_len; 1883f6d8bd05SEric Dumazet struct ip_options_rcu *old, *opt = NULL; 1884948bf85cSPaul Moore struct inet_sock *sk_inet; 1885948bf85cSPaul Moore struct inet_connection_sock *sk_conn; 1886948bf85cSPaul Moore 1887948bf85cSPaul Moore /* In the case of sock_create_lite(), the sock->sk field is not 1888948bf85cSPaul Moore * defined yet but it is not a problem as the only users of these 1889948bf85cSPaul Moore * "lite" PF_INET sockets are functions which do an accept() call 1890948bf85cSPaul Moore * afterwards so we will label the socket as part of the accept(). */ 1891948bf85cSPaul Moore if (sk == NULL) 1892948bf85cSPaul Moore return 0; 1893948bf85cSPaul Moore 1894948bf85cSPaul Moore /* We allocate the maximum CIPSO option size here so we are probably 1895948bf85cSPaul Moore * being a little wasteful, but it makes our life _much_ easier later 1896948bf85cSPaul Moore * on and after all we are only talking about 40 bytes. */ 1897948bf85cSPaul Moore buf_len = CIPSO_V4_OPT_LEN_MAX; 1898948bf85cSPaul Moore buf = kmalloc(buf_len, GFP_ATOMIC); 1899948bf85cSPaul Moore if (buf == NULL) { 1900948bf85cSPaul Moore ret_val = -ENOMEM; 1901948bf85cSPaul Moore goto socket_setattr_failure; 1902948bf85cSPaul Moore } 1903948bf85cSPaul Moore 1904948bf85cSPaul Moore ret_val = cipso_v4_genopt(buf, buf_len, doi_def, secattr); 1905948bf85cSPaul Moore if (ret_val < 0) 1906948bf85cSPaul Moore goto socket_setattr_failure; 1907948bf85cSPaul Moore buf_len = ret_val; 1908446fda4fSPaul Moore 1909446fda4fSPaul Moore /* We can't use ip_options_get() directly because it makes a call to 1910446fda4fSPaul Moore * ip_options_get_alloc() which allocates memory with GFP_KERNEL and 1911f8687afeSPaul Moore * we won't always have CAP_NET_RAW even though we _always_ want to 1912f8687afeSPaul Moore * set the IPOPT_CIPSO option. */ 1913446fda4fSPaul Moore opt_len = (buf_len + 3) & ~3; 1914446fda4fSPaul Moore opt = kzalloc(sizeof(*opt) + opt_len, GFP_ATOMIC); 1915446fda4fSPaul Moore if (opt == NULL) { 1916446fda4fSPaul Moore ret_val = -ENOMEM; 1917446fda4fSPaul Moore goto socket_setattr_failure; 1918446fda4fSPaul Moore } 1919f6d8bd05SEric Dumazet memcpy(opt->opt.__data, buf, buf_len); 1920f6d8bd05SEric Dumazet opt->opt.optlen = opt_len; 1921f6d8bd05SEric Dumazet opt->opt.cipso = sizeof(struct iphdr); 1922446fda4fSPaul Moore kfree(buf); 1923446fda4fSPaul Moore buf = NULL; 1924446fda4fSPaul Moore 1925446fda4fSPaul Moore sk_inet = inet_sk(sk); 1926f6d8bd05SEric Dumazet 1927f6d8bd05SEric Dumazet old = rcu_dereference_protected(sk_inet->inet_opt, sock_owned_by_user(sk)); 1928446fda4fSPaul Moore if (sk_inet->is_icsk) { 1929446fda4fSPaul Moore sk_conn = inet_csk(sk); 1930f6d8bd05SEric Dumazet if (old) 1931f6d8bd05SEric Dumazet sk_conn->icsk_ext_hdr_len -= old->opt.optlen; 1932f6d8bd05SEric Dumazet sk_conn->icsk_ext_hdr_len += opt->opt.optlen; 1933446fda4fSPaul Moore sk_conn->icsk_sync_mss(sk, sk_conn->icsk_pmtu_cookie); 1934446fda4fSPaul Moore } 1935f6d8bd05SEric Dumazet rcu_assign_pointer(sk_inet->inet_opt, opt); 1936f6d8bd05SEric Dumazet if (old) 19374f9c8c1bSPaul E. McKenney kfree_rcu(old, rcu); 1938446fda4fSPaul Moore 1939446fda4fSPaul Moore return 0; 1940446fda4fSPaul Moore 1941446fda4fSPaul Moore socket_setattr_failure: 1942446fda4fSPaul Moore kfree(buf); 1943446fda4fSPaul Moore kfree(opt); 1944446fda4fSPaul Moore return ret_val; 1945446fda4fSPaul Moore } 1946446fda4fSPaul Moore 1947446fda4fSPaul Moore /** 1948389fb800SPaul Moore * cipso_v4_req_setattr - Add a CIPSO option to a connection request socket 1949389fb800SPaul Moore * @req: the connection request socket 1950389fb800SPaul Moore * @doi_def: the CIPSO DOI to use 1951389fb800SPaul Moore * @secattr: the specific security attributes of the socket 1952014ab19aSPaul Moore * 1953014ab19aSPaul Moore * Description: 1954389fb800SPaul Moore * Set the CIPSO option on the given socket using the DOI definition and 1955389fb800SPaul Moore * security attributes passed to the function. Returns zero on success and 1956389fb800SPaul Moore * negative values on failure. 1957014ab19aSPaul Moore * 1958014ab19aSPaul Moore */ 1959389fb800SPaul Moore int cipso_v4_req_setattr(struct request_sock *req, 1960389fb800SPaul Moore const struct cipso_v4_doi *doi_def, 1961389fb800SPaul Moore const struct netlbl_lsm_secattr *secattr) 1962014ab19aSPaul Moore { 1963389fb800SPaul Moore int ret_val = -EPERM; 1964389fb800SPaul Moore unsigned char *buf = NULL; 1965389fb800SPaul Moore u32 buf_len; 1966389fb800SPaul Moore u32 opt_len; 1967f6d8bd05SEric Dumazet struct ip_options_rcu *opt = NULL; 1968389fb800SPaul Moore struct inet_request_sock *req_inet; 1969014ab19aSPaul Moore 1970389fb800SPaul Moore /* We allocate the maximum CIPSO option size here so we are probably 1971389fb800SPaul Moore * being a little wasteful, but it makes our life _much_ easier later 1972389fb800SPaul Moore * on and after all we are only talking about 40 bytes. */ 1973389fb800SPaul Moore buf_len = CIPSO_V4_OPT_LEN_MAX; 1974389fb800SPaul Moore buf = kmalloc(buf_len, GFP_ATOMIC); 1975389fb800SPaul Moore if (buf == NULL) { 1976389fb800SPaul Moore ret_val = -ENOMEM; 1977389fb800SPaul Moore goto req_setattr_failure; 1978389fb800SPaul Moore } 1979389fb800SPaul Moore 1980389fb800SPaul Moore ret_val = cipso_v4_genopt(buf, buf_len, doi_def, secattr); 1981389fb800SPaul Moore if (ret_val < 0) 1982389fb800SPaul Moore goto req_setattr_failure; 1983389fb800SPaul Moore buf_len = ret_val; 1984389fb800SPaul Moore 1985389fb800SPaul Moore /* We can't use ip_options_get() directly because it makes a call to 1986389fb800SPaul Moore * ip_options_get_alloc() which allocates memory with GFP_KERNEL and 1987389fb800SPaul Moore * we won't always have CAP_NET_RAW even though we _always_ want to 1988389fb800SPaul Moore * set the IPOPT_CIPSO option. */ 1989389fb800SPaul Moore opt_len = (buf_len + 3) & ~3; 1990389fb800SPaul Moore opt = kzalloc(sizeof(*opt) + opt_len, GFP_ATOMIC); 1991389fb800SPaul Moore if (opt == NULL) { 1992389fb800SPaul Moore ret_val = -ENOMEM; 1993389fb800SPaul Moore goto req_setattr_failure; 1994389fb800SPaul Moore } 1995f6d8bd05SEric Dumazet memcpy(opt->opt.__data, buf, buf_len); 1996f6d8bd05SEric Dumazet opt->opt.optlen = opt_len; 1997f6d8bd05SEric Dumazet opt->opt.cipso = sizeof(struct iphdr); 1998389fb800SPaul Moore kfree(buf); 1999389fb800SPaul Moore buf = NULL; 2000389fb800SPaul Moore 2001389fb800SPaul Moore req_inet = inet_rsk(req); 2002389fb800SPaul Moore opt = xchg(&req_inet->opt, opt); 2003f6d8bd05SEric Dumazet if (opt) 20044f9c8c1bSPaul E. McKenney kfree_rcu(opt, rcu); 2005389fb800SPaul Moore 2006389fb800SPaul Moore return 0; 2007389fb800SPaul Moore 2008389fb800SPaul Moore req_setattr_failure: 2009389fb800SPaul Moore kfree(buf); 2010389fb800SPaul Moore kfree(opt); 2011389fb800SPaul Moore return ret_val; 2012389fb800SPaul Moore } 2013389fb800SPaul Moore 2014389fb800SPaul Moore /** 2015389fb800SPaul Moore * cipso_v4_delopt - Delete the CIPSO option from a set of IP options 2016389fb800SPaul Moore * @opt_ptr: IP option pointer 2017389fb800SPaul Moore * 2018389fb800SPaul Moore * Description: 2019389fb800SPaul Moore * Deletes the CIPSO IP option from a set of IP options and makes the necessary 2020389fb800SPaul Moore * adjustments to the IP option structure. Returns zero on success, negative 2021389fb800SPaul Moore * values on failure. 2022389fb800SPaul Moore * 2023389fb800SPaul Moore */ 2024f6d8bd05SEric Dumazet static int cipso_v4_delopt(struct ip_options_rcu **opt_ptr) 2025389fb800SPaul Moore { 2026389fb800SPaul Moore int hdr_delta = 0; 2027f6d8bd05SEric Dumazet struct ip_options_rcu *opt = *opt_ptr; 2028014ab19aSPaul Moore 2029f6d8bd05SEric Dumazet if (opt->opt.srr || opt->opt.rr || opt->opt.ts || opt->opt.router_alert) { 2030014ab19aSPaul Moore u8 cipso_len; 2031014ab19aSPaul Moore u8 cipso_off; 2032014ab19aSPaul Moore unsigned char *cipso_ptr; 2033014ab19aSPaul Moore int iter; 2034014ab19aSPaul Moore int optlen_new; 2035014ab19aSPaul Moore 2036f6d8bd05SEric Dumazet cipso_off = opt->opt.cipso - sizeof(struct iphdr); 2037f6d8bd05SEric Dumazet cipso_ptr = &opt->opt.__data[cipso_off]; 2038014ab19aSPaul Moore cipso_len = cipso_ptr[1]; 2039014ab19aSPaul Moore 2040f6d8bd05SEric Dumazet if (opt->opt.srr > opt->opt.cipso) 2041f6d8bd05SEric Dumazet opt->opt.srr -= cipso_len; 2042f6d8bd05SEric Dumazet if (opt->opt.rr > opt->opt.cipso) 2043f6d8bd05SEric Dumazet opt->opt.rr -= cipso_len; 2044f6d8bd05SEric Dumazet if (opt->opt.ts > opt->opt.cipso) 2045f6d8bd05SEric Dumazet opt->opt.ts -= cipso_len; 2046f6d8bd05SEric Dumazet if (opt->opt.router_alert > opt->opt.cipso) 2047f6d8bd05SEric Dumazet opt->opt.router_alert -= cipso_len; 2048f6d8bd05SEric Dumazet opt->opt.cipso = 0; 2049014ab19aSPaul Moore 2050014ab19aSPaul Moore memmove(cipso_ptr, cipso_ptr + cipso_len, 2051f6d8bd05SEric Dumazet opt->opt.optlen - cipso_off - cipso_len); 2052014ab19aSPaul Moore 2053014ab19aSPaul Moore /* determining the new total option length is tricky because of 2054014ab19aSPaul Moore * the padding necessary, the only thing i can think to do at 2055014ab19aSPaul Moore * this point is walk the options one-by-one, skipping the 2056014ab19aSPaul Moore * padding at the end to determine the actual option size and 2057014ab19aSPaul Moore * from there we can determine the new total option length */ 2058014ab19aSPaul Moore iter = 0; 2059014ab19aSPaul Moore optlen_new = 0; 2060f6d8bd05SEric Dumazet while (iter < opt->opt.optlen) 2061f6d8bd05SEric Dumazet if (opt->opt.__data[iter] != IPOPT_NOP) { 2062f6d8bd05SEric Dumazet iter += opt->opt.__data[iter + 1]; 2063014ab19aSPaul Moore optlen_new = iter; 2064014ab19aSPaul Moore } else 2065014ab19aSPaul Moore iter++; 2066f6d8bd05SEric Dumazet hdr_delta = opt->opt.optlen; 2067f6d8bd05SEric Dumazet opt->opt.optlen = (optlen_new + 3) & ~3; 2068f6d8bd05SEric Dumazet hdr_delta -= opt->opt.optlen; 2069014ab19aSPaul Moore } else { 2070014ab19aSPaul Moore /* only the cipso option was present on the socket so we can 2071014ab19aSPaul Moore * remove the entire option struct */ 2072389fb800SPaul Moore *opt_ptr = NULL; 2073f6d8bd05SEric Dumazet hdr_delta = opt->opt.optlen; 20744f9c8c1bSPaul E. McKenney kfree_rcu(opt, rcu); 2075014ab19aSPaul Moore } 2076014ab19aSPaul Moore 2077389fb800SPaul Moore return hdr_delta; 2078389fb800SPaul Moore } 2079389fb800SPaul Moore 2080389fb800SPaul Moore /** 2081389fb800SPaul Moore * cipso_v4_sock_delattr - Delete the CIPSO option from a socket 2082389fb800SPaul Moore * @sk: the socket 2083389fb800SPaul Moore * 2084389fb800SPaul Moore * Description: 2085389fb800SPaul Moore * Removes the CIPSO option from a socket, if present. 2086389fb800SPaul Moore * 2087389fb800SPaul Moore */ 2088389fb800SPaul Moore void cipso_v4_sock_delattr(struct sock *sk) 2089389fb800SPaul Moore { 2090389fb800SPaul Moore int hdr_delta; 2091f6d8bd05SEric Dumazet struct ip_options_rcu *opt; 2092389fb800SPaul Moore struct inet_sock *sk_inet; 2093389fb800SPaul Moore 2094389fb800SPaul Moore sk_inet = inet_sk(sk); 2095f6d8bd05SEric Dumazet opt = rcu_dereference_protected(sk_inet->inet_opt, 1); 2096f6d8bd05SEric Dumazet if (opt == NULL || opt->opt.cipso == 0) 2097389fb800SPaul Moore return; 2098389fb800SPaul Moore 2099f6d8bd05SEric Dumazet hdr_delta = cipso_v4_delopt(&sk_inet->inet_opt); 2100014ab19aSPaul Moore if (sk_inet->is_icsk && hdr_delta > 0) { 2101014ab19aSPaul Moore struct inet_connection_sock *sk_conn = inet_csk(sk); 2102014ab19aSPaul Moore sk_conn->icsk_ext_hdr_len -= hdr_delta; 2103014ab19aSPaul Moore sk_conn->icsk_sync_mss(sk, sk_conn->icsk_pmtu_cookie); 2104014ab19aSPaul Moore } 2105014ab19aSPaul Moore } 2106014ab19aSPaul Moore 2107014ab19aSPaul Moore /** 2108389fb800SPaul Moore * cipso_v4_req_delattr - Delete the CIPSO option from a request socket 2109389fb800SPaul Moore * @reg: the request socket 2110389fb800SPaul Moore * 2111389fb800SPaul Moore * Description: 2112389fb800SPaul Moore * Removes the CIPSO option from a request socket, if present. 2113389fb800SPaul Moore * 2114389fb800SPaul Moore */ 2115389fb800SPaul Moore void cipso_v4_req_delattr(struct request_sock *req) 2116389fb800SPaul Moore { 2117f6d8bd05SEric Dumazet struct ip_options_rcu *opt; 2118389fb800SPaul Moore struct inet_request_sock *req_inet; 2119389fb800SPaul Moore 2120389fb800SPaul Moore req_inet = inet_rsk(req); 2121389fb800SPaul Moore opt = req_inet->opt; 2122f6d8bd05SEric Dumazet if (opt == NULL || opt->opt.cipso == 0) 2123389fb800SPaul Moore return; 2124389fb800SPaul Moore 2125389fb800SPaul Moore cipso_v4_delopt(&req_inet->opt); 2126389fb800SPaul Moore } 2127389fb800SPaul Moore 2128389fb800SPaul Moore /** 212963d804eaSPaul Moore * cipso_v4_getattr - Helper function for the cipso_v4_*_getattr functions 213063d804eaSPaul Moore * @cipso: the CIPSO v4 option 213163d804eaSPaul Moore * @secattr: the security attributes 213263d804eaSPaul Moore * 213363d804eaSPaul Moore * Description: 213463d804eaSPaul Moore * Inspect @cipso and return the security attributes in @secattr. Returns zero 213563d804eaSPaul Moore * on success and negative values on failure. 213663d804eaSPaul Moore * 213763d804eaSPaul Moore */ 213863d804eaSPaul Moore static int cipso_v4_getattr(const unsigned char *cipso, 213963d804eaSPaul Moore struct netlbl_lsm_secattr *secattr) 214063d804eaSPaul Moore { 214163d804eaSPaul Moore int ret_val = -ENOMSG; 214263d804eaSPaul Moore u32 doi; 214363d804eaSPaul Moore struct cipso_v4_doi *doi_def; 214463d804eaSPaul Moore 214563d804eaSPaul Moore if (cipso_v4_cache_check(cipso, cipso[1], secattr) == 0) 214663d804eaSPaul Moore return 0; 214763d804eaSPaul Moore 2148d3e2ce3bSHarvey Harrison doi = get_unaligned_be32(&cipso[2]); 214963d804eaSPaul Moore rcu_read_lock(); 215063d804eaSPaul Moore doi_def = cipso_v4_doi_search(doi); 215163d804eaSPaul Moore if (doi_def == NULL) 215263d804eaSPaul Moore goto getattr_return; 215363d804eaSPaul Moore /* XXX - This code assumes only one tag per CIPSO option which isn't 215463d804eaSPaul Moore * really a good assumption to make but since we only support the MAC 215563d804eaSPaul Moore * tags right now it is a safe assumption. */ 215663d804eaSPaul Moore switch (cipso[6]) { 215763d804eaSPaul Moore case CIPSO_V4_TAG_RBITMAP: 215863d804eaSPaul Moore ret_val = cipso_v4_parsetag_rbm(doi_def, &cipso[6], secattr); 215963d804eaSPaul Moore break; 216063d804eaSPaul Moore case CIPSO_V4_TAG_ENUM: 216163d804eaSPaul Moore ret_val = cipso_v4_parsetag_enum(doi_def, &cipso[6], secattr); 216263d804eaSPaul Moore break; 216363d804eaSPaul Moore case CIPSO_V4_TAG_RANGE: 216463d804eaSPaul Moore ret_val = cipso_v4_parsetag_rng(doi_def, &cipso[6], secattr); 216563d804eaSPaul Moore break; 216615c45f7bSPaul Moore case CIPSO_V4_TAG_LOCAL: 216715c45f7bSPaul Moore ret_val = cipso_v4_parsetag_loc(doi_def, &cipso[6], secattr); 216815c45f7bSPaul Moore break; 216963d804eaSPaul Moore } 217016efd454SPaul Moore if (ret_val == 0) 217116efd454SPaul Moore secattr->type = NETLBL_NLTYPE_CIPSOV4; 217263d804eaSPaul Moore 217363d804eaSPaul Moore getattr_return: 217463d804eaSPaul Moore rcu_read_unlock(); 217563d804eaSPaul Moore return ret_val; 217663d804eaSPaul Moore } 217763d804eaSPaul Moore 217863d804eaSPaul Moore /** 217914a72f53SPaul Moore * cipso_v4_sock_getattr - Get the security attributes from a sock 218014a72f53SPaul Moore * @sk: the sock 218114a72f53SPaul Moore * @secattr: the security attributes 218214a72f53SPaul Moore * 218314a72f53SPaul Moore * Description: 218414a72f53SPaul Moore * Query @sk to see if there is a CIPSO option attached to the sock and if 218514a72f53SPaul Moore * there is return the CIPSO security attributes in @secattr. This function 218614a72f53SPaul Moore * requires that @sk be locked, or privately held, but it does not do any 218714a72f53SPaul Moore * locking itself. Returns zero on success and negative values on failure. 218814a72f53SPaul Moore * 218914a72f53SPaul Moore */ 219014a72f53SPaul Moore int cipso_v4_sock_getattr(struct sock *sk, struct netlbl_lsm_secattr *secattr) 219114a72f53SPaul Moore { 2192f6d8bd05SEric Dumazet struct ip_options_rcu *opt; 2193f6d8bd05SEric Dumazet int res = -ENOMSG; 219414a72f53SPaul Moore 2195f6d8bd05SEric Dumazet rcu_read_lock(); 2196f6d8bd05SEric Dumazet opt = rcu_dereference(inet_sk(sk)->inet_opt); 2197f6d8bd05SEric Dumazet if (opt && opt->opt.cipso) 2198f6d8bd05SEric Dumazet res = cipso_v4_getattr(opt->opt.__data + 2199f6d8bd05SEric Dumazet opt->opt.cipso - 2200f6d8bd05SEric Dumazet sizeof(struct iphdr), 220114a72f53SPaul Moore secattr); 2202f6d8bd05SEric Dumazet rcu_read_unlock(); 2203f6d8bd05SEric Dumazet return res; 220414a72f53SPaul Moore } 220514a72f53SPaul Moore 220614a72f53SPaul Moore /** 2207948bf85cSPaul Moore * cipso_v4_skbuff_setattr - Set the CIPSO option on a packet 2208948bf85cSPaul Moore * @skb: the packet 2209948bf85cSPaul Moore * @secattr: the security attributes 2210948bf85cSPaul Moore * 2211948bf85cSPaul Moore * Description: 2212948bf85cSPaul Moore * Set the CIPSO option on the given packet based on the security attributes. 2213948bf85cSPaul Moore * Returns a pointer to the IP header on success and NULL on failure. 2214948bf85cSPaul Moore * 2215948bf85cSPaul Moore */ 2216948bf85cSPaul Moore int cipso_v4_skbuff_setattr(struct sk_buff *skb, 2217948bf85cSPaul Moore const struct cipso_v4_doi *doi_def, 2218948bf85cSPaul Moore const struct netlbl_lsm_secattr *secattr) 2219948bf85cSPaul Moore { 2220948bf85cSPaul Moore int ret_val; 2221948bf85cSPaul Moore struct iphdr *iph; 2222948bf85cSPaul Moore struct ip_options *opt = &IPCB(skb)->opt; 2223948bf85cSPaul Moore unsigned char buf[CIPSO_V4_OPT_LEN_MAX]; 2224948bf85cSPaul Moore u32 buf_len = CIPSO_V4_OPT_LEN_MAX; 2225948bf85cSPaul Moore u32 opt_len; 2226948bf85cSPaul Moore int len_delta; 2227948bf85cSPaul Moore 222800af5c69Sroel kluin ret_val = cipso_v4_genopt(buf, buf_len, doi_def, secattr); 222900af5c69Sroel kluin if (ret_val < 0) 223000af5c69Sroel kluin return ret_val; 223100af5c69Sroel kluin buf_len = ret_val; 2232948bf85cSPaul Moore opt_len = (buf_len + 3) & ~3; 2233948bf85cSPaul Moore 2234948bf85cSPaul Moore /* we overwrite any existing options to ensure that we have enough 2235948bf85cSPaul Moore * room for the CIPSO option, the reason is that we _need_ to guarantee 2236948bf85cSPaul Moore * that the security label is applied to the packet - we do the same 2237948bf85cSPaul Moore * thing when using the socket options and it hasn't caused a problem, 2238948bf85cSPaul Moore * if we need to we can always revisit this choice later */ 2239948bf85cSPaul Moore 2240948bf85cSPaul Moore len_delta = opt_len - opt->optlen; 2241948bf85cSPaul Moore /* if we don't ensure enough headroom we could panic on the skb_push() 2242948bf85cSPaul Moore * call below so make sure we have enough, we are also "mangling" the 2243948bf85cSPaul Moore * packet so we should probably do a copy-on-write call anyway */ 2244948bf85cSPaul Moore ret_val = skb_cow(skb, skb_headroom(skb) + len_delta); 2245948bf85cSPaul Moore if (ret_val < 0) 2246948bf85cSPaul Moore return ret_val; 2247948bf85cSPaul Moore 2248948bf85cSPaul Moore if (len_delta > 0) { 2249948bf85cSPaul Moore /* we assume that the header + opt->optlen have already been 2250948bf85cSPaul Moore * "pushed" in ip_options_build() or similar */ 2251948bf85cSPaul Moore iph = ip_hdr(skb); 2252948bf85cSPaul Moore skb_push(skb, len_delta); 2253948bf85cSPaul Moore memmove((char *)iph - len_delta, iph, iph->ihl << 2); 2254948bf85cSPaul Moore skb_reset_network_header(skb); 2255948bf85cSPaul Moore iph = ip_hdr(skb); 2256948bf85cSPaul Moore } else if (len_delta < 0) { 2257948bf85cSPaul Moore iph = ip_hdr(skb); 2258948bf85cSPaul Moore memset(iph + 1, IPOPT_NOP, opt->optlen); 2259948bf85cSPaul Moore } else 2260948bf85cSPaul Moore iph = ip_hdr(skb); 2261948bf85cSPaul Moore 2262948bf85cSPaul Moore if (opt->optlen > 0) 2263948bf85cSPaul Moore memset(opt, 0, sizeof(*opt)); 2264948bf85cSPaul Moore opt->optlen = opt_len; 2265948bf85cSPaul Moore opt->cipso = sizeof(struct iphdr); 2266948bf85cSPaul Moore opt->is_changed = 1; 2267948bf85cSPaul Moore 2268948bf85cSPaul Moore /* we have to do the following because we are being called from a 2269948bf85cSPaul Moore * netfilter hook which means the packet already has had the header 2270948bf85cSPaul Moore * fields populated and the checksum calculated - yes this means we 2271948bf85cSPaul Moore * are doing more work than needed but we do it to keep the core 2272948bf85cSPaul Moore * stack clean and tidy */ 2273948bf85cSPaul Moore memcpy(iph + 1, buf, buf_len); 2274948bf85cSPaul Moore if (opt_len > buf_len) 2275948bf85cSPaul Moore memset((char *)(iph + 1) + buf_len, 0, opt_len - buf_len); 2276948bf85cSPaul Moore if (len_delta != 0) { 2277948bf85cSPaul Moore iph->ihl = 5 + (opt_len >> 2); 2278948bf85cSPaul Moore iph->tot_len = htons(skb->len); 2279948bf85cSPaul Moore } 2280948bf85cSPaul Moore ip_send_check(iph); 2281948bf85cSPaul Moore 2282948bf85cSPaul Moore return 0; 2283948bf85cSPaul Moore } 2284948bf85cSPaul Moore 2285948bf85cSPaul Moore /** 2286948bf85cSPaul Moore * cipso_v4_skbuff_delattr - Delete any CIPSO options from a packet 2287948bf85cSPaul Moore * @skb: the packet 2288948bf85cSPaul Moore * 2289948bf85cSPaul Moore * Description: 2290948bf85cSPaul Moore * Removes any and all CIPSO options from the given packet. Returns zero on 2291948bf85cSPaul Moore * success, negative values on failure. 2292948bf85cSPaul Moore * 2293948bf85cSPaul Moore */ 2294948bf85cSPaul Moore int cipso_v4_skbuff_delattr(struct sk_buff *skb) 2295948bf85cSPaul Moore { 2296948bf85cSPaul Moore int ret_val; 2297948bf85cSPaul Moore struct iphdr *iph; 2298948bf85cSPaul Moore struct ip_options *opt = &IPCB(skb)->opt; 2299948bf85cSPaul Moore unsigned char *cipso_ptr; 2300948bf85cSPaul Moore 2301948bf85cSPaul Moore if (opt->cipso == 0) 2302948bf85cSPaul Moore return 0; 2303948bf85cSPaul Moore 2304948bf85cSPaul Moore /* since we are changing the packet we should make a copy */ 2305948bf85cSPaul Moore ret_val = skb_cow(skb, skb_headroom(skb)); 2306948bf85cSPaul Moore if (ret_val < 0) 2307948bf85cSPaul Moore return ret_val; 2308948bf85cSPaul Moore 2309948bf85cSPaul Moore /* the easiest thing to do is just replace the cipso option with noop 2310948bf85cSPaul Moore * options since we don't change the size of the packet, although we 2311948bf85cSPaul Moore * still need to recalculate the checksum */ 2312948bf85cSPaul Moore 2313948bf85cSPaul Moore iph = ip_hdr(skb); 2314948bf85cSPaul Moore cipso_ptr = (unsigned char *)iph + opt->cipso; 2315948bf85cSPaul Moore memset(cipso_ptr, IPOPT_NOOP, cipso_ptr[1]); 2316948bf85cSPaul Moore opt->cipso = 0; 2317948bf85cSPaul Moore opt->is_changed = 1; 2318948bf85cSPaul Moore 2319948bf85cSPaul Moore ip_send_check(iph); 2320948bf85cSPaul Moore 2321948bf85cSPaul Moore return 0; 2322948bf85cSPaul Moore } 2323948bf85cSPaul Moore 2324948bf85cSPaul Moore /** 2325446fda4fSPaul Moore * cipso_v4_skbuff_getattr - Get the security attributes from the CIPSO option 2326446fda4fSPaul Moore * @skb: the packet 2327446fda4fSPaul Moore * @secattr: the security attributes 2328446fda4fSPaul Moore * 2329446fda4fSPaul Moore * Description: 2330446fda4fSPaul Moore * Parse the given packet's CIPSO option and return the security attributes. 2331446fda4fSPaul Moore * Returns zero on success and negative values on failure. 2332446fda4fSPaul Moore * 2333446fda4fSPaul Moore */ 2334446fda4fSPaul Moore int cipso_v4_skbuff_getattr(const struct sk_buff *skb, 2335446fda4fSPaul Moore struct netlbl_lsm_secattr *secattr) 2336446fda4fSPaul Moore { 233763d804eaSPaul Moore return cipso_v4_getattr(CIPSO_V4_OPTPTR(skb), secattr); 2338446fda4fSPaul Moore } 2339446fda4fSPaul Moore 2340446fda4fSPaul Moore /* 2341446fda4fSPaul Moore * Setup Functions 2342446fda4fSPaul Moore */ 2343446fda4fSPaul Moore 2344446fda4fSPaul Moore /** 2345446fda4fSPaul Moore * cipso_v4_init - Initialize the CIPSO module 2346446fda4fSPaul Moore * 2347446fda4fSPaul Moore * Description: 2348446fda4fSPaul Moore * Initialize the CIPSO module and prepare it for use. Returns zero on success 2349446fda4fSPaul Moore * and negative values on failure. 2350446fda4fSPaul Moore * 2351446fda4fSPaul Moore */ 2352446fda4fSPaul Moore static int __init cipso_v4_init(void) 2353446fda4fSPaul Moore { 2354446fda4fSPaul Moore int ret_val; 2355446fda4fSPaul Moore 2356446fda4fSPaul Moore ret_val = cipso_v4_cache_init(); 2357446fda4fSPaul Moore if (ret_val != 0) 2358446fda4fSPaul Moore panic("Failed to initialize the CIPSO/IPv4 cache (%d)\n", 2359446fda4fSPaul Moore ret_val); 2360446fda4fSPaul Moore 2361446fda4fSPaul Moore return 0; 2362446fda4fSPaul Moore } 2363446fda4fSPaul Moore 2364446fda4fSPaul Moore subsys_initcall(cipso_v4_init); 2365