11ccea77eSThomas Gleixner // SPDX-License-Identifier: GPL-2.0-or-later 2446fda4fSPaul Moore /* 3446fda4fSPaul Moore * CIPSO - Commercial IP Security Option 4446fda4fSPaul Moore * 5446fda4fSPaul Moore * This is an implementation of the CIPSO 2.2 protocol as specified in 6446fda4fSPaul Moore * draft-ietf-cipso-ipsecurity-01.txt with additional tag types as found in 7586c2500SPaul Moore * FIPS-188. While CIPSO never became a full IETF RFC standard many vendors 8446fda4fSPaul Moore * have chosen to adopt the protocol and over the years it has become a 9446fda4fSPaul Moore * de-facto standard for labeled networking. 10446fda4fSPaul Moore * 11586c2500SPaul Moore * The CIPSO draft specification can be found in the kernel's Documentation 12586c2500SPaul Moore * directory as well as the following URL: 137a6498ebSAlexander A. Klimov * https://tools.ietf.org/id/draft-ietf-cipso-ipsecurity-01.txt 14586c2500SPaul Moore * The FIPS-188 specification can be found at the following URL: 157a6498ebSAlexander A. Klimov * https://www.itl.nist.gov/fipspubs/fip188.htm 16586c2500SPaul Moore * 17446fda4fSPaul Moore * Author: Paul Moore <paul.moore@hp.com> 18446fda4fSPaul Moore */ 19446fda4fSPaul Moore 20446fda4fSPaul Moore /* 21948bf85cSPaul Moore * (c) Copyright Hewlett-Packard Development Company, L.P., 2006, 2008 22446fda4fSPaul Moore */ 23446fda4fSPaul Moore 24446fda4fSPaul Moore #include <linux/init.h> 25446fda4fSPaul Moore #include <linux/types.h> 26446fda4fSPaul Moore #include <linux/rcupdate.h> 27446fda4fSPaul Moore #include <linux/list.h> 28446fda4fSPaul Moore #include <linux/spinlock.h> 29446fda4fSPaul Moore #include <linux/string.h> 30446fda4fSPaul Moore #include <linux/jhash.h> 316c2e8ac0SPaul Moore #include <linux/audit.h> 325a0e3ad6STejun Heo #include <linux/slab.h> 33446fda4fSPaul Moore #include <net/ip.h> 34446fda4fSPaul Moore #include <net/icmp.h> 35446fda4fSPaul Moore #include <net/tcp.h> 36446fda4fSPaul Moore #include <net/netlabel.h> 37446fda4fSPaul Moore #include <net/cipso_ipv4.h> 3860063497SArun Sharma #include <linux/atomic.h> 394c787b16SFabian Frederick #include <linux/bug.h> 4050e5d35cSPaul Moore #include <asm/unaligned.h> 41446fda4fSPaul Moore 42446fda4fSPaul Moore /* List of available DOI definitions */ 43446fda4fSPaul Moore /* XXX - This currently assumes a minimal number of different DOIs in use, 44446fda4fSPaul Moore * if in practice there are a lot of different DOIs this list should 45446fda4fSPaul Moore * probably be turned into a hash table or something similar so we 46446fda4fSPaul Moore * can do quick lookups. */ 478ce11e6aSAdrian Bunk static DEFINE_SPINLOCK(cipso_v4_doi_list_lock); 481596c97aSDenis Cheng static LIST_HEAD(cipso_v4_doi_list); 49446fda4fSPaul Moore 50446fda4fSPaul Moore /* Label mapping cache */ 51446fda4fSPaul Moore int cipso_v4_cache_enabled = 1; 52446fda4fSPaul Moore int cipso_v4_cache_bucketsize = 10; 53446fda4fSPaul Moore #define CIPSO_V4_CACHE_BUCKETBITS 7 54446fda4fSPaul Moore #define CIPSO_V4_CACHE_BUCKETS (1 << CIPSO_V4_CACHE_BUCKETBITS) 55446fda4fSPaul Moore #define CIPSO_V4_CACHE_REORDERLIMIT 10 56446fda4fSPaul Moore struct cipso_v4_map_cache_bkt { 57446fda4fSPaul Moore spinlock_t lock; 58446fda4fSPaul Moore u32 size; 59446fda4fSPaul Moore struct list_head list; 60446fda4fSPaul Moore }; 61988b1343SFabian Frederick 62446fda4fSPaul Moore struct cipso_v4_map_cache_entry { 63446fda4fSPaul Moore u32 hash; 64446fda4fSPaul Moore unsigned char *key; 65446fda4fSPaul Moore size_t key_len; 66446fda4fSPaul Moore 67ffb733c6Spaul.moore@hp.com struct netlbl_lsm_cache *lsm_data; 68446fda4fSPaul Moore 69446fda4fSPaul Moore u32 activity; 70446fda4fSPaul Moore struct list_head list; 71446fda4fSPaul Moore }; 72988b1343SFabian Frederick 73988b1343SFabian Frederick static struct cipso_v4_map_cache_bkt *cipso_v4_cache; 74446fda4fSPaul Moore 75446fda4fSPaul Moore /* Restricted bitmap (tag #1) flags */ 76db9c8e2bSwangzhitong int cipso_v4_rbm_optfmt; 77446fda4fSPaul Moore int cipso_v4_rbm_strictvalid = 1; 78446fda4fSPaul Moore 79446fda4fSPaul Moore /* 80f998e8cbSPaul Moore * Protocol Constants 81f998e8cbSPaul Moore */ 82f998e8cbSPaul Moore 83f998e8cbSPaul Moore /* Maximum size of the CIPSO IP option, derived from the fact that the maximum 84f998e8cbSPaul Moore * IPv4 header size is 60 bytes and the base IPv4 header is 20 bytes long. */ 85f998e8cbSPaul Moore #define CIPSO_V4_OPT_LEN_MAX 40 86f998e8cbSPaul Moore 87f998e8cbSPaul Moore /* Length of the base CIPSO option, this includes the option type (1 byte), the 88f998e8cbSPaul Moore * option length (1 byte), and the DOI (4 bytes). */ 89f998e8cbSPaul Moore #define CIPSO_V4_HDR_LEN 6 90f998e8cbSPaul Moore 91f998e8cbSPaul Moore /* Base length of the restrictive category bitmap tag (tag #1). */ 92f998e8cbSPaul Moore #define CIPSO_V4_TAG_RBM_BLEN 4 93f998e8cbSPaul Moore 94f998e8cbSPaul Moore /* Base length of the enumerated category tag (tag #2). */ 95f998e8cbSPaul Moore #define CIPSO_V4_TAG_ENUM_BLEN 4 96f998e8cbSPaul Moore 97f998e8cbSPaul Moore /* Base length of the ranged categories bitmap tag (tag #5). */ 98f998e8cbSPaul Moore #define CIPSO_V4_TAG_RNG_BLEN 4 99f998e8cbSPaul Moore /* The maximum number of category ranges permitted in the ranged category tag 100f998e8cbSPaul Moore * (tag #5). You may note that the IETF draft states that the maximum number 101f998e8cbSPaul Moore * of category ranges is 7, but if the low end of the last category range is 10225985edcSLucas De Marchi * zero then it is possible to fit 8 category ranges because the zero should 103f998e8cbSPaul Moore * be omitted. */ 104f998e8cbSPaul Moore #define CIPSO_V4_TAG_RNG_CAT_MAX 8 105f998e8cbSPaul Moore 10615c45f7bSPaul Moore /* Base length of the local tag (non-standard tag). 10715c45f7bSPaul Moore * Tag definition (may change between kernel versions) 10815c45f7bSPaul Moore * 10915c45f7bSPaul Moore * 0 8 16 24 32 11015c45f7bSPaul Moore * +----------+----------+----------+----------+ 11115c45f7bSPaul Moore * | 10000000 | 00000110 | 32-bit secid value | 11215c45f7bSPaul Moore * +----------+----------+----------+----------+ 11315c45f7bSPaul Moore * | in (host byte order)| 11415c45f7bSPaul Moore * +----------+----------+ 11515c45f7bSPaul Moore * 11615c45f7bSPaul Moore */ 11715c45f7bSPaul Moore #define CIPSO_V4_TAG_LOC_BLEN 6 11815c45f7bSPaul Moore 119f998e8cbSPaul Moore /* 120446fda4fSPaul Moore * Helper Functions 121446fda4fSPaul Moore */ 122446fda4fSPaul Moore 123446fda4fSPaul Moore /** 124446fda4fSPaul Moore * cipso_v4_cache_entry_free - Frees a cache entry 125446fda4fSPaul Moore * @entry: the entry to free 126446fda4fSPaul Moore * 127446fda4fSPaul Moore * Description: 128ffb733c6Spaul.moore@hp.com * This function frees the memory associated with a cache entry including the 129ffb733c6Spaul.moore@hp.com * LSM cache data if there are no longer any users, i.e. reference count == 0. 130446fda4fSPaul Moore * 131446fda4fSPaul Moore */ 132446fda4fSPaul Moore static void cipso_v4_cache_entry_free(struct cipso_v4_map_cache_entry *entry) 133446fda4fSPaul Moore { 134ffb733c6Spaul.moore@hp.com if (entry->lsm_data) 135ffb733c6Spaul.moore@hp.com netlbl_secattr_cache_free(entry->lsm_data); 136446fda4fSPaul Moore kfree(entry->key); 137446fda4fSPaul Moore kfree(entry); 138446fda4fSPaul Moore } 139446fda4fSPaul Moore 140446fda4fSPaul Moore /** 141446fda4fSPaul Moore * cipso_v4_map_cache_hash - Hashing function for the CIPSO cache 142446fda4fSPaul Moore * @key: the hash key 143446fda4fSPaul Moore * @key_len: the length of the key in bytes 144446fda4fSPaul Moore * 145446fda4fSPaul Moore * Description: 146446fda4fSPaul Moore * The CIPSO tag hashing function. Returns a 32-bit hash value. 147446fda4fSPaul Moore * 148446fda4fSPaul Moore */ 149446fda4fSPaul Moore static u32 cipso_v4_map_cache_hash(const unsigned char *key, u32 key_len) 150446fda4fSPaul Moore { 151446fda4fSPaul Moore return jhash(key, key_len, 0); 152446fda4fSPaul Moore } 153446fda4fSPaul Moore 154446fda4fSPaul Moore /* 155446fda4fSPaul Moore * Label Mapping Cache Functions 156446fda4fSPaul Moore */ 157446fda4fSPaul Moore 158446fda4fSPaul Moore /** 159446fda4fSPaul Moore * cipso_v4_cache_init - Initialize the CIPSO cache 160446fda4fSPaul Moore * 161446fda4fSPaul Moore * Description: 162446fda4fSPaul Moore * Initializes the CIPSO label mapping cache, this function should be called 163446fda4fSPaul Moore * before any of the other functions defined in this file. Returns zero on 164446fda4fSPaul Moore * success, negative values on error. 165446fda4fSPaul Moore * 166446fda4fSPaul Moore */ 167cb57659aSFabian Frederick static int __init cipso_v4_cache_init(void) 168446fda4fSPaul Moore { 169446fda4fSPaul Moore u32 iter; 170446fda4fSPaul Moore 171446fda4fSPaul Moore cipso_v4_cache = kcalloc(CIPSO_V4_CACHE_BUCKETS, 172446fda4fSPaul Moore sizeof(struct cipso_v4_map_cache_bkt), 173446fda4fSPaul Moore GFP_KERNEL); 17451456b29SIan Morris if (!cipso_v4_cache) 175446fda4fSPaul Moore return -ENOMEM; 176446fda4fSPaul Moore 177446fda4fSPaul Moore for (iter = 0; iter < CIPSO_V4_CACHE_BUCKETS; iter++) { 178446fda4fSPaul Moore spin_lock_init(&cipso_v4_cache[iter].lock); 179446fda4fSPaul Moore cipso_v4_cache[iter].size = 0; 180446fda4fSPaul Moore INIT_LIST_HEAD(&cipso_v4_cache[iter].list); 181446fda4fSPaul Moore } 182446fda4fSPaul Moore 183446fda4fSPaul Moore return 0; 184446fda4fSPaul Moore } 185446fda4fSPaul Moore 186446fda4fSPaul Moore /** 187446fda4fSPaul Moore * cipso_v4_cache_invalidate - Invalidates the current CIPSO cache 188446fda4fSPaul Moore * 189446fda4fSPaul Moore * Description: 1904ac9e23cSZheng Yejian * Invalidates and frees any entries in the CIPSO cache. 191446fda4fSPaul Moore * 192446fda4fSPaul Moore */ 193446fda4fSPaul Moore void cipso_v4_cache_invalidate(void) 194446fda4fSPaul Moore { 195446fda4fSPaul Moore struct cipso_v4_map_cache_entry *entry, *tmp_entry; 196446fda4fSPaul Moore u32 iter; 197446fda4fSPaul Moore 198446fda4fSPaul Moore for (iter = 0; iter < CIPSO_V4_CACHE_BUCKETS; iter++) { 199609c92feSPaul Moore spin_lock_bh(&cipso_v4_cache[iter].lock); 200446fda4fSPaul Moore list_for_each_entry_safe(entry, 201446fda4fSPaul Moore tmp_entry, 202446fda4fSPaul Moore &cipso_v4_cache[iter].list, list) { 203446fda4fSPaul Moore list_del(&entry->list); 204446fda4fSPaul Moore cipso_v4_cache_entry_free(entry); 205446fda4fSPaul Moore } 206446fda4fSPaul Moore cipso_v4_cache[iter].size = 0; 207609c92feSPaul Moore spin_unlock_bh(&cipso_v4_cache[iter].lock); 208446fda4fSPaul Moore } 209446fda4fSPaul Moore } 210446fda4fSPaul Moore 211446fda4fSPaul Moore /** 212446fda4fSPaul Moore * cipso_v4_cache_check - Check the CIPSO cache for a label mapping 213446fda4fSPaul Moore * @key: the buffer to check 214446fda4fSPaul Moore * @key_len: buffer length in bytes 215446fda4fSPaul Moore * @secattr: the security attribute struct to use 216446fda4fSPaul Moore * 217446fda4fSPaul Moore * Description: 218446fda4fSPaul Moore * This function checks the cache to see if a label mapping already exists for 219446fda4fSPaul Moore * the given key. If there is a match then the cache is adjusted and the 220446fda4fSPaul Moore * @secattr struct is populated with the correct LSM security attributes. The 221446fda4fSPaul Moore * cache is adjusted in the following manner if the entry is not already the 222446fda4fSPaul Moore * first in the cache bucket: 223446fda4fSPaul Moore * 224446fda4fSPaul Moore * 1. The cache entry's activity counter is incremented 225446fda4fSPaul Moore * 2. The previous (higher ranking) entry's activity counter is decremented 226446fda4fSPaul Moore * 3. If the difference between the two activity counters is geater than 227446fda4fSPaul Moore * CIPSO_V4_CACHE_REORDERLIMIT the two entries are swapped 228446fda4fSPaul Moore * 229446fda4fSPaul Moore * Returns zero on success, -ENOENT for a cache miss, and other negative values 230446fda4fSPaul Moore * on error. 231446fda4fSPaul Moore * 232446fda4fSPaul Moore */ 233446fda4fSPaul Moore static int cipso_v4_cache_check(const unsigned char *key, 234446fda4fSPaul Moore u32 key_len, 235446fda4fSPaul Moore struct netlbl_lsm_secattr *secattr) 236446fda4fSPaul Moore { 237446fda4fSPaul Moore u32 bkt; 238446fda4fSPaul Moore struct cipso_v4_map_cache_entry *entry; 239446fda4fSPaul Moore struct cipso_v4_map_cache_entry *prev_entry = NULL; 240446fda4fSPaul Moore u32 hash; 241446fda4fSPaul Moore 242dd44f04bSKuniyuki Iwashima if (!READ_ONCE(cipso_v4_cache_enabled)) 243446fda4fSPaul Moore return -ENOENT; 244446fda4fSPaul Moore 245446fda4fSPaul Moore hash = cipso_v4_map_cache_hash(key, key_len); 2465e0f8923SPavel Emelyanov bkt = hash & (CIPSO_V4_CACHE_BUCKETS - 1); 247609c92feSPaul Moore spin_lock_bh(&cipso_v4_cache[bkt].lock); 248446fda4fSPaul Moore list_for_each_entry(entry, &cipso_v4_cache[bkt].list, list) { 249446fda4fSPaul Moore if (entry->hash == hash && 250446fda4fSPaul Moore entry->key_len == key_len && 251446fda4fSPaul Moore memcmp(entry->key, key, key_len) == 0) { 252446fda4fSPaul Moore entry->activity += 1; 253b4217b82SReshetova, Elena refcount_inc(&entry->lsm_data->refcount); 254ffb733c6Spaul.moore@hp.com secattr->cache = entry->lsm_data; 255701a90baSPaul Moore secattr->flags |= NETLBL_SECATTR_CACHE; 25616efd454SPaul Moore secattr->type = NETLBL_NLTYPE_CIPSOV4; 25751456b29SIan Morris if (!prev_entry) { 258609c92feSPaul Moore spin_unlock_bh(&cipso_v4_cache[bkt].lock); 259446fda4fSPaul Moore return 0; 260446fda4fSPaul Moore } 261446fda4fSPaul Moore 262446fda4fSPaul Moore if (prev_entry->activity > 0) 263446fda4fSPaul Moore prev_entry->activity -= 1; 264446fda4fSPaul Moore if (entry->activity > prev_entry->activity && 265446fda4fSPaul Moore entry->activity - prev_entry->activity > 266446fda4fSPaul Moore CIPSO_V4_CACHE_REORDERLIMIT) { 267446fda4fSPaul Moore __list_del(entry->list.prev, entry->list.next); 268446fda4fSPaul Moore __list_add(&entry->list, 269446fda4fSPaul Moore prev_entry->list.prev, 270446fda4fSPaul Moore &prev_entry->list); 271446fda4fSPaul Moore } 272446fda4fSPaul Moore 273609c92feSPaul Moore spin_unlock_bh(&cipso_v4_cache[bkt].lock); 274446fda4fSPaul Moore return 0; 275446fda4fSPaul Moore } 276446fda4fSPaul Moore prev_entry = entry; 277446fda4fSPaul Moore } 278609c92feSPaul Moore spin_unlock_bh(&cipso_v4_cache[bkt].lock); 279446fda4fSPaul Moore 280446fda4fSPaul Moore return -ENOENT; 281446fda4fSPaul Moore } 282446fda4fSPaul Moore 283446fda4fSPaul Moore /** 284446fda4fSPaul Moore * cipso_v4_cache_add - Add an entry to the CIPSO cache 2853628e3cbSAndrew Lunn * @cipso_ptr: pointer to CIPSO IP option 286446fda4fSPaul Moore * @secattr: the packet's security attributes 287446fda4fSPaul Moore * 288446fda4fSPaul Moore * Description: 289446fda4fSPaul Moore * Add a new entry into the CIPSO label mapping cache. Add the new entry to 290446fda4fSPaul Moore * head of the cache bucket's list, if the cache bucket is out of room remove 291446fda4fSPaul Moore * the last entry in the list first. It is important to note that there is 292446fda4fSPaul Moore * currently no checking for duplicate keys. Returns zero on success, 293446fda4fSPaul Moore * negative values on failure. 294446fda4fSPaul Moore * 295446fda4fSPaul Moore */ 29604f81f01SPaul Moore int cipso_v4_cache_add(const unsigned char *cipso_ptr, 297446fda4fSPaul Moore const struct netlbl_lsm_secattr *secattr) 298446fda4fSPaul Moore { 299dd44f04bSKuniyuki Iwashima int bkt_size = READ_ONCE(cipso_v4_cache_bucketsize); 300446fda4fSPaul Moore int ret_val = -EPERM; 301446fda4fSPaul Moore u32 bkt; 302446fda4fSPaul Moore struct cipso_v4_map_cache_entry *entry = NULL; 303446fda4fSPaul Moore struct cipso_v4_map_cache_entry *old_entry = NULL; 304446fda4fSPaul Moore u32 cipso_ptr_len; 305446fda4fSPaul Moore 306dd44f04bSKuniyuki Iwashima if (!READ_ONCE(cipso_v4_cache_enabled) || bkt_size <= 0) 307446fda4fSPaul Moore return 0; 308446fda4fSPaul Moore 309446fda4fSPaul Moore cipso_ptr_len = cipso_ptr[1]; 310446fda4fSPaul Moore 311446fda4fSPaul Moore entry = kzalloc(sizeof(*entry), GFP_ATOMIC); 31251456b29SIan Morris if (!entry) 313446fda4fSPaul Moore return -ENOMEM; 314fac5d731SArnaldo Carvalho de Melo entry->key = kmemdup(cipso_ptr, cipso_ptr_len, GFP_ATOMIC); 31551456b29SIan Morris if (!entry->key) { 316446fda4fSPaul Moore ret_val = -ENOMEM; 317446fda4fSPaul Moore goto cache_add_failure; 318446fda4fSPaul Moore } 319446fda4fSPaul Moore entry->key_len = cipso_ptr_len; 320446fda4fSPaul Moore entry->hash = cipso_v4_map_cache_hash(cipso_ptr, cipso_ptr_len); 321b4217b82SReshetova, Elena refcount_inc(&secattr->cache->refcount); 322ffb733c6Spaul.moore@hp.com entry->lsm_data = secattr->cache; 323446fda4fSPaul Moore 3245e0f8923SPavel Emelyanov bkt = entry->hash & (CIPSO_V4_CACHE_BUCKETS - 1); 325609c92feSPaul Moore spin_lock_bh(&cipso_v4_cache[bkt].lock); 326dd44f04bSKuniyuki Iwashima if (cipso_v4_cache[bkt].size < bkt_size) { 327446fda4fSPaul Moore list_add(&entry->list, &cipso_v4_cache[bkt].list); 328446fda4fSPaul Moore cipso_v4_cache[bkt].size += 1; 329446fda4fSPaul Moore } else { 330446fda4fSPaul Moore old_entry = list_entry(cipso_v4_cache[bkt].list.prev, 331446fda4fSPaul Moore struct cipso_v4_map_cache_entry, list); 332446fda4fSPaul Moore list_del(&old_entry->list); 333446fda4fSPaul Moore list_add(&entry->list, &cipso_v4_cache[bkt].list); 334446fda4fSPaul Moore cipso_v4_cache_entry_free(old_entry); 335446fda4fSPaul Moore } 336609c92feSPaul Moore spin_unlock_bh(&cipso_v4_cache[bkt].lock); 337446fda4fSPaul Moore 338446fda4fSPaul Moore return 0; 339446fda4fSPaul Moore 340446fda4fSPaul Moore cache_add_failure: 341446fda4fSPaul Moore if (entry) 342446fda4fSPaul Moore cipso_v4_cache_entry_free(entry); 343446fda4fSPaul Moore return ret_val; 344446fda4fSPaul Moore } 345446fda4fSPaul Moore 346446fda4fSPaul Moore /* 347446fda4fSPaul Moore * DOI List Functions 348446fda4fSPaul Moore */ 349446fda4fSPaul Moore 350446fda4fSPaul Moore /** 351446fda4fSPaul Moore * cipso_v4_doi_search - Searches for a DOI definition 352446fda4fSPaul Moore * @doi: the DOI to search for 353446fda4fSPaul Moore * 354446fda4fSPaul Moore * Description: 355446fda4fSPaul Moore * Search the DOI definition list for a DOI definition with a DOI value that 35625985edcSLucas De Marchi * matches @doi. The caller is responsible for calling rcu_read_[un]lock(). 357446fda4fSPaul Moore * Returns a pointer to the DOI definition on success and NULL on failure. 358446fda4fSPaul Moore */ 359446fda4fSPaul Moore static struct cipso_v4_doi *cipso_v4_doi_search(u32 doi) 360446fda4fSPaul Moore { 361446fda4fSPaul Moore struct cipso_v4_doi *iter; 362446fda4fSPaul Moore 363446fda4fSPaul Moore list_for_each_entry_rcu(iter, &cipso_v4_doi_list, list) 364f6a6fedeSReshetova, Elena if (iter->doi == doi && refcount_read(&iter->refcount)) 365446fda4fSPaul Moore return iter; 366446fda4fSPaul Moore return NULL; 367446fda4fSPaul Moore } 368446fda4fSPaul Moore 369446fda4fSPaul Moore /** 370446fda4fSPaul Moore * cipso_v4_doi_add - Add a new DOI to the CIPSO protocol engine 371446fda4fSPaul Moore * @doi_def: the DOI structure 3726c2e8ac0SPaul Moore * @audit_info: NetLabel audit information 373446fda4fSPaul Moore * 374446fda4fSPaul Moore * Description: 375446fda4fSPaul Moore * The caller defines a new DOI for use by the CIPSO engine and calls this 376446fda4fSPaul Moore * function to add it to the list of acceptable domains. The caller must 377446fda4fSPaul Moore * ensure that the mapping table specified in @doi_def->map meets all of the 378446fda4fSPaul Moore * requirements of the mapping type (see cipso_ipv4.h for details). Returns 379446fda4fSPaul Moore * zero on success and non-zero on failure. 380446fda4fSPaul Moore * 381446fda4fSPaul Moore */ 3826c2e8ac0SPaul Moore int cipso_v4_doi_add(struct cipso_v4_doi *doi_def, 3836c2e8ac0SPaul Moore struct netlbl_audit *audit_info) 384446fda4fSPaul Moore { 3856c2e8ac0SPaul Moore int ret_val = -EINVAL; 3866ce61a7cSPaul Moore u32 iter; 3876c2e8ac0SPaul Moore u32 doi; 3886c2e8ac0SPaul Moore u32 doi_type; 3896c2e8ac0SPaul Moore struct audit_buffer *audit_buf; 3906c2e8ac0SPaul Moore 3916c2e8ac0SPaul Moore doi = doi_def->doi; 3926c2e8ac0SPaul Moore doi_type = doi_def->type; 3936ce61a7cSPaul Moore 39456755924SDan Carpenter if (doi_def->doi == CIPSO_V4_DOI_UNKNOWN) 3956c2e8ac0SPaul Moore goto doi_add_return; 3966ce61a7cSPaul Moore for (iter = 0; iter < CIPSO_V4_TAG_MAXCNT; iter++) { 3976ce61a7cSPaul Moore switch (doi_def->tags[iter]) { 3986ce61a7cSPaul Moore case CIPSO_V4_TAG_RBITMAP: 3996ce61a7cSPaul Moore break; 400484b3669SPaul Moore case CIPSO_V4_TAG_RANGE: 401654bbc2aSPaul Moore case CIPSO_V4_TAG_ENUM: 402654bbc2aSPaul Moore if (doi_def->type != CIPSO_V4_MAP_PASS) 4036c2e8ac0SPaul Moore goto doi_add_return; 404654bbc2aSPaul Moore break; 40515c45f7bSPaul Moore case CIPSO_V4_TAG_LOCAL: 40615c45f7bSPaul Moore if (doi_def->type != CIPSO_V4_MAP_LOCAL) 4076c2e8ac0SPaul Moore goto doi_add_return; 4086c2e8ac0SPaul Moore break; 4096c2e8ac0SPaul Moore case CIPSO_V4_TAG_INVALID: 4106c2e8ac0SPaul Moore if (iter == 0) 4116c2e8ac0SPaul Moore goto doi_add_return; 41215c45f7bSPaul Moore break; 4136ce61a7cSPaul Moore default: 4146c2e8ac0SPaul Moore goto doi_add_return; 4156ce61a7cSPaul Moore } 4166ce61a7cSPaul Moore } 417446fda4fSPaul Moore 418f6a6fedeSReshetova, Elena refcount_set(&doi_def->refcount, 1); 419446fda4fSPaul Moore 420446fda4fSPaul Moore spin_lock(&cipso_v4_doi_list_lock); 42100db4124SIan Morris if (cipso_v4_doi_search(doi_def->doi)) { 4226c2e8ac0SPaul Moore spin_unlock(&cipso_v4_doi_list_lock); 4236c2e8ac0SPaul Moore ret_val = -EEXIST; 4246c2e8ac0SPaul Moore goto doi_add_return; 4256c2e8ac0SPaul Moore } 426446fda4fSPaul Moore list_add_tail_rcu(&doi_def->list, &cipso_v4_doi_list); 427446fda4fSPaul Moore spin_unlock(&cipso_v4_doi_list_lock); 4286c2e8ac0SPaul Moore ret_val = 0; 429446fda4fSPaul Moore 4306c2e8ac0SPaul Moore doi_add_return: 4316c2e8ac0SPaul Moore audit_buf = netlbl_audit_start(AUDIT_MAC_CIPSOV4_ADD, audit_info); 43200db4124SIan Morris if (audit_buf) { 4336c2e8ac0SPaul Moore const char *type_str; 4346c2e8ac0SPaul Moore switch (doi_type) { 4356c2e8ac0SPaul Moore case CIPSO_V4_MAP_TRANS: 4366c2e8ac0SPaul Moore type_str = "trans"; 4376c2e8ac0SPaul Moore break; 4386c2e8ac0SPaul Moore case CIPSO_V4_MAP_PASS: 4396c2e8ac0SPaul Moore type_str = "pass"; 4406c2e8ac0SPaul Moore break; 4416c2e8ac0SPaul Moore case CIPSO_V4_MAP_LOCAL: 4426c2e8ac0SPaul Moore type_str = "local"; 4436c2e8ac0SPaul Moore break; 4446c2e8ac0SPaul Moore default: 4456c2e8ac0SPaul Moore type_str = "(unknown)"; 4466c2e8ac0SPaul Moore } 4476c2e8ac0SPaul Moore audit_log_format(audit_buf, 4486c2e8ac0SPaul Moore " cipso_doi=%u cipso_type=%s res=%u", 4496c2e8ac0SPaul Moore doi, type_str, ret_val == 0 ? 1 : 0); 4506c2e8ac0SPaul Moore audit_log_end(audit_buf); 4516c2e8ac0SPaul Moore } 452446fda4fSPaul Moore 4536c2e8ac0SPaul Moore return ret_val; 454446fda4fSPaul Moore } 455446fda4fSPaul Moore 456446fda4fSPaul Moore /** 457b1edeb10SPaul Moore * cipso_v4_doi_free - Frees a DOI definition 4584973404fSFabian Frederick * @doi_def: the DOI definition 459446fda4fSPaul Moore * 460446fda4fSPaul Moore * Description: 461b1edeb10SPaul Moore * This function frees all of the memory associated with a DOI definition. 462446fda4fSPaul Moore * 463446fda4fSPaul Moore */ 464b1edeb10SPaul Moore void cipso_v4_doi_free(struct cipso_v4_doi *doi_def) 465446fda4fSPaul Moore { 46651456b29SIan Morris if (!doi_def) 467b1edeb10SPaul Moore return; 468446fda4fSPaul Moore 469b1edeb10SPaul Moore switch (doi_def->type) { 47015c45f7bSPaul Moore case CIPSO_V4_MAP_TRANS: 471b1edeb10SPaul Moore kfree(doi_def->map.std->lvl.cipso); 472b1edeb10SPaul Moore kfree(doi_def->map.std->lvl.local); 473b1edeb10SPaul Moore kfree(doi_def->map.std->cat.cipso); 474b1edeb10SPaul Moore kfree(doi_def->map.std->cat.local); 475d612c3f3SNanyong Sun kfree(doi_def->map.std); 476b1edeb10SPaul Moore break; 477446fda4fSPaul Moore } 478b1edeb10SPaul Moore kfree(doi_def); 479446fda4fSPaul Moore } 480446fda4fSPaul Moore 481446fda4fSPaul Moore /** 482b1edeb10SPaul Moore * cipso_v4_doi_free_rcu - Frees a DOI definition via the RCU pointer 483b1edeb10SPaul Moore * @entry: the entry's RCU field 484b1edeb10SPaul Moore * 485b1edeb10SPaul Moore * Description: 486b1edeb10SPaul Moore * This function is designed to be used as a callback to the call_rcu() 487b1edeb10SPaul Moore * function so that the memory allocated to the DOI definition can be released 488b1edeb10SPaul Moore * safely. 489b1edeb10SPaul Moore * 490b1edeb10SPaul Moore */ 491b1edeb10SPaul Moore static void cipso_v4_doi_free_rcu(struct rcu_head *entry) 492b1edeb10SPaul Moore { 493b1edeb10SPaul Moore struct cipso_v4_doi *doi_def; 494b1edeb10SPaul Moore 495b1edeb10SPaul Moore doi_def = container_of(entry, struct cipso_v4_doi, rcu); 496b1edeb10SPaul Moore cipso_v4_doi_free(doi_def); 497b1edeb10SPaul Moore } 498b1edeb10SPaul Moore 499b1edeb10SPaul Moore /** 500b1edeb10SPaul Moore * cipso_v4_doi_remove - Remove an existing DOI from the CIPSO protocol engine 501b1edeb10SPaul Moore * @doi: the DOI value 5027edce636SWang Hai * @audit_info: NetLabel audit information 503b1edeb10SPaul Moore * 504b1edeb10SPaul Moore * Description: 505b1edeb10SPaul Moore * Removes a DOI definition from the CIPSO engine. The NetLabel routines will 506b1edeb10SPaul Moore * be called to release their own LSM domain mappings as well as our own 507b1edeb10SPaul Moore * domain list. Returns zero on success and negative values on failure. 508b1edeb10SPaul Moore * 509b1edeb10SPaul Moore */ 510b1edeb10SPaul Moore int cipso_v4_doi_remove(u32 doi, struct netlbl_audit *audit_info) 511b1edeb10SPaul Moore { 5126c2e8ac0SPaul Moore int ret_val; 513b1edeb10SPaul Moore struct cipso_v4_doi *doi_def; 5146c2e8ac0SPaul Moore struct audit_buffer *audit_buf; 515b1edeb10SPaul Moore 516b1edeb10SPaul Moore spin_lock(&cipso_v4_doi_list_lock); 517b1edeb10SPaul Moore doi_def = cipso_v4_doi_search(doi); 51851456b29SIan Morris if (!doi_def) { 519b1edeb10SPaul Moore spin_unlock(&cipso_v4_doi_list_lock); 5206c2e8ac0SPaul Moore ret_val = -ENOENT; 5216c2e8ac0SPaul Moore goto doi_remove_return; 522b1edeb10SPaul Moore } 523b1edeb10SPaul Moore list_del_rcu(&doi_def->list); 524b1edeb10SPaul Moore spin_unlock(&cipso_v4_doi_list_lock); 525b1edeb10SPaul Moore 526ad5d07f4SPaul Moore cipso_v4_doi_putdef(doi_def); 5276c2e8ac0SPaul Moore ret_val = 0; 528b1edeb10SPaul Moore 5296c2e8ac0SPaul Moore doi_remove_return: 5306c2e8ac0SPaul Moore audit_buf = netlbl_audit_start(AUDIT_MAC_CIPSOV4_DEL, audit_info); 53100db4124SIan Morris if (audit_buf) { 5326c2e8ac0SPaul Moore audit_log_format(audit_buf, 5336c2e8ac0SPaul Moore " cipso_doi=%u res=%u", 5346c2e8ac0SPaul Moore doi, ret_val == 0 ? 1 : 0); 5356c2e8ac0SPaul Moore audit_log_end(audit_buf); 5366c2e8ac0SPaul Moore } 5376c2e8ac0SPaul Moore 5386c2e8ac0SPaul Moore return ret_val; 539b1edeb10SPaul Moore } 540b1edeb10SPaul Moore 541b1edeb10SPaul Moore /** 542b1edeb10SPaul Moore * cipso_v4_doi_getdef - Returns a reference to a valid DOI definition 543446fda4fSPaul Moore * @doi: the DOI value 544446fda4fSPaul Moore * 545446fda4fSPaul Moore * Description: 546446fda4fSPaul Moore * Searches for a valid DOI definition and if one is found it is returned to 547446fda4fSPaul Moore * the caller. Otherwise NULL is returned. The caller must ensure that 548b1edeb10SPaul Moore * rcu_read_lock() is held while accessing the returned definition and the DOI 549b1edeb10SPaul Moore * definition reference count is decremented when the caller is done. 550446fda4fSPaul Moore * 551446fda4fSPaul Moore */ 552446fda4fSPaul Moore struct cipso_v4_doi *cipso_v4_doi_getdef(u32 doi) 553446fda4fSPaul Moore { 554b1edeb10SPaul Moore struct cipso_v4_doi *doi_def; 555b1edeb10SPaul Moore 556b1edeb10SPaul Moore rcu_read_lock(); 557b1edeb10SPaul Moore doi_def = cipso_v4_doi_search(doi); 55851456b29SIan Morris if (!doi_def) 559b1edeb10SPaul Moore goto doi_getdef_return; 560f6a6fedeSReshetova, Elena if (!refcount_inc_not_zero(&doi_def->refcount)) 561b1edeb10SPaul Moore doi_def = NULL; 562b1edeb10SPaul Moore 563b1edeb10SPaul Moore doi_getdef_return: 564b1edeb10SPaul Moore rcu_read_unlock(); 565b1edeb10SPaul Moore return doi_def; 566b1edeb10SPaul Moore } 567b1edeb10SPaul Moore 568b1edeb10SPaul Moore /** 569b1edeb10SPaul Moore * cipso_v4_doi_putdef - Releases a reference for the given DOI definition 570b1edeb10SPaul Moore * @doi_def: the DOI definition 571b1edeb10SPaul Moore * 572b1edeb10SPaul Moore * Description: 573b1edeb10SPaul Moore * Releases a DOI definition reference obtained from cipso_v4_doi_getdef(). 574b1edeb10SPaul Moore * 575b1edeb10SPaul Moore */ 576b1edeb10SPaul Moore void cipso_v4_doi_putdef(struct cipso_v4_doi *doi_def) 577b1edeb10SPaul Moore { 57851456b29SIan Morris if (!doi_def) 579b1edeb10SPaul Moore return; 580b1edeb10SPaul Moore 581f6a6fedeSReshetova, Elena if (!refcount_dec_and_test(&doi_def->refcount)) 582b1edeb10SPaul Moore return; 583b1edeb10SPaul Moore 584b1edeb10SPaul Moore cipso_v4_cache_invalidate(); 585b1edeb10SPaul Moore call_rcu(&doi_def->rcu, cipso_v4_doi_free_rcu); 586446fda4fSPaul Moore } 587446fda4fSPaul Moore 588446fda4fSPaul Moore /** 589fcd48280SPaul Moore * cipso_v4_doi_walk - Iterate through the DOI definitions 590fcd48280SPaul Moore * @skip_cnt: skip past this number of DOI definitions, updated 591fcd48280SPaul Moore * @callback: callback for each DOI definition 592fcd48280SPaul Moore * @cb_arg: argument for the callback function 593446fda4fSPaul Moore * 594446fda4fSPaul Moore * Description: 595fcd48280SPaul Moore * Iterate over the DOI definition list, skipping the first @skip_cnt entries. 596fcd48280SPaul Moore * For each entry call @callback, if @callback returns a negative value stop 597fcd48280SPaul Moore * 'walking' through the list and return. Updates the value in @skip_cnt upon 598fcd48280SPaul Moore * return. Returns zero on success, negative values on failure. 599446fda4fSPaul Moore * 600446fda4fSPaul Moore */ 601fcd48280SPaul Moore int cipso_v4_doi_walk(u32 *skip_cnt, 602fcd48280SPaul Moore int (*callback) (struct cipso_v4_doi *doi_def, void *arg), 603fcd48280SPaul Moore void *cb_arg) 604446fda4fSPaul Moore { 605fcd48280SPaul Moore int ret_val = -ENOENT; 606446fda4fSPaul Moore u32 doi_cnt = 0; 607fcd48280SPaul Moore struct cipso_v4_doi *iter_doi; 608446fda4fSPaul Moore 609446fda4fSPaul Moore rcu_read_lock(); 610fcd48280SPaul Moore list_for_each_entry_rcu(iter_doi, &cipso_v4_doi_list, list) 611f6a6fedeSReshetova, Elena if (refcount_read(&iter_doi->refcount) > 0) { 612fcd48280SPaul Moore if (doi_cnt++ < *skip_cnt) 613fcd48280SPaul Moore continue; 614fcd48280SPaul Moore ret_val = callback(iter_doi, cb_arg); 615fcd48280SPaul Moore if (ret_val < 0) { 616fcd48280SPaul Moore doi_cnt--; 617fcd48280SPaul Moore goto doi_walk_return; 618446fda4fSPaul Moore } 619446fda4fSPaul Moore } 620446fda4fSPaul Moore 621fcd48280SPaul Moore doi_walk_return: 622446fda4fSPaul Moore rcu_read_unlock(); 623fcd48280SPaul Moore *skip_cnt = doi_cnt; 624fcd48280SPaul Moore return ret_val; 625446fda4fSPaul Moore } 626446fda4fSPaul Moore 627446fda4fSPaul Moore /* 628446fda4fSPaul Moore * Label Mapping Functions 629446fda4fSPaul Moore */ 630446fda4fSPaul Moore 631446fda4fSPaul Moore /** 632446fda4fSPaul Moore * cipso_v4_map_lvl_valid - Checks to see if the given level is understood 633446fda4fSPaul Moore * @doi_def: the DOI definition 634446fda4fSPaul Moore * @level: the level to check 635446fda4fSPaul Moore * 636446fda4fSPaul Moore * Description: 637446fda4fSPaul Moore * Checks the given level against the given DOI definition and returns a 638446fda4fSPaul Moore * negative value if the level does not have a valid mapping and a zero value 639446fda4fSPaul Moore * if the level is defined by the DOI. 640446fda4fSPaul Moore * 641446fda4fSPaul Moore */ 642446fda4fSPaul Moore static int cipso_v4_map_lvl_valid(const struct cipso_v4_doi *doi_def, u8 level) 643446fda4fSPaul Moore { 644446fda4fSPaul Moore switch (doi_def->type) { 645446fda4fSPaul Moore case CIPSO_V4_MAP_PASS: 646446fda4fSPaul Moore return 0; 64715c45f7bSPaul Moore case CIPSO_V4_MAP_TRANS: 6485578de48SPaul Moore if ((level < doi_def->map.std->lvl.cipso_size) && 6495578de48SPaul Moore (doi_def->map.std->lvl.cipso[level] < CIPSO_V4_INV_LVL)) 650446fda4fSPaul Moore return 0; 651446fda4fSPaul Moore break; 652446fda4fSPaul Moore } 653446fda4fSPaul Moore 654446fda4fSPaul Moore return -EFAULT; 655446fda4fSPaul Moore } 656446fda4fSPaul Moore 657446fda4fSPaul Moore /** 658446fda4fSPaul Moore * cipso_v4_map_lvl_hton - Perform a level mapping from the host to the network 659446fda4fSPaul Moore * @doi_def: the DOI definition 660446fda4fSPaul Moore * @host_lvl: the host MLS level 661446fda4fSPaul Moore * @net_lvl: the network/CIPSO MLS level 662446fda4fSPaul Moore * 663446fda4fSPaul Moore * Description: 664446fda4fSPaul Moore * Perform a label mapping to translate a local MLS level to the correct 665446fda4fSPaul Moore * CIPSO level using the given DOI definition. Returns zero on success, 666446fda4fSPaul Moore * negative values otherwise. 667446fda4fSPaul Moore * 668446fda4fSPaul Moore */ 669446fda4fSPaul Moore static int cipso_v4_map_lvl_hton(const struct cipso_v4_doi *doi_def, 670446fda4fSPaul Moore u32 host_lvl, 671446fda4fSPaul Moore u32 *net_lvl) 672446fda4fSPaul Moore { 673446fda4fSPaul Moore switch (doi_def->type) { 674446fda4fSPaul Moore case CIPSO_V4_MAP_PASS: 675446fda4fSPaul Moore *net_lvl = host_lvl; 676446fda4fSPaul Moore return 0; 67715c45f7bSPaul Moore case CIPSO_V4_MAP_TRANS: 678c6387a86SPaul Moore if (host_lvl < doi_def->map.std->lvl.local_size && 679c6387a86SPaul Moore doi_def->map.std->lvl.local[host_lvl] < CIPSO_V4_INV_LVL) { 680446fda4fSPaul Moore *net_lvl = doi_def->map.std->lvl.local[host_lvl]; 681446fda4fSPaul Moore return 0; 682446fda4fSPaul Moore } 683c6387a86SPaul Moore return -EPERM; 684446fda4fSPaul Moore } 685446fda4fSPaul Moore 686446fda4fSPaul Moore return -EINVAL; 687446fda4fSPaul Moore } 688446fda4fSPaul Moore 689446fda4fSPaul Moore /** 690446fda4fSPaul Moore * cipso_v4_map_lvl_ntoh - Perform a level mapping from the network to the host 691446fda4fSPaul Moore * @doi_def: the DOI definition 692446fda4fSPaul Moore * @net_lvl: the network/CIPSO MLS level 693446fda4fSPaul Moore * @host_lvl: the host MLS level 694446fda4fSPaul Moore * 695446fda4fSPaul Moore * Description: 696446fda4fSPaul Moore * Perform a label mapping to translate a CIPSO level to the correct local MLS 697446fda4fSPaul Moore * level using the given DOI definition. Returns zero on success, negative 698446fda4fSPaul Moore * values otherwise. 699446fda4fSPaul Moore * 700446fda4fSPaul Moore */ 701446fda4fSPaul Moore static int cipso_v4_map_lvl_ntoh(const struct cipso_v4_doi *doi_def, 702446fda4fSPaul Moore u32 net_lvl, 703446fda4fSPaul Moore u32 *host_lvl) 704446fda4fSPaul Moore { 705446fda4fSPaul Moore struct cipso_v4_std_map_tbl *map_tbl; 706446fda4fSPaul Moore 707446fda4fSPaul Moore switch (doi_def->type) { 708446fda4fSPaul Moore case CIPSO_V4_MAP_PASS: 709446fda4fSPaul Moore *host_lvl = net_lvl; 710446fda4fSPaul Moore return 0; 71115c45f7bSPaul Moore case CIPSO_V4_MAP_TRANS: 712446fda4fSPaul Moore map_tbl = doi_def->map.std; 713446fda4fSPaul Moore if (net_lvl < map_tbl->lvl.cipso_size && 714446fda4fSPaul Moore map_tbl->lvl.cipso[net_lvl] < CIPSO_V4_INV_LVL) { 715446fda4fSPaul Moore *host_lvl = doi_def->map.std->lvl.cipso[net_lvl]; 716446fda4fSPaul Moore return 0; 717446fda4fSPaul Moore } 718c6387a86SPaul Moore return -EPERM; 719446fda4fSPaul Moore } 720446fda4fSPaul Moore 721446fda4fSPaul Moore return -EINVAL; 722446fda4fSPaul Moore } 723446fda4fSPaul Moore 724446fda4fSPaul Moore /** 725446fda4fSPaul Moore * cipso_v4_map_cat_rbm_valid - Checks to see if the category bitmap is valid 726446fda4fSPaul Moore * @doi_def: the DOI definition 727446fda4fSPaul Moore * @bitmap: category bitmap 728446fda4fSPaul Moore * @bitmap_len: bitmap length in bytes 729446fda4fSPaul Moore * 730446fda4fSPaul Moore * Description: 731446fda4fSPaul Moore * Checks the given category bitmap against the given DOI definition and 732446fda4fSPaul Moore * returns a negative value if any of the categories in the bitmap do not have 733446fda4fSPaul Moore * a valid mapping and a zero value if all of the categories are valid. 734446fda4fSPaul Moore * 735446fda4fSPaul Moore */ 736446fda4fSPaul Moore static int cipso_v4_map_cat_rbm_valid(const struct cipso_v4_doi *doi_def, 737446fda4fSPaul Moore const unsigned char *bitmap, 738446fda4fSPaul Moore u32 bitmap_len) 739446fda4fSPaul Moore { 740446fda4fSPaul Moore int cat = -1; 741446fda4fSPaul Moore u32 bitmap_len_bits = bitmap_len * 8; 742044a68edSPaul Moore u32 cipso_cat_size; 743044a68edSPaul Moore u32 *cipso_array; 744446fda4fSPaul Moore 745446fda4fSPaul Moore switch (doi_def->type) { 746446fda4fSPaul Moore case CIPSO_V4_MAP_PASS: 747446fda4fSPaul Moore return 0; 74815c45f7bSPaul Moore case CIPSO_V4_MAP_TRANS: 749044a68edSPaul Moore cipso_cat_size = doi_def->map.std->cat.cipso_size; 750044a68edSPaul Moore cipso_array = doi_def->map.std->cat.cipso; 751446fda4fSPaul Moore for (;;) { 7523faa8f98SHuw Davies cat = netlbl_bitmap_walk(bitmap, 753446fda4fSPaul Moore bitmap_len_bits, 754446fda4fSPaul Moore cat + 1, 755446fda4fSPaul Moore 1); 756446fda4fSPaul Moore if (cat < 0) 757446fda4fSPaul Moore break; 758446fda4fSPaul Moore if (cat >= cipso_cat_size || 759446fda4fSPaul Moore cipso_array[cat] >= CIPSO_V4_INV_CAT) 760446fda4fSPaul Moore return -EFAULT; 761446fda4fSPaul Moore } 762446fda4fSPaul Moore 763446fda4fSPaul Moore if (cat == -1) 764446fda4fSPaul Moore return 0; 765446fda4fSPaul Moore break; 766446fda4fSPaul Moore } 767446fda4fSPaul Moore 768446fda4fSPaul Moore return -EFAULT; 769446fda4fSPaul Moore } 770446fda4fSPaul Moore 771446fda4fSPaul Moore /** 772446fda4fSPaul Moore * cipso_v4_map_cat_rbm_hton - Perform a category mapping from host to network 773446fda4fSPaul Moore * @doi_def: the DOI definition 77402752760SPaul Moore * @secattr: the security attributes 775446fda4fSPaul Moore * @net_cat: the zero'd out category bitmap in network/CIPSO format 776446fda4fSPaul Moore * @net_cat_len: the length of the CIPSO bitmap in bytes 777446fda4fSPaul Moore * 778446fda4fSPaul Moore * Description: 779446fda4fSPaul Moore * Perform a label mapping to translate a local MLS category bitmap to the 780446fda4fSPaul Moore * correct CIPSO bitmap using the given DOI definition. Returns the minimum 781446fda4fSPaul Moore * size in bytes of the network bitmap on success, negative values otherwise. 782446fda4fSPaul Moore * 783446fda4fSPaul Moore */ 784446fda4fSPaul Moore static int cipso_v4_map_cat_rbm_hton(const struct cipso_v4_doi *doi_def, 78502752760SPaul Moore const struct netlbl_lsm_secattr *secattr, 786446fda4fSPaul Moore unsigned char *net_cat, 787446fda4fSPaul Moore u32 net_cat_len) 788446fda4fSPaul Moore { 789446fda4fSPaul Moore int host_spot = -1; 79002752760SPaul Moore u32 net_spot = CIPSO_V4_INV_CAT; 791446fda4fSPaul Moore u32 net_spot_max = 0; 792446fda4fSPaul Moore u32 net_clen_bits = net_cat_len * 8; 79302752760SPaul Moore u32 host_cat_size = 0; 79402752760SPaul Moore u32 *host_cat_array = NULL; 79502752760SPaul Moore 79615c45f7bSPaul Moore if (doi_def->type == CIPSO_V4_MAP_TRANS) { 79702752760SPaul Moore host_cat_size = doi_def->map.std->cat.local_size; 79802752760SPaul Moore host_cat_array = doi_def->map.std->cat.local; 79902752760SPaul Moore } 80002752760SPaul Moore 80102752760SPaul Moore for (;;) { 8024fbe63d1SPaul Moore host_spot = netlbl_catmap_walk(secattr->attr.mls.cat, 80302752760SPaul Moore host_spot + 1); 80402752760SPaul Moore if (host_spot < 0) 80502752760SPaul Moore break; 806446fda4fSPaul Moore 807446fda4fSPaul Moore switch (doi_def->type) { 808446fda4fSPaul Moore case CIPSO_V4_MAP_PASS: 80902752760SPaul Moore net_spot = host_spot; 810446fda4fSPaul Moore break; 81115c45f7bSPaul Moore case CIPSO_V4_MAP_TRANS: 812446fda4fSPaul Moore if (host_spot >= host_cat_size) 813446fda4fSPaul Moore return -EPERM; 814446fda4fSPaul Moore net_spot = host_cat_array[host_spot]; 8159fade4bfSPaul Moore if (net_spot >= CIPSO_V4_INV_CAT) 8169fade4bfSPaul Moore return -EPERM; 81702752760SPaul Moore break; 81802752760SPaul Moore } 819446fda4fSPaul Moore if (net_spot >= net_clen_bits) 820446fda4fSPaul Moore return -ENOSPC; 8213faa8f98SHuw Davies netlbl_bitmap_setbit(net_cat, net_spot, 1); 822446fda4fSPaul Moore 823446fda4fSPaul Moore if (net_spot > net_spot_max) 824446fda4fSPaul Moore net_spot_max = net_spot; 825446fda4fSPaul Moore } 826446fda4fSPaul Moore 827446fda4fSPaul Moore if (++net_spot_max % 8) 828446fda4fSPaul Moore return net_spot_max / 8 + 1; 829446fda4fSPaul Moore return net_spot_max / 8; 830446fda4fSPaul Moore } 831446fda4fSPaul Moore 832446fda4fSPaul Moore /** 833446fda4fSPaul Moore * cipso_v4_map_cat_rbm_ntoh - Perform a category mapping from network to host 834446fda4fSPaul Moore * @doi_def: the DOI definition 835446fda4fSPaul Moore * @net_cat: the category bitmap in network/CIPSO format 836446fda4fSPaul Moore * @net_cat_len: the length of the CIPSO bitmap in bytes 83702752760SPaul Moore * @secattr: the security attributes 838446fda4fSPaul Moore * 839446fda4fSPaul Moore * Description: 840446fda4fSPaul Moore * Perform a label mapping to translate a CIPSO bitmap to the correct local 84102752760SPaul Moore * MLS category bitmap using the given DOI definition. Returns zero on 84202752760SPaul Moore * success, negative values on failure. 843446fda4fSPaul Moore * 844446fda4fSPaul Moore */ 845446fda4fSPaul Moore static int cipso_v4_map_cat_rbm_ntoh(const struct cipso_v4_doi *doi_def, 846446fda4fSPaul Moore const unsigned char *net_cat, 847446fda4fSPaul Moore u32 net_cat_len, 84802752760SPaul Moore struct netlbl_lsm_secattr *secattr) 849446fda4fSPaul Moore { 85002752760SPaul Moore int ret_val; 851446fda4fSPaul Moore int net_spot = -1; 85202752760SPaul Moore u32 host_spot = CIPSO_V4_INV_CAT; 853446fda4fSPaul Moore u32 net_clen_bits = net_cat_len * 8; 85402752760SPaul Moore u32 net_cat_size = 0; 85502752760SPaul Moore u32 *net_cat_array = NULL; 856446fda4fSPaul Moore 85715c45f7bSPaul Moore if (doi_def->type == CIPSO_V4_MAP_TRANS) { 858044a68edSPaul Moore net_cat_size = doi_def->map.std->cat.cipso_size; 859044a68edSPaul Moore net_cat_array = doi_def->map.std->cat.cipso; 86002752760SPaul Moore } 86102752760SPaul Moore 862446fda4fSPaul Moore for (;;) { 8633faa8f98SHuw Davies net_spot = netlbl_bitmap_walk(net_cat, 864446fda4fSPaul Moore net_clen_bits, 865446fda4fSPaul Moore net_spot + 1, 866446fda4fSPaul Moore 1); 867*9ff74d77SZhengchao Shao if (net_spot < 0) 86802752760SPaul Moore return 0; 869446fda4fSPaul Moore 87002752760SPaul Moore switch (doi_def->type) { 87102752760SPaul Moore case CIPSO_V4_MAP_PASS: 87202752760SPaul Moore host_spot = net_spot; 87302752760SPaul Moore break; 87415c45f7bSPaul Moore case CIPSO_V4_MAP_TRANS: 87502752760SPaul Moore if (net_spot >= net_cat_size) 87602752760SPaul Moore return -EPERM; 877446fda4fSPaul Moore host_spot = net_cat_array[net_spot]; 8789fade4bfSPaul Moore if (host_spot >= CIPSO_V4_INV_CAT) 8799fade4bfSPaul Moore return -EPERM; 88002752760SPaul Moore break; 881446fda4fSPaul Moore } 8824fbe63d1SPaul Moore ret_val = netlbl_catmap_setbit(&secattr->attr.mls.cat, 88302752760SPaul Moore host_spot, 88402752760SPaul Moore GFP_ATOMIC); 88502752760SPaul Moore if (ret_val != 0) 88602752760SPaul Moore return ret_val; 887446fda4fSPaul Moore } 888446fda4fSPaul Moore 889446fda4fSPaul Moore return -EINVAL; 890446fda4fSPaul Moore } 891446fda4fSPaul Moore 892654bbc2aSPaul Moore /** 893654bbc2aSPaul Moore * cipso_v4_map_cat_enum_valid - Checks to see if the categories are valid 894654bbc2aSPaul Moore * @doi_def: the DOI definition 895654bbc2aSPaul Moore * @enumcat: category list 896654bbc2aSPaul Moore * @enumcat_len: length of the category list in bytes 897654bbc2aSPaul Moore * 898654bbc2aSPaul Moore * Description: 899654bbc2aSPaul Moore * Checks the given categories against the given DOI definition and returns a 900654bbc2aSPaul Moore * negative value if any of the categories do not have a valid mapping and a 901654bbc2aSPaul Moore * zero value if all of the categories are valid. 902654bbc2aSPaul Moore * 903654bbc2aSPaul Moore */ 904654bbc2aSPaul Moore static int cipso_v4_map_cat_enum_valid(const struct cipso_v4_doi *doi_def, 905654bbc2aSPaul Moore const unsigned char *enumcat, 906654bbc2aSPaul Moore u32 enumcat_len) 907654bbc2aSPaul Moore { 908654bbc2aSPaul Moore u16 cat; 909654bbc2aSPaul Moore int cat_prev = -1; 910654bbc2aSPaul Moore u32 iter; 911654bbc2aSPaul Moore 912654bbc2aSPaul Moore if (doi_def->type != CIPSO_V4_MAP_PASS || enumcat_len & 0x01) 913654bbc2aSPaul Moore return -EFAULT; 914654bbc2aSPaul Moore 915654bbc2aSPaul Moore for (iter = 0; iter < enumcat_len; iter += 2) { 916d3e2ce3bSHarvey Harrison cat = get_unaligned_be16(&enumcat[iter]); 917654bbc2aSPaul Moore if (cat <= cat_prev) 918654bbc2aSPaul Moore return -EFAULT; 919654bbc2aSPaul Moore cat_prev = cat; 920654bbc2aSPaul Moore } 921654bbc2aSPaul Moore 922654bbc2aSPaul Moore return 0; 923654bbc2aSPaul Moore } 924654bbc2aSPaul Moore 925654bbc2aSPaul Moore /** 926654bbc2aSPaul Moore * cipso_v4_map_cat_enum_hton - Perform a category mapping from host to network 927654bbc2aSPaul Moore * @doi_def: the DOI definition 928654bbc2aSPaul Moore * @secattr: the security attributes 929654bbc2aSPaul Moore * @net_cat: the zero'd out category list in network/CIPSO format 930654bbc2aSPaul Moore * @net_cat_len: the length of the CIPSO category list in bytes 931654bbc2aSPaul Moore * 932654bbc2aSPaul Moore * Description: 933654bbc2aSPaul Moore * Perform a label mapping to translate a local MLS category bitmap to the 934654bbc2aSPaul Moore * correct CIPSO category list using the given DOI definition. Returns the 935654bbc2aSPaul Moore * size in bytes of the network category bitmap on success, negative values 936654bbc2aSPaul Moore * otherwise. 937654bbc2aSPaul Moore * 938654bbc2aSPaul Moore */ 939654bbc2aSPaul Moore static int cipso_v4_map_cat_enum_hton(const struct cipso_v4_doi *doi_def, 940654bbc2aSPaul Moore const struct netlbl_lsm_secattr *secattr, 941654bbc2aSPaul Moore unsigned char *net_cat, 942654bbc2aSPaul Moore u32 net_cat_len) 943654bbc2aSPaul Moore { 944654bbc2aSPaul Moore int cat = -1; 945654bbc2aSPaul Moore u32 cat_iter = 0; 946654bbc2aSPaul Moore 947654bbc2aSPaul Moore for (;;) { 9484fbe63d1SPaul Moore cat = netlbl_catmap_walk(secattr->attr.mls.cat, cat + 1); 949654bbc2aSPaul Moore if (cat < 0) 950654bbc2aSPaul Moore break; 951654bbc2aSPaul Moore if ((cat_iter + 2) > net_cat_len) 952654bbc2aSPaul Moore return -ENOSPC; 953654bbc2aSPaul Moore 954654bbc2aSPaul Moore *((__be16 *)&net_cat[cat_iter]) = htons(cat); 955654bbc2aSPaul Moore cat_iter += 2; 956654bbc2aSPaul Moore } 957654bbc2aSPaul Moore 958654bbc2aSPaul Moore return cat_iter; 959654bbc2aSPaul Moore } 960654bbc2aSPaul Moore 961654bbc2aSPaul Moore /** 962654bbc2aSPaul Moore * cipso_v4_map_cat_enum_ntoh - Perform a category mapping from network to host 963654bbc2aSPaul Moore * @doi_def: the DOI definition 964654bbc2aSPaul Moore * @net_cat: the category list in network/CIPSO format 965654bbc2aSPaul Moore * @net_cat_len: the length of the CIPSO bitmap in bytes 966654bbc2aSPaul Moore * @secattr: the security attributes 967654bbc2aSPaul Moore * 968654bbc2aSPaul Moore * Description: 969654bbc2aSPaul Moore * Perform a label mapping to translate a CIPSO category list to the correct 970654bbc2aSPaul Moore * local MLS category bitmap using the given DOI definition. Returns zero on 971654bbc2aSPaul Moore * success, negative values on failure. 972654bbc2aSPaul Moore * 973654bbc2aSPaul Moore */ 974654bbc2aSPaul Moore static int cipso_v4_map_cat_enum_ntoh(const struct cipso_v4_doi *doi_def, 975654bbc2aSPaul Moore const unsigned char *net_cat, 976654bbc2aSPaul Moore u32 net_cat_len, 977654bbc2aSPaul Moore struct netlbl_lsm_secattr *secattr) 978654bbc2aSPaul Moore { 979654bbc2aSPaul Moore int ret_val; 980654bbc2aSPaul Moore u32 iter; 981654bbc2aSPaul Moore 982654bbc2aSPaul Moore for (iter = 0; iter < net_cat_len; iter += 2) { 9834fbe63d1SPaul Moore ret_val = netlbl_catmap_setbit(&secattr->attr.mls.cat, 984d3e2ce3bSHarvey Harrison get_unaligned_be16(&net_cat[iter]), 985654bbc2aSPaul Moore GFP_ATOMIC); 986654bbc2aSPaul Moore if (ret_val != 0) 987654bbc2aSPaul Moore return ret_val; 988654bbc2aSPaul Moore } 989654bbc2aSPaul Moore 990654bbc2aSPaul Moore return 0; 991654bbc2aSPaul Moore } 992654bbc2aSPaul Moore 993484b3669SPaul Moore /** 994484b3669SPaul Moore * cipso_v4_map_cat_rng_valid - Checks to see if the categories are valid 995484b3669SPaul Moore * @doi_def: the DOI definition 996484b3669SPaul Moore * @rngcat: category list 997484b3669SPaul Moore * @rngcat_len: length of the category list in bytes 998484b3669SPaul Moore * 999484b3669SPaul Moore * Description: 1000484b3669SPaul Moore * Checks the given categories against the given DOI definition and returns a 1001484b3669SPaul Moore * negative value if any of the categories do not have a valid mapping and a 1002484b3669SPaul Moore * zero value if all of the categories are valid. 1003484b3669SPaul Moore * 1004484b3669SPaul Moore */ 1005484b3669SPaul Moore static int cipso_v4_map_cat_rng_valid(const struct cipso_v4_doi *doi_def, 1006484b3669SPaul Moore const unsigned char *rngcat, 1007484b3669SPaul Moore u32 rngcat_len) 1008484b3669SPaul Moore { 1009484b3669SPaul Moore u16 cat_high; 1010484b3669SPaul Moore u16 cat_low; 1011484b3669SPaul Moore u32 cat_prev = CIPSO_V4_MAX_REM_CATS + 1; 1012484b3669SPaul Moore u32 iter; 1013484b3669SPaul Moore 1014484b3669SPaul Moore if (doi_def->type != CIPSO_V4_MAP_PASS || rngcat_len & 0x01) 1015484b3669SPaul Moore return -EFAULT; 1016484b3669SPaul Moore 1017484b3669SPaul Moore for (iter = 0; iter < rngcat_len; iter += 4) { 1018d3e2ce3bSHarvey Harrison cat_high = get_unaligned_be16(&rngcat[iter]); 1019484b3669SPaul Moore if ((iter + 4) <= rngcat_len) 1020d3e2ce3bSHarvey Harrison cat_low = get_unaligned_be16(&rngcat[iter + 2]); 1021484b3669SPaul Moore else 1022484b3669SPaul Moore cat_low = 0; 1023484b3669SPaul Moore 1024484b3669SPaul Moore if (cat_high > cat_prev) 1025484b3669SPaul Moore return -EFAULT; 1026484b3669SPaul Moore 1027484b3669SPaul Moore cat_prev = cat_low; 1028484b3669SPaul Moore } 1029484b3669SPaul Moore 1030484b3669SPaul Moore return 0; 1031484b3669SPaul Moore } 1032484b3669SPaul Moore 1033484b3669SPaul Moore /** 1034484b3669SPaul Moore * cipso_v4_map_cat_rng_hton - Perform a category mapping from host to network 1035484b3669SPaul Moore * @doi_def: the DOI definition 1036484b3669SPaul Moore * @secattr: the security attributes 1037484b3669SPaul Moore * @net_cat: the zero'd out category list in network/CIPSO format 1038484b3669SPaul Moore * @net_cat_len: the length of the CIPSO category list in bytes 1039484b3669SPaul Moore * 1040484b3669SPaul Moore * Description: 1041484b3669SPaul Moore * Perform a label mapping to translate a local MLS category bitmap to the 1042484b3669SPaul Moore * correct CIPSO category list using the given DOI definition. Returns the 1043484b3669SPaul Moore * size in bytes of the network category bitmap on success, negative values 1044484b3669SPaul Moore * otherwise. 1045484b3669SPaul Moore * 1046484b3669SPaul Moore */ 1047484b3669SPaul Moore static int cipso_v4_map_cat_rng_hton(const struct cipso_v4_doi *doi_def, 1048484b3669SPaul Moore const struct netlbl_lsm_secattr *secattr, 1049484b3669SPaul Moore unsigned char *net_cat, 1050484b3669SPaul Moore u32 net_cat_len) 1051484b3669SPaul Moore { 1052484b3669SPaul Moore int iter = -1; 1053f998e8cbSPaul Moore u16 array[CIPSO_V4_TAG_RNG_CAT_MAX * 2]; 1054484b3669SPaul Moore u32 array_cnt = 0; 1055484b3669SPaul Moore u32 cat_size = 0; 1056484b3669SPaul Moore 1057f998e8cbSPaul Moore /* make sure we don't overflow the 'array[]' variable */ 1058128c6b6cSPaul Moore if (net_cat_len > 1059128c6b6cSPaul Moore (CIPSO_V4_OPT_LEN_MAX - CIPSO_V4_HDR_LEN - CIPSO_V4_TAG_RNG_BLEN)) 1060128c6b6cSPaul Moore return -ENOSPC; 1061484b3669SPaul Moore 1062484b3669SPaul Moore for (;;) { 10634fbe63d1SPaul Moore iter = netlbl_catmap_walk(secattr->attr.mls.cat, iter + 1); 1064484b3669SPaul Moore if (iter < 0) 1065484b3669SPaul Moore break; 1066484b3669SPaul Moore cat_size += (iter == 0 ? 0 : sizeof(u16)); 1067484b3669SPaul Moore if (cat_size > net_cat_len) 1068484b3669SPaul Moore return -ENOSPC; 1069484b3669SPaul Moore array[array_cnt++] = iter; 1070484b3669SPaul Moore 10714fbe63d1SPaul Moore iter = netlbl_catmap_walkrng(secattr->attr.mls.cat, iter); 1072484b3669SPaul Moore if (iter < 0) 1073484b3669SPaul Moore return -EFAULT; 1074484b3669SPaul Moore cat_size += sizeof(u16); 1075484b3669SPaul Moore if (cat_size > net_cat_len) 1076484b3669SPaul Moore return -ENOSPC; 1077484b3669SPaul Moore array[array_cnt++] = iter; 1078484b3669SPaul Moore } 1079484b3669SPaul Moore 1080484b3669SPaul Moore for (iter = 0; array_cnt > 0;) { 1081484b3669SPaul Moore *((__be16 *)&net_cat[iter]) = htons(array[--array_cnt]); 1082484b3669SPaul Moore iter += 2; 1083484b3669SPaul Moore array_cnt--; 1084484b3669SPaul Moore if (array[array_cnt] != 0) { 1085484b3669SPaul Moore *((__be16 *)&net_cat[iter]) = htons(array[array_cnt]); 1086484b3669SPaul Moore iter += 2; 1087484b3669SPaul Moore } 1088484b3669SPaul Moore } 1089484b3669SPaul Moore 1090484b3669SPaul Moore return cat_size; 1091484b3669SPaul Moore } 1092484b3669SPaul Moore 1093484b3669SPaul Moore /** 1094484b3669SPaul Moore * cipso_v4_map_cat_rng_ntoh - Perform a category mapping from network to host 1095484b3669SPaul Moore * @doi_def: the DOI definition 1096484b3669SPaul Moore * @net_cat: the category list in network/CIPSO format 1097484b3669SPaul Moore * @net_cat_len: the length of the CIPSO bitmap in bytes 1098484b3669SPaul Moore * @secattr: the security attributes 1099484b3669SPaul Moore * 1100484b3669SPaul Moore * Description: 1101484b3669SPaul Moore * Perform a label mapping to translate a CIPSO category list to the correct 1102484b3669SPaul Moore * local MLS category bitmap using the given DOI definition. Returns zero on 1103484b3669SPaul Moore * success, negative values on failure. 1104484b3669SPaul Moore * 1105484b3669SPaul Moore */ 1106484b3669SPaul Moore static int cipso_v4_map_cat_rng_ntoh(const struct cipso_v4_doi *doi_def, 1107484b3669SPaul Moore const unsigned char *net_cat, 1108484b3669SPaul Moore u32 net_cat_len, 1109484b3669SPaul Moore struct netlbl_lsm_secattr *secattr) 1110484b3669SPaul Moore { 1111484b3669SPaul Moore int ret_val; 1112484b3669SPaul Moore u32 net_iter; 1113484b3669SPaul Moore u16 cat_low; 1114484b3669SPaul Moore u16 cat_high; 1115484b3669SPaul Moore 1116484b3669SPaul Moore for (net_iter = 0; net_iter < net_cat_len; net_iter += 4) { 1117d3e2ce3bSHarvey Harrison cat_high = get_unaligned_be16(&net_cat[net_iter]); 1118484b3669SPaul Moore if ((net_iter + 4) <= net_cat_len) 1119d3e2ce3bSHarvey Harrison cat_low = get_unaligned_be16(&net_cat[net_iter + 2]); 1120484b3669SPaul Moore else 1121484b3669SPaul Moore cat_low = 0; 1122484b3669SPaul Moore 11234fbe63d1SPaul Moore ret_val = netlbl_catmap_setrng(&secattr->attr.mls.cat, 1124484b3669SPaul Moore cat_low, 1125484b3669SPaul Moore cat_high, 1126484b3669SPaul Moore GFP_ATOMIC); 1127484b3669SPaul Moore if (ret_val != 0) 1128484b3669SPaul Moore return ret_val; 1129484b3669SPaul Moore } 1130484b3669SPaul Moore 1131484b3669SPaul Moore return 0; 1132484b3669SPaul Moore } 1133484b3669SPaul Moore 1134446fda4fSPaul Moore /* 1135446fda4fSPaul Moore * Protocol Handling Functions 1136446fda4fSPaul Moore */ 1137446fda4fSPaul Moore 1138446fda4fSPaul Moore /** 1139446fda4fSPaul Moore * cipso_v4_gentag_hdr - Generate a CIPSO option header 1140446fda4fSPaul Moore * @doi_def: the DOI definition 114191b1ed0aSPaul Moore * @len: the total tag length in bytes, not including this header 1142446fda4fSPaul Moore * @buf: the CIPSO option buffer 1143446fda4fSPaul Moore * 1144446fda4fSPaul Moore * Description: 114591b1ed0aSPaul Moore * Write a CIPSO header into the beginning of @buffer. 1146446fda4fSPaul Moore * 1147446fda4fSPaul Moore */ 114891b1ed0aSPaul Moore static void cipso_v4_gentag_hdr(const struct cipso_v4_doi *doi_def, 114991b1ed0aSPaul Moore unsigned char *buf, 115091b1ed0aSPaul Moore u32 len) 1151446fda4fSPaul Moore { 1152446fda4fSPaul Moore buf[0] = IPOPT_CIPSO; 1153446fda4fSPaul Moore buf[1] = CIPSO_V4_HDR_LEN + len; 1154e233febdSSergey Nazarov put_unaligned_be32(doi_def->doi, &buf[2]); 1155446fda4fSPaul Moore } 1156446fda4fSPaul Moore 1157446fda4fSPaul Moore /** 1158446fda4fSPaul Moore * cipso_v4_gentag_rbm - Generate a CIPSO restricted bitmap tag (type #1) 1159446fda4fSPaul Moore * @doi_def: the DOI definition 1160446fda4fSPaul Moore * @secattr: the security attributes 1161446fda4fSPaul Moore * @buffer: the option buffer 1162446fda4fSPaul Moore * @buffer_len: length of buffer in bytes 1163446fda4fSPaul Moore * 1164446fda4fSPaul Moore * Description: 1165446fda4fSPaul Moore * Generate a CIPSO option using the restricted bitmap tag, tag type #1. The 1166446fda4fSPaul Moore * actual buffer length may be larger than the indicated size due to 116791b1ed0aSPaul Moore * translation between host and network category bitmaps. Returns the size of 116891b1ed0aSPaul Moore * the tag on success, negative values on failure. 1169446fda4fSPaul Moore * 1170446fda4fSPaul Moore */ 1171446fda4fSPaul Moore static int cipso_v4_gentag_rbm(const struct cipso_v4_doi *doi_def, 1172446fda4fSPaul Moore const struct netlbl_lsm_secattr *secattr, 117391b1ed0aSPaul Moore unsigned char *buffer, 117491b1ed0aSPaul Moore u32 buffer_len) 1175446fda4fSPaul Moore { 1176701a90baSPaul Moore int ret_val; 117791b1ed0aSPaul Moore u32 tag_len; 1178446fda4fSPaul Moore u32 level; 1179446fda4fSPaul Moore 1180701a90baSPaul Moore if ((secattr->flags & NETLBL_SECATTR_MLS_LVL) == 0) 1181701a90baSPaul Moore return -EPERM; 1182701a90baSPaul Moore 118316efd454SPaul Moore ret_val = cipso_v4_map_lvl_hton(doi_def, 118416efd454SPaul Moore secattr->attr.mls.lvl, 118516efd454SPaul Moore &level); 118691b1ed0aSPaul Moore if (ret_val != 0) 118791b1ed0aSPaul Moore return ret_val; 1188446fda4fSPaul Moore 118991b1ed0aSPaul Moore if (secattr->flags & NETLBL_SECATTR_MLS_CAT) { 1190446fda4fSPaul Moore ret_val = cipso_v4_map_cat_rbm_hton(doi_def, 119102752760SPaul Moore secattr, 119291b1ed0aSPaul Moore &buffer[4], 119391b1ed0aSPaul Moore buffer_len - 4); 1194446fda4fSPaul Moore if (ret_val < 0) 119591b1ed0aSPaul Moore return ret_val; 1196446fda4fSPaul Moore 1197446fda4fSPaul Moore /* This will send packets using the "optimized" format when 119825985edcSLucas De Marchi * possible as specified in section 3.4.2.6 of the 1199446fda4fSPaul Moore * CIPSO draft. */ 1200dd44f04bSKuniyuki Iwashima if (READ_ONCE(cipso_v4_rbm_optfmt) && ret_val > 0 && 1201dd44f04bSKuniyuki Iwashima ret_val <= 10) 120291b1ed0aSPaul Moore tag_len = 14; 1203701a90baSPaul Moore else 120491b1ed0aSPaul Moore tag_len = 4 + ret_val; 120591b1ed0aSPaul Moore } else 120691b1ed0aSPaul Moore tag_len = 4; 1207446fda4fSPaul Moore 120815c45f7bSPaul Moore buffer[0] = CIPSO_V4_TAG_RBITMAP; 120991b1ed0aSPaul Moore buffer[1] = tag_len; 121091b1ed0aSPaul Moore buffer[3] = level; 1211446fda4fSPaul Moore 121291b1ed0aSPaul Moore return tag_len; 1213446fda4fSPaul Moore } 1214446fda4fSPaul Moore 1215446fda4fSPaul Moore /** 1216446fda4fSPaul Moore * cipso_v4_parsetag_rbm - Parse a CIPSO restricted bitmap tag 1217446fda4fSPaul Moore * @doi_def: the DOI definition 1218446fda4fSPaul Moore * @tag: the CIPSO tag 1219446fda4fSPaul Moore * @secattr: the security attributes 1220446fda4fSPaul Moore * 1221446fda4fSPaul Moore * Description: 1222446fda4fSPaul Moore * Parse a CIPSO restricted bitmap tag (tag type #1) and return the security 1223446fda4fSPaul Moore * attributes in @secattr. Return zero on success, negatives values on 1224446fda4fSPaul Moore * failure. 1225446fda4fSPaul Moore * 1226446fda4fSPaul Moore */ 1227446fda4fSPaul Moore static int cipso_v4_parsetag_rbm(const struct cipso_v4_doi *doi_def, 1228446fda4fSPaul Moore const unsigned char *tag, 1229446fda4fSPaul Moore struct netlbl_lsm_secattr *secattr) 1230446fda4fSPaul Moore { 1231446fda4fSPaul Moore int ret_val; 1232446fda4fSPaul Moore u8 tag_len = tag[1]; 1233446fda4fSPaul Moore u32 level; 1234446fda4fSPaul Moore 1235446fda4fSPaul Moore ret_val = cipso_v4_map_lvl_ntoh(doi_def, tag[3], &level); 1236446fda4fSPaul Moore if (ret_val != 0) 1237446fda4fSPaul Moore return ret_val; 123816efd454SPaul Moore secattr->attr.mls.lvl = level; 1239701a90baSPaul Moore secattr->flags |= NETLBL_SECATTR_MLS_LVL; 1240446fda4fSPaul Moore 1241446fda4fSPaul Moore if (tag_len > 4) { 1242446fda4fSPaul Moore ret_val = cipso_v4_map_cat_rbm_ntoh(doi_def, 1243446fda4fSPaul Moore &tag[4], 1244446fda4fSPaul Moore tag_len - 4, 124502752760SPaul Moore secattr); 124602752760SPaul Moore if (ret_val != 0) { 12474fbe63d1SPaul Moore netlbl_catmap_free(secattr->attr.mls.cat); 1248446fda4fSPaul Moore return ret_val; 1249701a90baSPaul Moore } 125002752760SPaul Moore 1251eead1c2eSPaolo Abeni if (secattr->attr.mls.cat) 125202752760SPaul Moore secattr->flags |= NETLBL_SECATTR_MLS_CAT; 1253446fda4fSPaul Moore } 1254446fda4fSPaul Moore 1255446fda4fSPaul Moore return 0; 1256446fda4fSPaul Moore } 1257446fda4fSPaul Moore 1258446fda4fSPaul Moore /** 1259654bbc2aSPaul Moore * cipso_v4_gentag_enum - Generate a CIPSO enumerated tag (type #2) 1260654bbc2aSPaul Moore * @doi_def: the DOI definition 1261654bbc2aSPaul Moore * @secattr: the security attributes 1262654bbc2aSPaul Moore * @buffer: the option buffer 1263654bbc2aSPaul Moore * @buffer_len: length of buffer in bytes 1264654bbc2aSPaul Moore * 1265654bbc2aSPaul Moore * Description: 1266654bbc2aSPaul Moore * Generate a CIPSO option using the enumerated tag, tag type #2. Returns the 1267654bbc2aSPaul Moore * size of the tag on success, negative values on failure. 1268654bbc2aSPaul Moore * 1269654bbc2aSPaul Moore */ 1270654bbc2aSPaul Moore static int cipso_v4_gentag_enum(const struct cipso_v4_doi *doi_def, 1271654bbc2aSPaul Moore const struct netlbl_lsm_secattr *secattr, 1272654bbc2aSPaul Moore unsigned char *buffer, 1273654bbc2aSPaul Moore u32 buffer_len) 1274654bbc2aSPaul Moore { 1275654bbc2aSPaul Moore int ret_val; 1276654bbc2aSPaul Moore u32 tag_len; 1277654bbc2aSPaul Moore u32 level; 1278654bbc2aSPaul Moore 1279654bbc2aSPaul Moore if (!(secattr->flags & NETLBL_SECATTR_MLS_LVL)) 1280654bbc2aSPaul Moore return -EPERM; 1281654bbc2aSPaul Moore 128216efd454SPaul Moore ret_val = cipso_v4_map_lvl_hton(doi_def, 128316efd454SPaul Moore secattr->attr.mls.lvl, 128416efd454SPaul Moore &level); 1285654bbc2aSPaul Moore if (ret_val != 0) 1286654bbc2aSPaul Moore return ret_val; 1287654bbc2aSPaul Moore 1288654bbc2aSPaul Moore if (secattr->flags & NETLBL_SECATTR_MLS_CAT) { 1289654bbc2aSPaul Moore ret_val = cipso_v4_map_cat_enum_hton(doi_def, 1290654bbc2aSPaul Moore secattr, 1291654bbc2aSPaul Moore &buffer[4], 1292654bbc2aSPaul Moore buffer_len - 4); 1293654bbc2aSPaul Moore if (ret_val < 0) 1294654bbc2aSPaul Moore return ret_val; 1295654bbc2aSPaul Moore 1296654bbc2aSPaul Moore tag_len = 4 + ret_val; 1297654bbc2aSPaul Moore } else 1298654bbc2aSPaul Moore tag_len = 4; 1299654bbc2aSPaul Moore 130015c45f7bSPaul Moore buffer[0] = CIPSO_V4_TAG_ENUM; 1301654bbc2aSPaul Moore buffer[1] = tag_len; 1302654bbc2aSPaul Moore buffer[3] = level; 1303654bbc2aSPaul Moore 1304654bbc2aSPaul Moore return tag_len; 1305654bbc2aSPaul Moore } 1306654bbc2aSPaul Moore 1307654bbc2aSPaul Moore /** 1308654bbc2aSPaul Moore * cipso_v4_parsetag_enum - Parse a CIPSO enumerated tag 1309654bbc2aSPaul Moore * @doi_def: the DOI definition 1310654bbc2aSPaul Moore * @tag: the CIPSO tag 1311654bbc2aSPaul Moore * @secattr: the security attributes 1312654bbc2aSPaul Moore * 1313654bbc2aSPaul Moore * Description: 1314654bbc2aSPaul Moore * Parse a CIPSO enumerated tag (tag type #2) and return the security 1315654bbc2aSPaul Moore * attributes in @secattr. Return zero on success, negatives values on 1316654bbc2aSPaul Moore * failure. 1317654bbc2aSPaul Moore * 1318654bbc2aSPaul Moore */ 1319654bbc2aSPaul Moore static int cipso_v4_parsetag_enum(const struct cipso_v4_doi *doi_def, 1320654bbc2aSPaul Moore const unsigned char *tag, 1321654bbc2aSPaul Moore struct netlbl_lsm_secattr *secattr) 1322654bbc2aSPaul Moore { 1323654bbc2aSPaul Moore int ret_val; 1324654bbc2aSPaul Moore u8 tag_len = tag[1]; 1325654bbc2aSPaul Moore u32 level; 1326654bbc2aSPaul Moore 1327654bbc2aSPaul Moore ret_val = cipso_v4_map_lvl_ntoh(doi_def, tag[3], &level); 1328654bbc2aSPaul Moore if (ret_val != 0) 1329654bbc2aSPaul Moore return ret_val; 133016efd454SPaul Moore secattr->attr.mls.lvl = level; 1331654bbc2aSPaul Moore secattr->flags |= NETLBL_SECATTR_MLS_LVL; 1332654bbc2aSPaul Moore 1333654bbc2aSPaul Moore if (tag_len > 4) { 1334654bbc2aSPaul Moore ret_val = cipso_v4_map_cat_enum_ntoh(doi_def, 1335654bbc2aSPaul Moore &tag[4], 1336654bbc2aSPaul Moore tag_len - 4, 1337654bbc2aSPaul Moore secattr); 1338654bbc2aSPaul Moore if (ret_val != 0) { 13394fbe63d1SPaul Moore netlbl_catmap_free(secattr->attr.mls.cat); 1340654bbc2aSPaul Moore return ret_val; 1341654bbc2aSPaul Moore } 1342654bbc2aSPaul Moore 1343654bbc2aSPaul Moore secattr->flags |= NETLBL_SECATTR_MLS_CAT; 1344654bbc2aSPaul Moore } 1345654bbc2aSPaul Moore 1346654bbc2aSPaul Moore return 0; 1347654bbc2aSPaul Moore } 1348654bbc2aSPaul Moore 1349654bbc2aSPaul Moore /** 1350484b3669SPaul Moore * cipso_v4_gentag_rng - Generate a CIPSO ranged tag (type #5) 1351484b3669SPaul Moore * @doi_def: the DOI definition 1352484b3669SPaul Moore * @secattr: the security attributes 1353484b3669SPaul Moore * @buffer: the option buffer 1354484b3669SPaul Moore * @buffer_len: length of buffer in bytes 1355484b3669SPaul Moore * 1356484b3669SPaul Moore * Description: 1357484b3669SPaul Moore * Generate a CIPSO option using the ranged tag, tag type #5. Returns the 1358484b3669SPaul Moore * size of the tag on success, negative values on failure. 1359484b3669SPaul Moore * 1360484b3669SPaul Moore */ 1361484b3669SPaul Moore static int cipso_v4_gentag_rng(const struct cipso_v4_doi *doi_def, 1362484b3669SPaul Moore const struct netlbl_lsm_secattr *secattr, 1363484b3669SPaul Moore unsigned char *buffer, 1364484b3669SPaul Moore u32 buffer_len) 1365484b3669SPaul Moore { 1366484b3669SPaul Moore int ret_val; 1367484b3669SPaul Moore u32 tag_len; 1368484b3669SPaul Moore u32 level; 1369484b3669SPaul Moore 1370484b3669SPaul Moore if (!(secattr->flags & NETLBL_SECATTR_MLS_LVL)) 1371484b3669SPaul Moore return -EPERM; 1372484b3669SPaul Moore 137316efd454SPaul Moore ret_val = cipso_v4_map_lvl_hton(doi_def, 137416efd454SPaul Moore secattr->attr.mls.lvl, 137516efd454SPaul Moore &level); 1376484b3669SPaul Moore if (ret_val != 0) 1377484b3669SPaul Moore return ret_val; 1378484b3669SPaul Moore 1379484b3669SPaul Moore if (secattr->flags & NETLBL_SECATTR_MLS_CAT) { 1380484b3669SPaul Moore ret_val = cipso_v4_map_cat_rng_hton(doi_def, 1381484b3669SPaul Moore secattr, 1382484b3669SPaul Moore &buffer[4], 1383484b3669SPaul Moore buffer_len - 4); 1384484b3669SPaul Moore if (ret_val < 0) 1385484b3669SPaul Moore return ret_val; 1386484b3669SPaul Moore 1387484b3669SPaul Moore tag_len = 4 + ret_val; 1388484b3669SPaul Moore } else 1389484b3669SPaul Moore tag_len = 4; 1390484b3669SPaul Moore 139115c45f7bSPaul Moore buffer[0] = CIPSO_V4_TAG_RANGE; 1392484b3669SPaul Moore buffer[1] = tag_len; 1393484b3669SPaul Moore buffer[3] = level; 1394484b3669SPaul Moore 1395484b3669SPaul Moore return tag_len; 1396484b3669SPaul Moore } 1397484b3669SPaul Moore 1398484b3669SPaul Moore /** 1399484b3669SPaul Moore * cipso_v4_parsetag_rng - Parse a CIPSO ranged tag 1400484b3669SPaul Moore * @doi_def: the DOI definition 1401484b3669SPaul Moore * @tag: the CIPSO tag 1402484b3669SPaul Moore * @secattr: the security attributes 1403484b3669SPaul Moore * 1404484b3669SPaul Moore * Description: 1405484b3669SPaul Moore * Parse a CIPSO ranged tag (tag type #5) and return the security attributes 1406484b3669SPaul Moore * in @secattr. Return zero on success, negatives values on failure. 1407484b3669SPaul Moore * 1408484b3669SPaul Moore */ 1409484b3669SPaul Moore static int cipso_v4_parsetag_rng(const struct cipso_v4_doi *doi_def, 1410484b3669SPaul Moore const unsigned char *tag, 1411484b3669SPaul Moore struct netlbl_lsm_secattr *secattr) 1412484b3669SPaul Moore { 1413484b3669SPaul Moore int ret_val; 1414484b3669SPaul Moore u8 tag_len = tag[1]; 1415484b3669SPaul Moore u32 level; 1416484b3669SPaul Moore 1417484b3669SPaul Moore ret_val = cipso_v4_map_lvl_ntoh(doi_def, tag[3], &level); 1418484b3669SPaul Moore if (ret_val != 0) 1419484b3669SPaul Moore return ret_val; 142016efd454SPaul Moore secattr->attr.mls.lvl = level; 1421484b3669SPaul Moore secattr->flags |= NETLBL_SECATTR_MLS_LVL; 1422484b3669SPaul Moore 1423484b3669SPaul Moore if (tag_len > 4) { 1424484b3669SPaul Moore ret_val = cipso_v4_map_cat_rng_ntoh(doi_def, 1425484b3669SPaul Moore &tag[4], 1426484b3669SPaul Moore tag_len - 4, 1427484b3669SPaul Moore secattr); 1428484b3669SPaul Moore if (ret_val != 0) { 14294fbe63d1SPaul Moore netlbl_catmap_free(secattr->attr.mls.cat); 1430484b3669SPaul Moore return ret_val; 1431484b3669SPaul Moore } 1432484b3669SPaul Moore 1433eead1c2eSPaolo Abeni if (secattr->attr.mls.cat) 1434484b3669SPaul Moore secattr->flags |= NETLBL_SECATTR_MLS_CAT; 1435484b3669SPaul Moore } 1436484b3669SPaul Moore 1437484b3669SPaul Moore return 0; 1438484b3669SPaul Moore } 1439484b3669SPaul Moore 1440484b3669SPaul Moore /** 144115c45f7bSPaul Moore * cipso_v4_gentag_loc - Generate a CIPSO local tag (non-standard) 144215c45f7bSPaul Moore * @doi_def: the DOI definition 144315c45f7bSPaul Moore * @secattr: the security attributes 144415c45f7bSPaul Moore * @buffer: the option buffer 144515c45f7bSPaul Moore * @buffer_len: length of buffer in bytes 144615c45f7bSPaul Moore * 144715c45f7bSPaul Moore * Description: 144815c45f7bSPaul Moore * Generate a CIPSO option using the local tag. Returns the size of the tag 144915c45f7bSPaul Moore * on success, negative values on failure. 145015c45f7bSPaul Moore * 145115c45f7bSPaul Moore */ 145215c45f7bSPaul Moore static int cipso_v4_gentag_loc(const struct cipso_v4_doi *doi_def, 145315c45f7bSPaul Moore const struct netlbl_lsm_secattr *secattr, 145415c45f7bSPaul Moore unsigned char *buffer, 145515c45f7bSPaul Moore u32 buffer_len) 145615c45f7bSPaul Moore { 145715c45f7bSPaul Moore if (!(secattr->flags & NETLBL_SECATTR_SECID)) 145815c45f7bSPaul Moore return -EPERM; 145915c45f7bSPaul Moore 146015c45f7bSPaul Moore buffer[0] = CIPSO_V4_TAG_LOCAL; 146115c45f7bSPaul Moore buffer[1] = CIPSO_V4_TAG_LOC_BLEN; 146215c45f7bSPaul Moore *(u32 *)&buffer[2] = secattr->attr.secid; 146315c45f7bSPaul Moore 146415c45f7bSPaul Moore return CIPSO_V4_TAG_LOC_BLEN; 146515c45f7bSPaul Moore } 146615c45f7bSPaul Moore 146715c45f7bSPaul Moore /** 146815c45f7bSPaul Moore * cipso_v4_parsetag_loc - Parse a CIPSO local tag 146915c45f7bSPaul Moore * @doi_def: the DOI definition 147015c45f7bSPaul Moore * @tag: the CIPSO tag 147115c45f7bSPaul Moore * @secattr: the security attributes 147215c45f7bSPaul Moore * 147315c45f7bSPaul Moore * Description: 147415c45f7bSPaul Moore * Parse a CIPSO local tag and return the security attributes in @secattr. 147515c45f7bSPaul Moore * Return zero on success, negatives values on failure. 147615c45f7bSPaul Moore * 147715c45f7bSPaul Moore */ 147815c45f7bSPaul Moore static int cipso_v4_parsetag_loc(const struct cipso_v4_doi *doi_def, 147915c45f7bSPaul Moore const unsigned char *tag, 148015c45f7bSPaul Moore struct netlbl_lsm_secattr *secattr) 148115c45f7bSPaul Moore { 148215c45f7bSPaul Moore secattr->attr.secid = *(u32 *)&tag[2]; 148315c45f7bSPaul Moore secattr->flags |= NETLBL_SECATTR_SECID; 148415c45f7bSPaul Moore 148515c45f7bSPaul Moore return 0; 148615c45f7bSPaul Moore } 148715c45f7bSPaul Moore 148815c45f7bSPaul Moore /** 148904f81f01SPaul Moore * cipso_v4_optptr - Find the CIPSO option in the packet 149004f81f01SPaul Moore * @skb: the packet 149104f81f01SPaul Moore * 149204f81f01SPaul Moore * Description: 149304f81f01SPaul Moore * Parse the packet's IP header looking for a CIPSO option. Returns a pointer 1494076ed3daSStefan Nuernberger * to the start of the CIPSO option on success, NULL if one is not found. 149504f81f01SPaul Moore * 149604f81f01SPaul Moore */ 149704f81f01SPaul Moore unsigned char *cipso_v4_optptr(const struct sk_buff *skb) 149804f81f01SPaul Moore { 149904f81f01SPaul Moore const struct iphdr *iph = ip_hdr(skb); 150004f81f01SPaul Moore unsigned char *optptr = (unsigned char *)&(ip_hdr(skb)[1]); 150104f81f01SPaul Moore int optlen; 150204f81f01SPaul Moore int taglen; 150304f81f01SPaul Moore 1504076ed3daSStefan Nuernberger for (optlen = iph->ihl*4 - sizeof(struct iphdr); optlen > 1; ) { 150540413955Syujuan.qi switch (optptr[0]) { 150640413955Syujuan.qi case IPOPT_END: 150740413955Syujuan.qi return NULL; 150840413955Syujuan.qi case IPOPT_NOOP: 150940413955Syujuan.qi taglen = 1; 151040413955Syujuan.qi break; 151140413955Syujuan.qi default: 151204f81f01SPaul Moore taglen = optptr[1]; 151340413955Syujuan.qi } 1514076ed3daSStefan Nuernberger if (!taglen || taglen > optlen) 1515076ed3daSStefan Nuernberger return NULL; 1516076ed3daSStefan Nuernberger if (optptr[0] == IPOPT_CIPSO) 1517076ed3daSStefan Nuernberger return optptr; 1518076ed3daSStefan Nuernberger 151904f81f01SPaul Moore optlen -= taglen; 152004f81f01SPaul Moore optptr += taglen; 152104f81f01SPaul Moore } 152204f81f01SPaul Moore 152304f81f01SPaul Moore return NULL; 152404f81f01SPaul Moore } 152504f81f01SPaul Moore 152604f81f01SPaul Moore /** 1527446fda4fSPaul Moore * cipso_v4_validate - Validate a CIPSO option 15283628e3cbSAndrew Lunn * @skb: the packet 1529446fda4fSPaul Moore * @option: the start of the option, on error it is set to point to the error 1530446fda4fSPaul Moore * 1531446fda4fSPaul Moore * Description: 1532446fda4fSPaul Moore * This routine is called to validate a CIPSO option, it checks all of the 1533446fda4fSPaul Moore * fields to ensure that they are at least valid, see the draft snippet below 1534446fda4fSPaul Moore * for details. If the option is valid then a zero value is returned and 1535446fda4fSPaul Moore * the value of @option is unchanged. If the option is invalid then a 1536446fda4fSPaul Moore * non-zero value is returned and @option is adjusted to point to the 1537446fda4fSPaul Moore * offending portion of the option. From the IETF draft ... 1538446fda4fSPaul Moore * 1539446fda4fSPaul Moore * "If any field within the CIPSO options, such as the DOI identifier, is not 1540446fda4fSPaul Moore * recognized the IP datagram is discarded and an ICMP 'parameter problem' 1541446fda4fSPaul Moore * (type 12) is generated and returned. The ICMP code field is set to 'bad 1542446fda4fSPaul Moore * parameter' (code 0) and the pointer is set to the start of the CIPSO field 1543446fda4fSPaul Moore * that is unrecognized." 1544446fda4fSPaul Moore * 1545446fda4fSPaul Moore */ 154615c45f7bSPaul Moore int cipso_v4_validate(const struct sk_buff *skb, unsigned char **option) 1547446fda4fSPaul Moore { 1548446fda4fSPaul Moore unsigned char *opt = *option; 1549446fda4fSPaul Moore unsigned char *tag; 1550446fda4fSPaul Moore unsigned char opt_iter; 1551446fda4fSPaul Moore unsigned char err_offset = 0; 1552446fda4fSPaul Moore u8 opt_len; 1553446fda4fSPaul Moore u8 tag_len; 1554446fda4fSPaul Moore struct cipso_v4_doi *doi_def = NULL; 1555446fda4fSPaul Moore u32 tag_iter; 1556446fda4fSPaul Moore 1557446fda4fSPaul Moore /* caller already checks for length values that are too large */ 1558446fda4fSPaul Moore opt_len = opt[1]; 1559446fda4fSPaul Moore if (opt_len < 8) { 1560446fda4fSPaul Moore err_offset = 1; 1561446fda4fSPaul Moore goto validate_return; 1562446fda4fSPaul Moore } 1563446fda4fSPaul Moore 1564446fda4fSPaul Moore rcu_read_lock(); 1565d3e2ce3bSHarvey Harrison doi_def = cipso_v4_doi_search(get_unaligned_be32(&opt[2])); 156651456b29SIan Morris if (!doi_def) { 1567446fda4fSPaul Moore err_offset = 2; 1568446fda4fSPaul Moore goto validate_return_locked; 1569446fda4fSPaul Moore } 1570446fda4fSPaul Moore 157115c45f7bSPaul Moore opt_iter = CIPSO_V4_HDR_LEN; 1572446fda4fSPaul Moore tag = opt + opt_iter; 1573446fda4fSPaul Moore while (opt_iter < opt_len) { 1574446fda4fSPaul Moore for (tag_iter = 0; doi_def->tags[tag_iter] != tag[0];) 1575446fda4fSPaul Moore if (doi_def->tags[tag_iter] == CIPSO_V4_TAG_INVALID || 1576446fda4fSPaul Moore ++tag_iter == CIPSO_V4_TAG_MAXCNT) { 1577446fda4fSPaul Moore err_offset = opt_iter; 1578446fda4fSPaul Moore goto validate_return_locked; 1579446fda4fSPaul Moore } 1580446fda4fSPaul Moore 1581d71b7896SEric Dumazet if (opt_iter + 1 == opt_len) { 1582d71b7896SEric Dumazet err_offset = opt_iter; 1583d71b7896SEric Dumazet goto validate_return_locked; 1584d71b7896SEric Dumazet } 1585446fda4fSPaul Moore tag_len = tag[1]; 1586446fda4fSPaul Moore if (tag_len > (opt_len - opt_iter)) { 1587446fda4fSPaul Moore err_offset = opt_iter + 1; 1588446fda4fSPaul Moore goto validate_return_locked; 1589446fda4fSPaul Moore } 1590446fda4fSPaul Moore 1591446fda4fSPaul Moore switch (tag[0]) { 1592446fda4fSPaul Moore case CIPSO_V4_TAG_RBITMAP: 159315c45f7bSPaul Moore if (tag_len < CIPSO_V4_TAG_RBM_BLEN) { 1594446fda4fSPaul Moore err_offset = opt_iter + 1; 1595446fda4fSPaul Moore goto validate_return_locked; 1596446fda4fSPaul Moore } 1597446fda4fSPaul Moore 1598446fda4fSPaul Moore /* We are already going to do all the verification 1599446fda4fSPaul Moore * necessary at the socket layer so from our point of 1600446fda4fSPaul Moore * view it is safe to turn these checks off (and less 1601446fda4fSPaul Moore * work), however, the CIPSO draft says we should do 1602446fda4fSPaul Moore * all the CIPSO validations here but it doesn't 1603446fda4fSPaul Moore * really specify _exactly_ what we need to validate 1604446fda4fSPaul Moore * ... so, just make it a sysctl tunable. */ 1605dd44f04bSKuniyuki Iwashima if (READ_ONCE(cipso_v4_rbm_strictvalid)) { 1606446fda4fSPaul Moore if (cipso_v4_map_lvl_valid(doi_def, 1607446fda4fSPaul Moore tag[3]) < 0) { 1608446fda4fSPaul Moore err_offset = opt_iter + 3; 1609446fda4fSPaul Moore goto validate_return_locked; 1610446fda4fSPaul Moore } 161115c45f7bSPaul Moore if (tag_len > CIPSO_V4_TAG_RBM_BLEN && 1612446fda4fSPaul Moore cipso_v4_map_cat_rbm_valid(doi_def, 1613446fda4fSPaul Moore &tag[4], 1614446fda4fSPaul Moore tag_len - 4) < 0) { 1615446fda4fSPaul Moore err_offset = opt_iter + 4; 1616446fda4fSPaul Moore goto validate_return_locked; 1617446fda4fSPaul Moore } 1618446fda4fSPaul Moore } 1619446fda4fSPaul Moore break; 1620654bbc2aSPaul Moore case CIPSO_V4_TAG_ENUM: 162115c45f7bSPaul Moore if (tag_len < CIPSO_V4_TAG_ENUM_BLEN) { 1622654bbc2aSPaul Moore err_offset = opt_iter + 1; 1623654bbc2aSPaul Moore goto validate_return_locked; 1624654bbc2aSPaul Moore } 1625654bbc2aSPaul Moore 1626654bbc2aSPaul Moore if (cipso_v4_map_lvl_valid(doi_def, 1627654bbc2aSPaul Moore tag[3]) < 0) { 1628654bbc2aSPaul Moore err_offset = opt_iter + 3; 1629654bbc2aSPaul Moore goto validate_return_locked; 1630654bbc2aSPaul Moore } 163115c45f7bSPaul Moore if (tag_len > CIPSO_V4_TAG_ENUM_BLEN && 1632654bbc2aSPaul Moore cipso_v4_map_cat_enum_valid(doi_def, 1633654bbc2aSPaul Moore &tag[4], 1634654bbc2aSPaul Moore tag_len - 4) < 0) { 1635654bbc2aSPaul Moore err_offset = opt_iter + 4; 1636654bbc2aSPaul Moore goto validate_return_locked; 1637654bbc2aSPaul Moore } 1638654bbc2aSPaul Moore break; 1639484b3669SPaul Moore case CIPSO_V4_TAG_RANGE: 164015c45f7bSPaul Moore if (tag_len < CIPSO_V4_TAG_RNG_BLEN) { 1641484b3669SPaul Moore err_offset = opt_iter + 1; 1642484b3669SPaul Moore goto validate_return_locked; 1643484b3669SPaul Moore } 1644484b3669SPaul Moore 1645484b3669SPaul Moore if (cipso_v4_map_lvl_valid(doi_def, 1646484b3669SPaul Moore tag[3]) < 0) { 1647484b3669SPaul Moore err_offset = opt_iter + 3; 1648484b3669SPaul Moore goto validate_return_locked; 1649484b3669SPaul Moore } 165015c45f7bSPaul Moore if (tag_len > CIPSO_V4_TAG_RNG_BLEN && 1651484b3669SPaul Moore cipso_v4_map_cat_rng_valid(doi_def, 1652484b3669SPaul Moore &tag[4], 1653484b3669SPaul Moore tag_len - 4) < 0) { 1654484b3669SPaul Moore err_offset = opt_iter + 4; 1655484b3669SPaul Moore goto validate_return_locked; 1656484b3669SPaul Moore } 1657484b3669SPaul Moore break; 165815c45f7bSPaul Moore case CIPSO_V4_TAG_LOCAL: 165915c45f7bSPaul Moore /* This is a non-standard tag that we only allow for 166015c45f7bSPaul Moore * local connections, so if the incoming interface is 166189d7ae34SPaul Moore * not the loopback device drop the packet. Further, 166289d7ae34SPaul Moore * there is no legitimate reason for setting this from 166389d7ae34SPaul Moore * userspace so reject it if skb is NULL. */ 166451456b29SIan Morris if (!skb || !(skb->dev->flags & IFF_LOOPBACK)) { 166515c45f7bSPaul Moore err_offset = opt_iter; 166615c45f7bSPaul Moore goto validate_return_locked; 166715c45f7bSPaul Moore } 166815c45f7bSPaul Moore if (tag_len != CIPSO_V4_TAG_LOC_BLEN) { 166915c45f7bSPaul Moore err_offset = opt_iter + 1; 167015c45f7bSPaul Moore goto validate_return_locked; 167115c45f7bSPaul Moore } 167215c45f7bSPaul Moore break; 1673446fda4fSPaul Moore default: 1674446fda4fSPaul Moore err_offset = opt_iter; 1675446fda4fSPaul Moore goto validate_return_locked; 1676446fda4fSPaul Moore } 1677446fda4fSPaul Moore 1678446fda4fSPaul Moore tag += tag_len; 1679446fda4fSPaul Moore opt_iter += tag_len; 1680446fda4fSPaul Moore } 1681446fda4fSPaul Moore 1682446fda4fSPaul Moore validate_return_locked: 1683446fda4fSPaul Moore rcu_read_unlock(); 1684446fda4fSPaul Moore validate_return: 1685446fda4fSPaul Moore *option = opt + err_offset; 1686446fda4fSPaul Moore return err_offset; 1687446fda4fSPaul Moore } 1688446fda4fSPaul Moore 1689446fda4fSPaul Moore /** 169025985edcSLucas De Marchi * cipso_v4_error - Send the correct response for a bad packet 1691446fda4fSPaul Moore * @skb: the packet 1692446fda4fSPaul Moore * @error: the error code 1693446fda4fSPaul Moore * @gateway: CIPSO gateway flag 1694446fda4fSPaul Moore * 1695446fda4fSPaul Moore * Description: 1696446fda4fSPaul Moore * Based on the error code given in @error, send an ICMP error message back to 1697446fda4fSPaul Moore * the originating host. From the IETF draft ... 1698446fda4fSPaul Moore * 1699446fda4fSPaul Moore * "If the contents of the CIPSO [option] are valid but the security label is 1700446fda4fSPaul Moore * outside of the configured host or port label range, the datagram is 1701446fda4fSPaul Moore * discarded and an ICMP 'destination unreachable' (type 3) is generated and 1702446fda4fSPaul Moore * returned. The code field of the ICMP is set to 'communication with 1703446fda4fSPaul Moore * destination network administratively prohibited' (code 9) or to 1704446fda4fSPaul Moore * 'communication with destination host administratively prohibited' 1705446fda4fSPaul Moore * (code 10). The value of the code is dependent on whether the originator 1706446fda4fSPaul Moore * of the ICMP message is acting as a CIPSO host or a CIPSO gateway. The 1707446fda4fSPaul Moore * recipient of the ICMP message MUST be able to handle either value. The 1708446fda4fSPaul Moore * same procedure is performed if a CIPSO [option] can not be added to an 1709446fda4fSPaul Moore * IP packet because it is too large to fit in the IP options area." 1710446fda4fSPaul Moore * 1711446fda4fSPaul Moore * "If the error is triggered by receipt of an ICMP message, the message is 1712446fda4fSPaul Moore * discarded and no response is permitted (consistent with general ICMP 1713446fda4fSPaul Moore * processing rules)." 1714446fda4fSPaul Moore * 1715446fda4fSPaul Moore */ 1716446fda4fSPaul Moore void cipso_v4_error(struct sk_buff *skb, int error, u32 gateway) 1717446fda4fSPaul Moore { 17183da1ed7aSNazarov Sergey unsigned char optbuf[sizeof(struct ip_options) + 40]; 17193da1ed7aSNazarov Sergey struct ip_options *opt = (struct ip_options *)optbuf; 17203e72dfdfSMatteo Croce int res; 17213da1ed7aSNazarov Sergey 1722eddc9ec5SArnaldo Carvalho de Melo if (ip_hdr(skb)->protocol == IPPROTO_ICMP || error != -EACCES) 1723446fda4fSPaul Moore return; 1724446fda4fSPaul Moore 17253da1ed7aSNazarov Sergey /* 17263da1ed7aSNazarov Sergey * We might be called above the IP layer, 17273da1ed7aSNazarov Sergey * so we can not use icmp_send and IPCB here. 17283da1ed7aSNazarov Sergey */ 17293da1ed7aSNazarov Sergey 17303da1ed7aSNazarov Sergey memset(opt, 0, sizeof(struct ip_options)); 17313da1ed7aSNazarov Sergey opt->optlen = ip_hdr(skb)->ihl*4 - sizeof(struct iphdr); 17323e72dfdfSMatteo Croce rcu_read_lock(); 17333e72dfdfSMatteo Croce res = __ip_options_compile(dev_net(skb->dev), opt, skb, NULL); 17343e72dfdfSMatteo Croce rcu_read_unlock(); 17353e72dfdfSMatteo Croce 17363e72dfdfSMatteo Croce if (res) 17373da1ed7aSNazarov Sergey return; 17383da1ed7aSNazarov Sergey 1739446fda4fSPaul Moore if (gateway) 17403da1ed7aSNazarov Sergey __icmp_send(skb, ICMP_DEST_UNREACH, ICMP_NET_ANO, 0, opt); 1741446fda4fSPaul Moore else 17423da1ed7aSNazarov Sergey __icmp_send(skb, ICMP_DEST_UNREACH, ICMP_HOST_ANO, 0, opt); 1743446fda4fSPaul Moore } 1744446fda4fSPaul Moore 1745446fda4fSPaul Moore /** 1746948bf85cSPaul Moore * cipso_v4_genopt - Generate a CIPSO option 1747948bf85cSPaul Moore * @buf: the option buffer 1748948bf85cSPaul Moore * @buf_len: the size of opt_buf 1749446fda4fSPaul Moore * @doi_def: the CIPSO DOI to use 1750948bf85cSPaul Moore * @secattr: the security attributes 1751446fda4fSPaul Moore * 1752446fda4fSPaul Moore * Description: 1753948bf85cSPaul Moore * Generate a CIPSO option using the DOI definition and security attributes 1754948bf85cSPaul Moore * passed to the function. Returns the length of the option on success and 1755948bf85cSPaul Moore * negative values on failure. 1756446fda4fSPaul Moore * 1757446fda4fSPaul Moore */ 1758948bf85cSPaul Moore static int cipso_v4_genopt(unsigned char *buf, u32 buf_len, 1759446fda4fSPaul Moore const struct cipso_v4_doi *doi_def, 1760446fda4fSPaul Moore const struct netlbl_lsm_secattr *secattr) 1761446fda4fSPaul Moore { 1762948bf85cSPaul Moore int ret_val; 1763446fda4fSPaul Moore u32 iter; 1764446fda4fSPaul Moore 1765948bf85cSPaul Moore if (buf_len <= CIPSO_V4_HDR_LEN) 1766948bf85cSPaul Moore return -ENOSPC; 176791b1ed0aSPaul Moore 1768446fda4fSPaul Moore /* XXX - This code assumes only one tag per CIPSO option which isn't 1769446fda4fSPaul Moore * really a good assumption to make but since we only support the MAC 1770446fda4fSPaul Moore * tags right now it is a safe assumption. */ 1771446fda4fSPaul Moore iter = 0; 1772446fda4fSPaul Moore do { 177391b1ed0aSPaul Moore memset(buf, 0, buf_len); 1774446fda4fSPaul Moore switch (doi_def->tags[iter]) { 1775446fda4fSPaul Moore case CIPSO_V4_TAG_RBITMAP: 1776446fda4fSPaul Moore ret_val = cipso_v4_gentag_rbm(doi_def, 1777446fda4fSPaul Moore secattr, 177891b1ed0aSPaul Moore &buf[CIPSO_V4_HDR_LEN], 177991b1ed0aSPaul Moore buf_len - CIPSO_V4_HDR_LEN); 1780446fda4fSPaul Moore break; 1781654bbc2aSPaul Moore case CIPSO_V4_TAG_ENUM: 1782654bbc2aSPaul Moore ret_val = cipso_v4_gentag_enum(doi_def, 1783654bbc2aSPaul Moore secattr, 1784654bbc2aSPaul Moore &buf[CIPSO_V4_HDR_LEN], 1785654bbc2aSPaul Moore buf_len - CIPSO_V4_HDR_LEN); 1786654bbc2aSPaul Moore break; 1787484b3669SPaul Moore case CIPSO_V4_TAG_RANGE: 1788484b3669SPaul Moore ret_val = cipso_v4_gentag_rng(doi_def, 1789484b3669SPaul Moore secattr, 1790484b3669SPaul Moore &buf[CIPSO_V4_HDR_LEN], 1791484b3669SPaul Moore buf_len - CIPSO_V4_HDR_LEN); 1792484b3669SPaul Moore break; 179315c45f7bSPaul Moore case CIPSO_V4_TAG_LOCAL: 179415c45f7bSPaul Moore ret_val = cipso_v4_gentag_loc(doi_def, 179515c45f7bSPaul Moore secattr, 179615c45f7bSPaul Moore &buf[CIPSO_V4_HDR_LEN], 179715c45f7bSPaul Moore buf_len - CIPSO_V4_HDR_LEN); 179815c45f7bSPaul Moore break; 1799446fda4fSPaul Moore default: 1800948bf85cSPaul Moore return -EPERM; 1801446fda4fSPaul Moore } 1802446fda4fSPaul Moore 1803446fda4fSPaul Moore iter++; 180491b1ed0aSPaul Moore } while (ret_val < 0 && 1805446fda4fSPaul Moore iter < CIPSO_V4_TAG_MAXCNT && 1806446fda4fSPaul Moore doi_def->tags[iter] != CIPSO_V4_TAG_INVALID); 180791b1ed0aSPaul Moore if (ret_val < 0) 1808948bf85cSPaul Moore return ret_val; 180991b1ed0aSPaul Moore cipso_v4_gentag_hdr(doi_def, buf, ret_val); 1810948bf85cSPaul Moore return CIPSO_V4_HDR_LEN + ret_val; 1811948bf85cSPaul Moore } 1812948bf85cSPaul Moore 1813948bf85cSPaul Moore /** 1814948bf85cSPaul Moore * cipso_v4_sock_setattr - Add a CIPSO option to a socket 1815948bf85cSPaul Moore * @sk: the socket 1816948bf85cSPaul Moore * @doi_def: the CIPSO DOI to use 1817948bf85cSPaul Moore * @secattr: the specific security attributes of the socket 1818948bf85cSPaul Moore * 1819948bf85cSPaul Moore * Description: 1820948bf85cSPaul Moore * Set the CIPSO option on the given socket using the DOI definition and 1821948bf85cSPaul Moore * security attributes passed to the function. This function requires 1822948bf85cSPaul Moore * exclusive access to @sk, which means it either needs to be in the 1823948bf85cSPaul Moore * process of being created or locked. Returns zero on success and negative 1824948bf85cSPaul Moore * values on failure. 1825948bf85cSPaul Moore * 1826948bf85cSPaul Moore */ 1827948bf85cSPaul Moore int cipso_v4_sock_setattr(struct sock *sk, 1828948bf85cSPaul Moore const struct cipso_v4_doi *doi_def, 1829948bf85cSPaul Moore const struct netlbl_lsm_secattr *secattr) 1830948bf85cSPaul Moore { 1831948bf85cSPaul Moore int ret_val = -EPERM; 1832948bf85cSPaul Moore unsigned char *buf = NULL; 1833948bf85cSPaul Moore u32 buf_len; 1834948bf85cSPaul Moore u32 opt_len; 1835f6d8bd05SEric Dumazet struct ip_options_rcu *old, *opt = NULL; 1836948bf85cSPaul Moore struct inet_sock *sk_inet; 1837948bf85cSPaul Moore struct inet_connection_sock *sk_conn; 1838948bf85cSPaul Moore 1839948bf85cSPaul Moore /* In the case of sock_create_lite(), the sock->sk field is not 1840948bf85cSPaul Moore * defined yet but it is not a problem as the only users of these 1841948bf85cSPaul Moore * "lite" PF_INET sockets are functions which do an accept() call 1842948bf85cSPaul Moore * afterwards so we will label the socket as part of the accept(). */ 184351456b29SIan Morris if (!sk) 1844948bf85cSPaul Moore return 0; 1845948bf85cSPaul Moore 1846948bf85cSPaul Moore /* We allocate the maximum CIPSO option size here so we are probably 1847948bf85cSPaul Moore * being a little wasteful, but it makes our life _much_ easier later 1848948bf85cSPaul Moore * on and after all we are only talking about 40 bytes. */ 1849948bf85cSPaul Moore buf_len = CIPSO_V4_OPT_LEN_MAX; 1850948bf85cSPaul Moore buf = kmalloc(buf_len, GFP_ATOMIC); 185151456b29SIan Morris if (!buf) { 1852948bf85cSPaul Moore ret_val = -ENOMEM; 1853948bf85cSPaul Moore goto socket_setattr_failure; 1854948bf85cSPaul Moore } 1855948bf85cSPaul Moore 1856948bf85cSPaul Moore ret_val = cipso_v4_genopt(buf, buf_len, doi_def, secattr); 1857948bf85cSPaul Moore if (ret_val < 0) 1858948bf85cSPaul Moore goto socket_setattr_failure; 1859948bf85cSPaul Moore buf_len = ret_val; 1860446fda4fSPaul Moore 1861446fda4fSPaul Moore /* We can't use ip_options_get() directly because it makes a call to 1862446fda4fSPaul Moore * ip_options_get_alloc() which allocates memory with GFP_KERNEL and 1863f8687afeSPaul Moore * we won't always have CAP_NET_RAW even though we _always_ want to 1864f8687afeSPaul Moore * set the IPOPT_CIPSO option. */ 1865446fda4fSPaul Moore opt_len = (buf_len + 3) & ~3; 1866446fda4fSPaul Moore opt = kzalloc(sizeof(*opt) + opt_len, GFP_ATOMIC); 186751456b29SIan Morris if (!opt) { 1868446fda4fSPaul Moore ret_val = -ENOMEM; 1869446fda4fSPaul Moore goto socket_setattr_failure; 1870446fda4fSPaul Moore } 1871f6d8bd05SEric Dumazet memcpy(opt->opt.__data, buf, buf_len); 1872f6d8bd05SEric Dumazet opt->opt.optlen = opt_len; 1873f6d8bd05SEric Dumazet opt->opt.cipso = sizeof(struct iphdr); 1874446fda4fSPaul Moore kfree(buf); 1875446fda4fSPaul Moore buf = NULL; 1876446fda4fSPaul Moore 1877446fda4fSPaul Moore sk_inet = inet_sk(sk); 1878f6d8bd05SEric Dumazet 18791e1d04e6SHannes Frederic Sowa old = rcu_dereference_protected(sk_inet->inet_opt, 18801e1d04e6SHannes Frederic Sowa lockdep_sock_is_held(sk)); 1881b1c0356aSEric Dumazet if (inet_test_bit(IS_ICSK, sk)) { 1882446fda4fSPaul Moore sk_conn = inet_csk(sk); 1883f6d8bd05SEric Dumazet if (old) 1884f6d8bd05SEric Dumazet sk_conn->icsk_ext_hdr_len -= old->opt.optlen; 1885f6d8bd05SEric Dumazet sk_conn->icsk_ext_hdr_len += opt->opt.optlen; 1886446fda4fSPaul Moore sk_conn->icsk_sync_mss(sk, sk_conn->icsk_pmtu_cookie); 1887446fda4fSPaul Moore } 1888f6d8bd05SEric Dumazet rcu_assign_pointer(sk_inet->inet_opt, opt); 1889f6d8bd05SEric Dumazet if (old) 18904f9c8c1bSPaul E. McKenney kfree_rcu(old, rcu); 1891446fda4fSPaul Moore 1892446fda4fSPaul Moore return 0; 1893446fda4fSPaul Moore 1894446fda4fSPaul Moore socket_setattr_failure: 1895446fda4fSPaul Moore kfree(buf); 1896446fda4fSPaul Moore kfree(opt); 1897446fda4fSPaul Moore return ret_val; 1898446fda4fSPaul Moore } 1899446fda4fSPaul Moore 1900446fda4fSPaul Moore /** 1901389fb800SPaul Moore * cipso_v4_req_setattr - Add a CIPSO option to a connection request socket 1902389fb800SPaul Moore * @req: the connection request socket 1903389fb800SPaul Moore * @doi_def: the CIPSO DOI to use 1904389fb800SPaul Moore * @secattr: the specific security attributes of the socket 1905014ab19aSPaul Moore * 1906014ab19aSPaul Moore * Description: 1907389fb800SPaul Moore * Set the CIPSO option on the given socket using the DOI definition and 1908389fb800SPaul Moore * security attributes passed to the function. Returns zero on success and 1909389fb800SPaul Moore * negative values on failure. 1910014ab19aSPaul Moore * 1911014ab19aSPaul Moore */ 1912389fb800SPaul Moore int cipso_v4_req_setattr(struct request_sock *req, 1913389fb800SPaul Moore const struct cipso_v4_doi *doi_def, 1914389fb800SPaul Moore const struct netlbl_lsm_secattr *secattr) 1915014ab19aSPaul Moore { 1916389fb800SPaul Moore int ret_val = -EPERM; 1917389fb800SPaul Moore unsigned char *buf = NULL; 1918389fb800SPaul Moore u32 buf_len; 1919389fb800SPaul Moore u32 opt_len; 1920f6d8bd05SEric Dumazet struct ip_options_rcu *opt = NULL; 1921389fb800SPaul Moore struct inet_request_sock *req_inet; 1922014ab19aSPaul Moore 1923389fb800SPaul Moore /* We allocate the maximum CIPSO option size here so we are probably 1924389fb800SPaul Moore * being a little wasteful, but it makes our life _much_ easier later 1925389fb800SPaul Moore * on and after all we are only talking about 40 bytes. */ 1926389fb800SPaul Moore buf_len = CIPSO_V4_OPT_LEN_MAX; 1927389fb800SPaul Moore buf = kmalloc(buf_len, GFP_ATOMIC); 192851456b29SIan Morris if (!buf) { 1929389fb800SPaul Moore ret_val = -ENOMEM; 1930389fb800SPaul Moore goto req_setattr_failure; 1931389fb800SPaul Moore } 1932389fb800SPaul Moore 1933389fb800SPaul Moore ret_val = cipso_v4_genopt(buf, buf_len, doi_def, secattr); 1934389fb800SPaul Moore if (ret_val < 0) 1935389fb800SPaul Moore goto req_setattr_failure; 1936389fb800SPaul Moore buf_len = ret_val; 1937389fb800SPaul Moore 1938389fb800SPaul Moore /* We can't use ip_options_get() directly because it makes a call to 1939389fb800SPaul Moore * ip_options_get_alloc() which allocates memory with GFP_KERNEL and 1940389fb800SPaul Moore * we won't always have CAP_NET_RAW even though we _always_ want to 1941389fb800SPaul Moore * set the IPOPT_CIPSO option. */ 1942389fb800SPaul Moore opt_len = (buf_len + 3) & ~3; 1943389fb800SPaul Moore opt = kzalloc(sizeof(*opt) + opt_len, GFP_ATOMIC); 194451456b29SIan Morris if (!opt) { 1945389fb800SPaul Moore ret_val = -ENOMEM; 1946389fb800SPaul Moore goto req_setattr_failure; 1947389fb800SPaul Moore } 1948f6d8bd05SEric Dumazet memcpy(opt->opt.__data, buf, buf_len); 1949f6d8bd05SEric Dumazet opt->opt.optlen = opt_len; 1950f6d8bd05SEric Dumazet opt->opt.cipso = sizeof(struct iphdr); 1951389fb800SPaul Moore kfree(buf); 1952389fb800SPaul Moore buf = NULL; 1953389fb800SPaul Moore 1954389fb800SPaul Moore req_inet = inet_rsk(req); 1955c92e8c02SEric Dumazet opt = xchg((__force struct ip_options_rcu **)&req_inet->ireq_opt, opt); 1956f6d8bd05SEric Dumazet if (opt) 19574f9c8c1bSPaul E. McKenney kfree_rcu(opt, rcu); 1958389fb800SPaul Moore 1959389fb800SPaul Moore return 0; 1960389fb800SPaul Moore 1961389fb800SPaul Moore req_setattr_failure: 1962389fb800SPaul Moore kfree(buf); 1963389fb800SPaul Moore kfree(opt); 1964389fb800SPaul Moore return ret_val; 1965389fb800SPaul Moore } 1966389fb800SPaul Moore 1967389fb800SPaul Moore /** 1968389fb800SPaul Moore * cipso_v4_delopt - Delete the CIPSO option from a set of IP options 1969389fb800SPaul Moore * @opt_ptr: IP option pointer 1970389fb800SPaul Moore * 1971389fb800SPaul Moore * Description: 1972389fb800SPaul Moore * Deletes the CIPSO IP option from a set of IP options and makes the necessary 1973389fb800SPaul Moore * adjustments to the IP option structure. Returns zero on success, negative 1974389fb800SPaul Moore * values on failure. 1975389fb800SPaul Moore * 1976389fb800SPaul Moore */ 1977c92e8c02SEric Dumazet static int cipso_v4_delopt(struct ip_options_rcu __rcu **opt_ptr) 1978389fb800SPaul Moore { 1979c92e8c02SEric Dumazet struct ip_options_rcu *opt = rcu_dereference_protected(*opt_ptr, 1); 1980389fb800SPaul Moore int hdr_delta = 0; 1981014ab19aSPaul Moore 1982c92e8c02SEric Dumazet if (!opt || opt->opt.cipso == 0) 1983c92e8c02SEric Dumazet return 0; 1984f6d8bd05SEric Dumazet if (opt->opt.srr || opt->opt.rr || opt->opt.ts || opt->opt.router_alert) { 1985014ab19aSPaul Moore u8 cipso_len; 1986014ab19aSPaul Moore u8 cipso_off; 1987014ab19aSPaul Moore unsigned char *cipso_ptr; 1988014ab19aSPaul Moore int iter; 1989014ab19aSPaul Moore int optlen_new; 1990014ab19aSPaul Moore 1991f6d8bd05SEric Dumazet cipso_off = opt->opt.cipso - sizeof(struct iphdr); 1992f6d8bd05SEric Dumazet cipso_ptr = &opt->opt.__data[cipso_off]; 1993014ab19aSPaul Moore cipso_len = cipso_ptr[1]; 1994014ab19aSPaul Moore 1995f6d8bd05SEric Dumazet if (opt->opt.srr > opt->opt.cipso) 1996f6d8bd05SEric Dumazet opt->opt.srr -= cipso_len; 1997f6d8bd05SEric Dumazet if (opt->opt.rr > opt->opt.cipso) 1998f6d8bd05SEric Dumazet opt->opt.rr -= cipso_len; 1999f6d8bd05SEric Dumazet if (opt->opt.ts > opt->opt.cipso) 2000f6d8bd05SEric Dumazet opt->opt.ts -= cipso_len; 2001f6d8bd05SEric Dumazet if (opt->opt.router_alert > opt->opt.cipso) 2002f6d8bd05SEric Dumazet opt->opt.router_alert -= cipso_len; 2003f6d8bd05SEric Dumazet opt->opt.cipso = 0; 2004014ab19aSPaul Moore 2005014ab19aSPaul Moore memmove(cipso_ptr, cipso_ptr + cipso_len, 2006f6d8bd05SEric Dumazet opt->opt.optlen - cipso_off - cipso_len); 2007014ab19aSPaul Moore 2008014ab19aSPaul Moore /* determining the new total option length is tricky because of 2009014ab19aSPaul Moore * the padding necessary, the only thing i can think to do at 2010014ab19aSPaul Moore * this point is walk the options one-by-one, skipping the 2011014ab19aSPaul Moore * padding at the end to determine the actual option size and 2012014ab19aSPaul Moore * from there we can determine the new total option length */ 2013014ab19aSPaul Moore iter = 0; 2014014ab19aSPaul Moore optlen_new = 0; 2015f6d8bd05SEric Dumazet while (iter < opt->opt.optlen) 2016f6d8bd05SEric Dumazet if (opt->opt.__data[iter] != IPOPT_NOP) { 2017f6d8bd05SEric Dumazet iter += opt->opt.__data[iter + 1]; 2018014ab19aSPaul Moore optlen_new = iter; 2019014ab19aSPaul Moore } else 2020014ab19aSPaul Moore iter++; 2021f6d8bd05SEric Dumazet hdr_delta = opt->opt.optlen; 2022f6d8bd05SEric Dumazet opt->opt.optlen = (optlen_new + 3) & ~3; 2023f6d8bd05SEric Dumazet hdr_delta -= opt->opt.optlen; 2024014ab19aSPaul Moore } else { 2025014ab19aSPaul Moore /* only the cipso option was present on the socket so we can 2026014ab19aSPaul Moore * remove the entire option struct */ 2027389fb800SPaul Moore *opt_ptr = NULL; 2028f6d8bd05SEric Dumazet hdr_delta = opt->opt.optlen; 20294f9c8c1bSPaul E. McKenney kfree_rcu(opt, rcu); 2030014ab19aSPaul Moore } 2031014ab19aSPaul Moore 2032389fb800SPaul Moore return hdr_delta; 2033389fb800SPaul Moore } 2034389fb800SPaul Moore 2035389fb800SPaul Moore /** 2036389fb800SPaul Moore * cipso_v4_sock_delattr - Delete the CIPSO option from a socket 2037389fb800SPaul Moore * @sk: the socket 2038389fb800SPaul Moore * 2039389fb800SPaul Moore * Description: 2040389fb800SPaul Moore * Removes the CIPSO option from a socket, if present. 2041389fb800SPaul Moore * 2042389fb800SPaul Moore */ 2043389fb800SPaul Moore void cipso_v4_sock_delattr(struct sock *sk) 2044389fb800SPaul Moore { 2045389fb800SPaul Moore struct inet_sock *sk_inet; 2046c92e8c02SEric Dumazet int hdr_delta; 2047389fb800SPaul Moore 2048389fb800SPaul Moore sk_inet = inet_sk(sk); 2049389fb800SPaul Moore 2050f6d8bd05SEric Dumazet hdr_delta = cipso_v4_delopt(&sk_inet->inet_opt); 2051b1c0356aSEric Dumazet if (inet_test_bit(IS_ICSK, sk) && hdr_delta > 0) { 2052014ab19aSPaul Moore struct inet_connection_sock *sk_conn = inet_csk(sk); 2053014ab19aSPaul Moore sk_conn->icsk_ext_hdr_len -= hdr_delta; 2054014ab19aSPaul Moore sk_conn->icsk_sync_mss(sk, sk_conn->icsk_pmtu_cookie); 2055014ab19aSPaul Moore } 2056014ab19aSPaul Moore } 2057014ab19aSPaul Moore 2058014ab19aSPaul Moore /** 2059389fb800SPaul Moore * cipso_v4_req_delattr - Delete the CIPSO option from a request socket 20603628e3cbSAndrew Lunn * @req: the request socket 2061389fb800SPaul Moore * 2062389fb800SPaul Moore * Description: 2063389fb800SPaul Moore * Removes the CIPSO option from a request socket, if present. 2064389fb800SPaul Moore * 2065389fb800SPaul Moore */ 2066389fb800SPaul Moore void cipso_v4_req_delattr(struct request_sock *req) 2067389fb800SPaul Moore { 2068c92e8c02SEric Dumazet cipso_v4_delopt(&inet_rsk(req)->ireq_opt); 2069389fb800SPaul Moore } 2070389fb800SPaul Moore 2071389fb800SPaul Moore /** 207263d804eaSPaul Moore * cipso_v4_getattr - Helper function for the cipso_v4_*_getattr functions 207363d804eaSPaul Moore * @cipso: the CIPSO v4 option 207463d804eaSPaul Moore * @secattr: the security attributes 207563d804eaSPaul Moore * 207663d804eaSPaul Moore * Description: 207763d804eaSPaul Moore * Inspect @cipso and return the security attributes in @secattr. Returns zero 207863d804eaSPaul Moore * on success and negative values on failure. 207963d804eaSPaul Moore * 208063d804eaSPaul Moore */ 208104f81f01SPaul Moore int cipso_v4_getattr(const unsigned char *cipso, 208263d804eaSPaul Moore struct netlbl_lsm_secattr *secattr) 208363d804eaSPaul Moore { 208463d804eaSPaul Moore int ret_val = -ENOMSG; 208563d804eaSPaul Moore u32 doi; 208663d804eaSPaul Moore struct cipso_v4_doi *doi_def; 208763d804eaSPaul Moore 208863d804eaSPaul Moore if (cipso_v4_cache_check(cipso, cipso[1], secattr) == 0) 208963d804eaSPaul Moore return 0; 209063d804eaSPaul Moore 2091d3e2ce3bSHarvey Harrison doi = get_unaligned_be32(&cipso[2]); 209263d804eaSPaul Moore rcu_read_lock(); 209363d804eaSPaul Moore doi_def = cipso_v4_doi_search(doi); 209451456b29SIan Morris if (!doi_def) 209563d804eaSPaul Moore goto getattr_return; 209663d804eaSPaul Moore /* XXX - This code assumes only one tag per CIPSO option which isn't 209763d804eaSPaul Moore * really a good assumption to make but since we only support the MAC 209863d804eaSPaul Moore * tags right now it is a safe assumption. */ 209963d804eaSPaul Moore switch (cipso[6]) { 210063d804eaSPaul Moore case CIPSO_V4_TAG_RBITMAP: 210163d804eaSPaul Moore ret_val = cipso_v4_parsetag_rbm(doi_def, &cipso[6], secattr); 210263d804eaSPaul Moore break; 210363d804eaSPaul Moore case CIPSO_V4_TAG_ENUM: 210463d804eaSPaul Moore ret_val = cipso_v4_parsetag_enum(doi_def, &cipso[6], secattr); 210563d804eaSPaul Moore break; 210663d804eaSPaul Moore case CIPSO_V4_TAG_RANGE: 210763d804eaSPaul Moore ret_val = cipso_v4_parsetag_rng(doi_def, &cipso[6], secattr); 210863d804eaSPaul Moore break; 210915c45f7bSPaul Moore case CIPSO_V4_TAG_LOCAL: 211015c45f7bSPaul Moore ret_val = cipso_v4_parsetag_loc(doi_def, &cipso[6], secattr); 211115c45f7bSPaul Moore break; 211263d804eaSPaul Moore } 211316efd454SPaul Moore if (ret_val == 0) 211416efd454SPaul Moore secattr->type = NETLBL_NLTYPE_CIPSOV4; 211563d804eaSPaul Moore 211663d804eaSPaul Moore getattr_return: 211763d804eaSPaul Moore rcu_read_unlock(); 211863d804eaSPaul Moore return ret_val; 211963d804eaSPaul Moore } 212063d804eaSPaul Moore 212163d804eaSPaul Moore /** 212214a72f53SPaul Moore * cipso_v4_sock_getattr - Get the security attributes from a sock 212314a72f53SPaul Moore * @sk: the sock 212414a72f53SPaul Moore * @secattr: the security attributes 212514a72f53SPaul Moore * 212614a72f53SPaul Moore * Description: 212714a72f53SPaul Moore * Query @sk to see if there is a CIPSO option attached to the sock and if 212814a72f53SPaul Moore * there is return the CIPSO security attributes in @secattr. This function 212914a72f53SPaul Moore * requires that @sk be locked, or privately held, but it does not do any 213014a72f53SPaul Moore * locking itself. Returns zero on success and negative values on failure. 213114a72f53SPaul Moore * 213214a72f53SPaul Moore */ 213314a72f53SPaul Moore int cipso_v4_sock_getattr(struct sock *sk, struct netlbl_lsm_secattr *secattr) 213414a72f53SPaul Moore { 2135f6d8bd05SEric Dumazet struct ip_options_rcu *opt; 2136f6d8bd05SEric Dumazet int res = -ENOMSG; 213714a72f53SPaul Moore 2138f6d8bd05SEric Dumazet rcu_read_lock(); 2139f6d8bd05SEric Dumazet opt = rcu_dereference(inet_sk(sk)->inet_opt); 2140f6d8bd05SEric Dumazet if (opt && opt->opt.cipso) 2141f6d8bd05SEric Dumazet res = cipso_v4_getattr(opt->opt.__data + 2142f6d8bd05SEric Dumazet opt->opt.cipso - 2143f6d8bd05SEric Dumazet sizeof(struct iphdr), 214414a72f53SPaul Moore secattr); 2145f6d8bd05SEric Dumazet rcu_read_unlock(); 2146f6d8bd05SEric Dumazet return res; 214714a72f53SPaul Moore } 214814a72f53SPaul Moore 214914a72f53SPaul Moore /** 2150948bf85cSPaul Moore * cipso_v4_skbuff_setattr - Set the CIPSO option on a packet 2151948bf85cSPaul Moore * @skb: the packet 21523628e3cbSAndrew Lunn * @doi_def: the DOI structure 2153948bf85cSPaul Moore * @secattr: the security attributes 2154948bf85cSPaul Moore * 2155948bf85cSPaul Moore * Description: 2156948bf85cSPaul Moore * Set the CIPSO option on the given packet based on the security attributes. 2157948bf85cSPaul Moore * Returns a pointer to the IP header on success and NULL on failure. 2158948bf85cSPaul Moore * 2159948bf85cSPaul Moore */ 2160948bf85cSPaul Moore int cipso_v4_skbuff_setattr(struct sk_buff *skb, 2161948bf85cSPaul Moore const struct cipso_v4_doi *doi_def, 2162948bf85cSPaul Moore const struct netlbl_lsm_secattr *secattr) 2163948bf85cSPaul Moore { 2164948bf85cSPaul Moore int ret_val; 2165948bf85cSPaul Moore struct iphdr *iph; 2166948bf85cSPaul Moore struct ip_options *opt = &IPCB(skb)->opt; 2167948bf85cSPaul Moore unsigned char buf[CIPSO_V4_OPT_LEN_MAX]; 2168948bf85cSPaul Moore u32 buf_len = CIPSO_V4_OPT_LEN_MAX; 2169948bf85cSPaul Moore u32 opt_len; 2170948bf85cSPaul Moore int len_delta; 2171948bf85cSPaul Moore 217200af5c69Sroel kluin ret_val = cipso_v4_genopt(buf, buf_len, doi_def, secattr); 217300af5c69Sroel kluin if (ret_val < 0) 217400af5c69Sroel kluin return ret_val; 217500af5c69Sroel kluin buf_len = ret_val; 2176948bf85cSPaul Moore opt_len = (buf_len + 3) & ~3; 2177948bf85cSPaul Moore 2178948bf85cSPaul Moore /* we overwrite any existing options to ensure that we have enough 2179948bf85cSPaul Moore * room for the CIPSO option, the reason is that we _need_ to guarantee 2180948bf85cSPaul Moore * that the security label is applied to the packet - we do the same 2181948bf85cSPaul Moore * thing when using the socket options and it hasn't caused a problem, 2182948bf85cSPaul Moore * if we need to we can always revisit this choice later */ 2183948bf85cSPaul Moore 2184948bf85cSPaul Moore len_delta = opt_len - opt->optlen; 2185948bf85cSPaul Moore /* if we don't ensure enough headroom we could panic on the skb_push() 2186948bf85cSPaul Moore * call below so make sure we have enough, we are also "mangling" the 2187948bf85cSPaul Moore * packet so we should probably do a copy-on-write call anyway */ 2188948bf85cSPaul Moore ret_val = skb_cow(skb, skb_headroom(skb) + len_delta); 2189948bf85cSPaul Moore if (ret_val < 0) 2190948bf85cSPaul Moore return ret_val; 2191948bf85cSPaul Moore 2192948bf85cSPaul Moore if (len_delta > 0) { 2193948bf85cSPaul Moore /* we assume that the header + opt->optlen have already been 2194948bf85cSPaul Moore * "pushed" in ip_options_build() or similar */ 2195948bf85cSPaul Moore iph = ip_hdr(skb); 2196948bf85cSPaul Moore skb_push(skb, len_delta); 2197948bf85cSPaul Moore memmove((char *)iph - len_delta, iph, iph->ihl << 2); 2198948bf85cSPaul Moore skb_reset_network_header(skb); 2199948bf85cSPaul Moore iph = ip_hdr(skb); 2200948bf85cSPaul Moore } else if (len_delta < 0) { 2201948bf85cSPaul Moore iph = ip_hdr(skb); 2202948bf85cSPaul Moore memset(iph + 1, IPOPT_NOP, opt->optlen); 2203948bf85cSPaul Moore } else 2204948bf85cSPaul Moore iph = ip_hdr(skb); 2205948bf85cSPaul Moore 2206948bf85cSPaul Moore if (opt->optlen > 0) 2207948bf85cSPaul Moore memset(opt, 0, sizeof(*opt)); 2208948bf85cSPaul Moore opt->optlen = opt_len; 2209948bf85cSPaul Moore opt->cipso = sizeof(struct iphdr); 2210948bf85cSPaul Moore opt->is_changed = 1; 2211948bf85cSPaul Moore 2212948bf85cSPaul Moore /* we have to do the following because we are being called from a 2213948bf85cSPaul Moore * netfilter hook which means the packet already has had the header 2214948bf85cSPaul Moore * fields populated and the checksum calculated - yes this means we 2215948bf85cSPaul Moore * are doing more work than needed but we do it to keep the core 2216948bf85cSPaul Moore * stack clean and tidy */ 2217948bf85cSPaul Moore memcpy(iph + 1, buf, buf_len); 2218948bf85cSPaul Moore if (opt_len > buf_len) 2219948bf85cSPaul Moore memset((char *)(iph + 1) + buf_len, 0, opt_len - buf_len); 2220948bf85cSPaul Moore if (len_delta != 0) { 2221948bf85cSPaul Moore iph->ihl = 5 + (opt_len >> 2); 22227eb072beSXin Long iph_set_totlen(iph, skb->len); 2223948bf85cSPaul Moore } 2224948bf85cSPaul Moore ip_send_check(iph); 2225948bf85cSPaul Moore 2226948bf85cSPaul Moore return 0; 2227948bf85cSPaul Moore } 2228948bf85cSPaul Moore 2229948bf85cSPaul Moore /** 2230948bf85cSPaul Moore * cipso_v4_skbuff_delattr - Delete any CIPSO options from a packet 2231948bf85cSPaul Moore * @skb: the packet 2232948bf85cSPaul Moore * 2233948bf85cSPaul Moore * Description: 2234948bf85cSPaul Moore * Removes any and all CIPSO options from the given packet. Returns zero on 2235948bf85cSPaul Moore * success, negative values on failure. 2236948bf85cSPaul Moore * 2237948bf85cSPaul Moore */ 2238948bf85cSPaul Moore int cipso_v4_skbuff_delattr(struct sk_buff *skb) 2239948bf85cSPaul Moore { 2240948bf85cSPaul Moore int ret_val; 2241948bf85cSPaul Moore struct iphdr *iph; 2242948bf85cSPaul Moore struct ip_options *opt = &IPCB(skb)->opt; 2243948bf85cSPaul Moore unsigned char *cipso_ptr; 2244948bf85cSPaul Moore 2245948bf85cSPaul Moore if (opt->cipso == 0) 2246948bf85cSPaul Moore return 0; 2247948bf85cSPaul Moore 2248948bf85cSPaul Moore /* since we are changing the packet we should make a copy */ 2249948bf85cSPaul Moore ret_val = skb_cow(skb, skb_headroom(skb)); 2250948bf85cSPaul Moore if (ret_val < 0) 2251948bf85cSPaul Moore return ret_val; 2252948bf85cSPaul Moore 2253948bf85cSPaul Moore /* the easiest thing to do is just replace the cipso option with noop 2254948bf85cSPaul Moore * options since we don't change the size of the packet, although we 2255948bf85cSPaul Moore * still need to recalculate the checksum */ 2256948bf85cSPaul Moore 2257948bf85cSPaul Moore iph = ip_hdr(skb); 2258948bf85cSPaul Moore cipso_ptr = (unsigned char *)iph + opt->cipso; 2259948bf85cSPaul Moore memset(cipso_ptr, IPOPT_NOOP, cipso_ptr[1]); 2260948bf85cSPaul Moore opt->cipso = 0; 2261948bf85cSPaul Moore opt->is_changed = 1; 2262948bf85cSPaul Moore 2263948bf85cSPaul Moore ip_send_check(iph); 2264948bf85cSPaul Moore 2265948bf85cSPaul Moore return 0; 2266948bf85cSPaul Moore } 2267948bf85cSPaul Moore 2268446fda4fSPaul Moore /* 2269446fda4fSPaul Moore * Setup Functions 2270446fda4fSPaul Moore */ 2271446fda4fSPaul Moore 2272446fda4fSPaul Moore /** 2273446fda4fSPaul Moore * cipso_v4_init - Initialize the CIPSO module 2274446fda4fSPaul Moore * 2275446fda4fSPaul Moore * Description: 2276446fda4fSPaul Moore * Initialize the CIPSO module and prepare it for use. Returns zero on success 2277446fda4fSPaul Moore * and negative values on failure. 2278446fda4fSPaul Moore * 2279446fda4fSPaul Moore */ 2280446fda4fSPaul Moore static int __init cipso_v4_init(void) 2281446fda4fSPaul Moore { 2282446fda4fSPaul Moore int ret_val; 2283446fda4fSPaul Moore 2284446fda4fSPaul Moore ret_val = cipso_v4_cache_init(); 2285446fda4fSPaul Moore if (ret_val != 0) 2286446fda4fSPaul Moore panic("Failed to initialize the CIPSO/IPv4 cache (%d)\n", 2287446fda4fSPaul Moore ret_val); 2288446fda4fSPaul Moore 2289446fda4fSPaul Moore return 0; 2290446fda4fSPaul Moore } 2291446fda4fSPaul Moore 2292446fda4fSPaul Moore subsys_initcall(cipso_v4_init); 2293