// SPDX-License-Identifier: GPL-2.0-or-later1/*2* CIPSO - Commercial IP Security Option3*4* This is an implementation of the CIPSO 2.2 protocol as specified in5* draft-ietf-cipso-ipsecurity-01.txt with additional tag types as found in6* FIPS-188. While CIPSO never became a full IETF RFC standard many vendors7* have chosen to adopt the protocol and over the years it has become a8* de-facto standard for labeled networking.9*10* The CIPSO draft specification can be found in the kernel's Documentation11* directory as well as the following URL:12* https://tools.ietf.org/id/draft-ietf-cipso-ipsecurity-01.txt13* The FIPS-188 specification can be found at the following URL:14* https://www.itl.nist.gov/fipspubs/fip188.htm15*16* Author: Paul Moore <[email protected]>17*/1819/*20* (c) Copyright Hewlett-Packard Development Company, L.P., 2006, 200821*/2223#include <linux/init.h>24#include <linux/types.h>25#include <linux/rcupdate.h>26#include <linux/list.h>27#include <linux/spinlock.h>28#include <linux/string.h>29#include <linux/jhash.h>30#include <linux/audit.h>31#include <linux/slab.h>32#include <net/ip.h>33#include <net/icmp.h>34#include <net/tcp.h>35#include <net/netlabel.h>36#include <net/cipso_ipv4.h>37#include <linux/atomic.h>38#include <linux/bug.h>39#include <linux/unaligned.h>4041/* List of available DOI definitions */42/* XXX - This currently assumes a minimal number of different DOIs in use,43* if in practice there are a lot of different DOIs this list should44* probably be turned into a hash table or something similar so we45* can do quick lookups. */46static DEFINE_SPINLOCK(cipso_v4_doi_list_lock);47static LIST_HEAD(cipso_v4_doi_list);4849/* Label mapping cache */50int cipso_v4_cache_enabled = 1;51int cipso_v4_cache_bucketsize = 10;52#define CIPSO_V4_CACHE_BUCKETBITS 753#define CIPSO_V4_CACHE_BUCKETS (1 << CIPSO_V4_CACHE_BUCKETBITS)54#define CIPSO_V4_CACHE_REORDERLIMIT 1055struct cipso_v4_map_cache_bkt {56spinlock_t lock;57u32 size;58struct list_head list;59};6061struct cipso_v4_map_cache_entry {62u32 hash;63unsigned char *key;64size_t key_len;6566struct netlbl_lsm_cache *lsm_data;6768u32 activity;69struct list_head list;70};7172static struct cipso_v4_map_cache_bkt *cipso_v4_cache;7374/* Restricted bitmap (tag #1) flags */75int cipso_v4_rbm_optfmt;76int cipso_v4_rbm_strictvalid = 1;7778/*79* Protocol Constants80*/8182/* Maximum size of the CIPSO IP option, derived from the fact that the maximum83* IPv4 header size is 60 bytes and the base IPv4 header is 20 bytes long. */84#define CIPSO_V4_OPT_LEN_MAX 408586/* Length of the base CIPSO option, this includes the option type (1 byte), the87* option length (1 byte), and the DOI (4 bytes). */88#define CIPSO_V4_HDR_LEN 68990/* Base length of the restrictive category bitmap tag (tag #1). */91#define CIPSO_V4_TAG_RBM_BLEN 49293/* Base length of the enumerated category tag (tag #2). */94#define CIPSO_V4_TAG_ENUM_BLEN 49596/* Base length of the ranged categories bitmap tag (tag #5). */97#define CIPSO_V4_TAG_RNG_BLEN 498/* The maximum number of category ranges permitted in the ranged category tag99* (tag #5). You may note that the IETF draft states that the maximum number100* of category ranges is 7, but if the low end of the last category range is101* zero then it is possible to fit 8 category ranges because the zero should102* be omitted. */103#define CIPSO_V4_TAG_RNG_CAT_MAX 8104105/* Base length of the local tag (non-standard tag).106* Tag definition (may change between kernel versions)107*108* 0 8 16 24 32109* +----------+----------+----------+----------+110* | 10000000 | 00000110 | 32-bit secid value |111* +----------+----------+----------+----------+112* | in (host byte order)|113* +----------+----------+114*115*/116#define CIPSO_V4_TAG_LOC_BLEN 6117118/*119* Helper Functions120*/121122/**123* cipso_v4_cache_entry_free - Frees a cache entry124* @entry: the entry to free125*126* Description:127* This function frees the memory associated with a cache entry including the128* LSM cache data if there are no longer any users, i.e. reference count == 0.129*130*/131static void cipso_v4_cache_entry_free(struct cipso_v4_map_cache_entry *entry)132{133if (entry->lsm_data)134netlbl_secattr_cache_free(entry->lsm_data);135kfree(entry->key);136kfree(entry);137}138139/**140* cipso_v4_map_cache_hash - Hashing function for the CIPSO cache141* @key: the hash key142* @key_len: the length of the key in bytes143*144* Description:145* The CIPSO tag hashing function. Returns a 32-bit hash value.146*147*/148static u32 cipso_v4_map_cache_hash(const unsigned char *key, u32 key_len)149{150return jhash(key, key_len, 0);151}152153/*154* Label Mapping Cache Functions155*/156157/**158* cipso_v4_cache_init - Initialize the CIPSO cache159*160* Description:161* Initializes the CIPSO label mapping cache, this function should be called162* before any of the other functions defined in this file. Returns zero on163* success, negative values on error.164*165*/166static int __init cipso_v4_cache_init(void)167{168u32 iter;169170cipso_v4_cache = kcalloc(CIPSO_V4_CACHE_BUCKETS,171sizeof(struct cipso_v4_map_cache_bkt),172GFP_KERNEL);173if (!cipso_v4_cache)174return -ENOMEM;175176for (iter = 0; iter < CIPSO_V4_CACHE_BUCKETS; iter++) {177spin_lock_init(&cipso_v4_cache[iter].lock);178cipso_v4_cache[iter].size = 0;179INIT_LIST_HEAD(&cipso_v4_cache[iter].list);180}181182return 0;183}184185/**186* cipso_v4_cache_invalidate - Invalidates the current CIPSO cache187*188* Description:189* Invalidates and frees any entries in the CIPSO cache.190*191*/192void cipso_v4_cache_invalidate(void)193{194struct cipso_v4_map_cache_entry *entry, *tmp_entry;195u32 iter;196197for (iter = 0; iter < CIPSO_V4_CACHE_BUCKETS; iter++) {198spin_lock_bh(&cipso_v4_cache[iter].lock);199list_for_each_entry_safe(entry,200tmp_entry,201&cipso_v4_cache[iter].list, list) {202list_del(&entry->list);203cipso_v4_cache_entry_free(entry);204}205cipso_v4_cache[iter].size = 0;206spin_unlock_bh(&cipso_v4_cache[iter].lock);207}208}209210/**211* cipso_v4_cache_check - Check the CIPSO cache for a label mapping212* @key: the buffer to check213* @key_len: buffer length in bytes214* @secattr: the security attribute struct to use215*216* Description:217* This function checks the cache to see if a label mapping already exists for218* the given key. If there is a match then the cache is adjusted and the219* @secattr struct is populated with the correct LSM security attributes. The220* cache is adjusted in the following manner if the entry is not already the221* first in the cache bucket:222*223* 1. The cache entry's activity counter is incremented224* 2. The previous (higher ranking) entry's activity counter is decremented225* 3. If the difference between the two activity counters is geater than226* CIPSO_V4_CACHE_REORDERLIMIT the two entries are swapped227*228* Returns zero on success, -ENOENT for a cache miss, and other negative values229* on error.230*231*/232static int cipso_v4_cache_check(const unsigned char *key,233u32 key_len,234struct netlbl_lsm_secattr *secattr)235{236u32 bkt;237struct cipso_v4_map_cache_entry *entry;238struct cipso_v4_map_cache_entry *prev_entry = NULL;239u32 hash;240241if (!READ_ONCE(cipso_v4_cache_enabled))242return -ENOENT;243244hash = cipso_v4_map_cache_hash(key, key_len);245bkt = hash & (CIPSO_V4_CACHE_BUCKETS - 1);246spin_lock_bh(&cipso_v4_cache[bkt].lock);247list_for_each_entry(entry, &cipso_v4_cache[bkt].list, list) {248if (entry->hash == hash &&249entry->key_len == key_len &&250memcmp(entry->key, key, key_len) == 0) {251entry->activity += 1;252refcount_inc(&entry->lsm_data->refcount);253secattr->cache = entry->lsm_data;254secattr->flags |= NETLBL_SECATTR_CACHE;255secattr->type = NETLBL_NLTYPE_CIPSOV4;256if (!prev_entry) {257spin_unlock_bh(&cipso_v4_cache[bkt].lock);258return 0;259}260261if (prev_entry->activity > 0)262prev_entry->activity -= 1;263if (entry->activity > prev_entry->activity &&264entry->activity - prev_entry->activity >265CIPSO_V4_CACHE_REORDERLIMIT) {266__list_del(entry->list.prev, entry->list.next);267__list_add(&entry->list,268prev_entry->list.prev,269&prev_entry->list);270}271272spin_unlock_bh(&cipso_v4_cache[bkt].lock);273return 0;274}275prev_entry = entry;276}277spin_unlock_bh(&cipso_v4_cache[bkt].lock);278279return -ENOENT;280}281282/**283* cipso_v4_cache_add - Add an entry to the CIPSO cache284* @cipso_ptr: pointer to CIPSO IP option285* @secattr: the packet's security attributes286*287* Description:288* Add a new entry into the CIPSO label mapping cache. Add the new entry to289* head of the cache bucket's list, if the cache bucket is out of room remove290* the last entry in the list first. It is important to note that there is291* currently no checking for duplicate keys. Returns zero on success,292* negative values on failure.293*294*/295int cipso_v4_cache_add(const unsigned char *cipso_ptr,296const struct netlbl_lsm_secattr *secattr)297{298int bkt_size = READ_ONCE(cipso_v4_cache_bucketsize);299int ret_val = -EPERM;300u32 bkt;301struct cipso_v4_map_cache_entry *entry = NULL;302struct cipso_v4_map_cache_entry *old_entry = NULL;303u32 cipso_ptr_len;304305if (!READ_ONCE(cipso_v4_cache_enabled) || bkt_size <= 0)306return 0;307308cipso_ptr_len = cipso_ptr[1];309310entry = kzalloc(sizeof(*entry), GFP_ATOMIC);311if (!entry)312return -ENOMEM;313entry->key = kmemdup(cipso_ptr, cipso_ptr_len, GFP_ATOMIC);314if (!entry->key) {315ret_val = -ENOMEM;316goto cache_add_failure;317}318entry->key_len = cipso_ptr_len;319entry->hash = cipso_v4_map_cache_hash(cipso_ptr, cipso_ptr_len);320refcount_inc(&secattr->cache->refcount);321entry->lsm_data = secattr->cache;322323bkt = entry->hash & (CIPSO_V4_CACHE_BUCKETS - 1);324spin_lock_bh(&cipso_v4_cache[bkt].lock);325if (cipso_v4_cache[bkt].size < bkt_size) {326list_add(&entry->list, &cipso_v4_cache[bkt].list);327cipso_v4_cache[bkt].size += 1;328} else {329old_entry = list_entry(cipso_v4_cache[bkt].list.prev,330struct cipso_v4_map_cache_entry, list);331list_del(&old_entry->list);332list_add(&entry->list, &cipso_v4_cache[bkt].list);333cipso_v4_cache_entry_free(old_entry);334}335spin_unlock_bh(&cipso_v4_cache[bkt].lock);336337return 0;338339cache_add_failure:340if (entry)341cipso_v4_cache_entry_free(entry);342return ret_val;343}344345/*346* DOI List Functions347*/348349/**350* cipso_v4_doi_search - Searches for a DOI definition351* @doi: the DOI to search for352*353* Description:354* Search the DOI definition list for a DOI definition with a DOI value that355* matches @doi. The caller is responsible for calling rcu_read_[un]lock().356* Returns a pointer to the DOI definition on success and NULL on failure.357*/358static struct cipso_v4_doi *cipso_v4_doi_search(u32 doi)359{360struct cipso_v4_doi *iter;361362list_for_each_entry_rcu(iter, &cipso_v4_doi_list, list)363if (iter->doi == doi && refcount_read(&iter->refcount))364return iter;365return NULL;366}367368/**369* cipso_v4_doi_add - Add a new DOI to the CIPSO protocol engine370* @doi_def: the DOI structure371* @audit_info: NetLabel audit information372*373* Description:374* The caller defines a new DOI for use by the CIPSO engine and calls this375* function to add it to the list of acceptable domains. The caller must376* ensure that the mapping table specified in @doi_def->map meets all of the377* requirements of the mapping type (see cipso_ipv4.h for details). Returns378* zero on success and non-zero on failure.379*380*/381int cipso_v4_doi_add(struct cipso_v4_doi *doi_def,382struct netlbl_audit *audit_info)383{384int ret_val = -EINVAL;385u32 iter;386u32 doi;387u32 doi_type;388struct audit_buffer *audit_buf;389390doi = doi_def->doi;391doi_type = doi_def->type;392393if (doi_def->doi == CIPSO_V4_DOI_UNKNOWN)394goto doi_add_return;395for (iter = 0; iter < CIPSO_V4_TAG_MAXCNT; iter++) {396switch (doi_def->tags[iter]) {397case CIPSO_V4_TAG_RBITMAP:398break;399case CIPSO_V4_TAG_RANGE:400case CIPSO_V4_TAG_ENUM:401if (doi_def->type != CIPSO_V4_MAP_PASS)402goto doi_add_return;403break;404case CIPSO_V4_TAG_LOCAL:405if (doi_def->type != CIPSO_V4_MAP_LOCAL)406goto doi_add_return;407break;408case CIPSO_V4_TAG_INVALID:409if (iter == 0)410goto doi_add_return;411break;412default:413goto doi_add_return;414}415}416417refcount_set(&doi_def->refcount, 1);418419spin_lock(&cipso_v4_doi_list_lock);420if (cipso_v4_doi_search(doi_def->doi)) {421spin_unlock(&cipso_v4_doi_list_lock);422ret_val = -EEXIST;423goto doi_add_return;424}425list_add_tail_rcu(&doi_def->list, &cipso_v4_doi_list);426spin_unlock(&cipso_v4_doi_list_lock);427ret_val = 0;428429doi_add_return:430audit_buf = netlbl_audit_start(AUDIT_MAC_CIPSOV4_ADD, audit_info);431if (audit_buf) {432const char *type_str;433switch (doi_type) {434case CIPSO_V4_MAP_TRANS:435type_str = "trans";436break;437case CIPSO_V4_MAP_PASS:438type_str = "pass";439break;440case CIPSO_V4_MAP_LOCAL:441type_str = "local";442break;443default:444type_str = "(unknown)";445}446audit_log_format(audit_buf,447" cipso_doi=%u cipso_type=%s res=%u",448doi, type_str, ret_val == 0 ? 1 : 0);449audit_log_end(audit_buf);450}451452return ret_val;453}454455/**456* cipso_v4_doi_free - Frees a DOI definition457* @doi_def: the DOI definition458*459* Description:460* This function frees all of the memory associated with a DOI definition.461*462*/463void cipso_v4_doi_free(struct cipso_v4_doi *doi_def)464{465if (!doi_def)466return;467468switch (doi_def->type) {469case CIPSO_V4_MAP_TRANS:470kfree(doi_def->map.std->lvl.cipso);471kfree(doi_def->map.std->lvl.local);472kfree(doi_def->map.std->cat.cipso);473kfree(doi_def->map.std->cat.local);474kfree(doi_def->map.std);475break;476}477kfree(doi_def);478}479480/**481* cipso_v4_doi_free_rcu - Frees a DOI definition via the RCU pointer482* @entry: the entry's RCU field483*484* Description:485* This function is designed to be used as a callback to the call_rcu()486* function so that the memory allocated to the DOI definition can be released487* safely.488*489*/490static void cipso_v4_doi_free_rcu(struct rcu_head *entry)491{492struct cipso_v4_doi *doi_def;493494doi_def = container_of(entry, struct cipso_v4_doi, rcu);495cipso_v4_doi_free(doi_def);496}497498/**499* cipso_v4_doi_remove - Remove an existing DOI from the CIPSO protocol engine500* @doi: the DOI value501* @audit_info: NetLabel audit information502*503* Description:504* Removes a DOI definition from the CIPSO engine. The NetLabel routines will505* be called to release their own LSM domain mappings as well as our own506* domain list. Returns zero on success and negative values on failure.507*508*/509int cipso_v4_doi_remove(u32 doi, struct netlbl_audit *audit_info)510{511int ret_val;512struct cipso_v4_doi *doi_def;513struct audit_buffer *audit_buf;514515spin_lock(&cipso_v4_doi_list_lock);516doi_def = cipso_v4_doi_search(doi);517if (!doi_def) {518spin_unlock(&cipso_v4_doi_list_lock);519ret_val = -ENOENT;520goto doi_remove_return;521}522list_del_rcu(&doi_def->list);523spin_unlock(&cipso_v4_doi_list_lock);524525cipso_v4_doi_putdef(doi_def);526ret_val = 0;527528doi_remove_return:529audit_buf = netlbl_audit_start(AUDIT_MAC_CIPSOV4_DEL, audit_info);530if (audit_buf) {531audit_log_format(audit_buf,532" cipso_doi=%u res=%u",533doi, ret_val == 0 ? 1 : 0);534audit_log_end(audit_buf);535}536537return ret_val;538}539540/**541* cipso_v4_doi_getdef - Returns a reference to a valid DOI definition542* @doi: the DOI value543*544* Description:545* Searches for a valid DOI definition and if one is found it is returned to546* the caller. Otherwise NULL is returned. The caller must ensure that547* rcu_read_lock() is held while accessing the returned definition and the DOI548* definition reference count is decremented when the caller is done.549*550*/551struct cipso_v4_doi *cipso_v4_doi_getdef(u32 doi)552{553struct cipso_v4_doi *doi_def;554555rcu_read_lock();556doi_def = cipso_v4_doi_search(doi);557if (!doi_def)558goto doi_getdef_return;559if (!refcount_inc_not_zero(&doi_def->refcount))560doi_def = NULL;561562doi_getdef_return:563rcu_read_unlock();564return doi_def;565}566567/**568* cipso_v4_doi_putdef - Releases a reference for the given DOI definition569* @doi_def: the DOI definition570*571* Description:572* Releases a DOI definition reference obtained from cipso_v4_doi_getdef().573*574*/575void cipso_v4_doi_putdef(struct cipso_v4_doi *doi_def)576{577if (!doi_def)578return;579580if (!refcount_dec_and_test(&doi_def->refcount))581return;582583cipso_v4_cache_invalidate();584call_rcu(&doi_def->rcu, cipso_v4_doi_free_rcu);585}586587/**588* cipso_v4_doi_walk - Iterate through the DOI definitions589* @skip_cnt: skip past this number of DOI definitions, updated590* @callback: callback for each DOI definition591* @cb_arg: argument for the callback function592*593* Description:594* Iterate over the DOI definition list, skipping the first @skip_cnt entries.595* For each entry call @callback, if @callback returns a negative value stop596* 'walking' through the list and return. Updates the value in @skip_cnt upon597* return. Returns zero on success, negative values on failure.598*599*/600int cipso_v4_doi_walk(u32 *skip_cnt,601int (*callback) (struct cipso_v4_doi *doi_def, void *arg),602void *cb_arg)603{604int ret_val = -ENOENT;605u32 doi_cnt = 0;606struct cipso_v4_doi *iter_doi;607608rcu_read_lock();609list_for_each_entry_rcu(iter_doi, &cipso_v4_doi_list, list)610if (refcount_read(&iter_doi->refcount) > 0) {611if (doi_cnt++ < *skip_cnt)612continue;613ret_val = callback(iter_doi, cb_arg);614if (ret_val < 0) {615doi_cnt--;616goto doi_walk_return;617}618}619620doi_walk_return:621rcu_read_unlock();622*skip_cnt = doi_cnt;623return ret_val;624}625626/*627* Label Mapping Functions628*/629630/**631* cipso_v4_map_lvl_valid - Checks to see if the given level is understood632* @doi_def: the DOI definition633* @level: the level to check634*635* Description:636* Checks the given level against the given DOI definition and returns a637* negative value if the level does not have a valid mapping and a zero value638* if the level is defined by the DOI.639*640*/641static int cipso_v4_map_lvl_valid(const struct cipso_v4_doi *doi_def, u8 level)642{643switch (doi_def->type) {644case CIPSO_V4_MAP_PASS:645return 0;646case CIPSO_V4_MAP_TRANS:647if ((level < doi_def->map.std->lvl.cipso_size) &&648(doi_def->map.std->lvl.cipso[level] < CIPSO_V4_INV_LVL))649return 0;650break;651}652653return -EFAULT;654}655656/**657* cipso_v4_map_lvl_hton - Perform a level mapping from the host to the network658* @doi_def: the DOI definition659* @host_lvl: the host MLS level660* @net_lvl: the network/CIPSO MLS level661*662* Description:663* Perform a label mapping to translate a local MLS level to the correct664* CIPSO level using the given DOI definition. Returns zero on success,665* negative values otherwise.666*667*/668static int cipso_v4_map_lvl_hton(const struct cipso_v4_doi *doi_def,669u32 host_lvl,670u32 *net_lvl)671{672switch (doi_def->type) {673case CIPSO_V4_MAP_PASS:674*net_lvl = host_lvl;675return 0;676case CIPSO_V4_MAP_TRANS:677if (host_lvl < doi_def->map.std->lvl.local_size &&678doi_def->map.std->lvl.local[host_lvl] < CIPSO_V4_INV_LVL) {679*net_lvl = doi_def->map.std->lvl.local[host_lvl];680return 0;681}682return -EPERM;683}684685return -EINVAL;686}687688/**689* cipso_v4_map_lvl_ntoh - Perform a level mapping from the network to the host690* @doi_def: the DOI definition691* @net_lvl: the network/CIPSO MLS level692* @host_lvl: the host MLS level693*694* Description:695* Perform a label mapping to translate a CIPSO level to the correct local MLS696* level using the given DOI definition. Returns zero on success, negative697* values otherwise.698*699*/700static int cipso_v4_map_lvl_ntoh(const struct cipso_v4_doi *doi_def,701u32 net_lvl,702u32 *host_lvl)703{704struct cipso_v4_std_map_tbl *map_tbl;705706switch (doi_def->type) {707case CIPSO_V4_MAP_PASS:708*host_lvl = net_lvl;709return 0;710case CIPSO_V4_MAP_TRANS:711map_tbl = doi_def->map.std;712if (net_lvl < map_tbl->lvl.cipso_size &&713map_tbl->lvl.cipso[net_lvl] < CIPSO_V4_INV_LVL) {714*host_lvl = doi_def->map.std->lvl.cipso[net_lvl];715return 0;716}717return -EPERM;718}719720return -EINVAL;721}722723/**724* cipso_v4_map_cat_rbm_valid - Checks to see if the category bitmap is valid725* @doi_def: the DOI definition726* @bitmap: category bitmap727* @bitmap_len: bitmap length in bytes728*729* Description:730* Checks the given category bitmap against the given DOI definition and731* returns a negative value if any of the categories in the bitmap do not have732* a valid mapping and a zero value if all of the categories are valid.733*734*/735static int cipso_v4_map_cat_rbm_valid(const struct cipso_v4_doi *doi_def,736const unsigned char *bitmap,737u32 bitmap_len)738{739int cat = -1;740u32 bitmap_len_bits = bitmap_len * 8;741u32 cipso_cat_size;742u32 *cipso_array;743744switch (doi_def->type) {745case CIPSO_V4_MAP_PASS:746return 0;747case CIPSO_V4_MAP_TRANS:748cipso_cat_size = doi_def->map.std->cat.cipso_size;749cipso_array = doi_def->map.std->cat.cipso;750for (;;) {751cat = netlbl_bitmap_walk(bitmap,752bitmap_len_bits,753cat + 1,7541);755if (cat < 0)756break;757if (cat >= cipso_cat_size ||758cipso_array[cat] >= CIPSO_V4_INV_CAT)759return -EFAULT;760}761762if (cat == -1)763return 0;764break;765}766767return -EFAULT;768}769770/**771* cipso_v4_map_cat_rbm_hton - Perform a category mapping from host to network772* @doi_def: the DOI definition773* @secattr: the security attributes774* @net_cat: the zero'd out category bitmap in network/CIPSO format775* @net_cat_len: the length of the CIPSO bitmap in bytes776*777* Description:778* Perform a label mapping to translate a local MLS category bitmap to the779* correct CIPSO bitmap using the given DOI definition. Returns the minimum780* size in bytes of the network bitmap on success, negative values otherwise.781*782*/783static int cipso_v4_map_cat_rbm_hton(const struct cipso_v4_doi *doi_def,784const struct netlbl_lsm_secattr *secattr,785unsigned char *net_cat,786u32 net_cat_len)787{788int host_spot = -1;789u32 net_spot = CIPSO_V4_INV_CAT;790u32 net_spot_max = 0;791u32 net_clen_bits = net_cat_len * 8;792u32 host_cat_size = 0;793u32 *host_cat_array = NULL;794795if (doi_def->type == CIPSO_V4_MAP_TRANS) {796host_cat_size = doi_def->map.std->cat.local_size;797host_cat_array = doi_def->map.std->cat.local;798}799800for (;;) {801host_spot = netlbl_catmap_walk(secattr->attr.mls.cat,802host_spot + 1);803if (host_spot < 0)804break;805806switch (doi_def->type) {807case CIPSO_V4_MAP_PASS:808net_spot = host_spot;809break;810case CIPSO_V4_MAP_TRANS:811if (host_spot >= host_cat_size)812return -EPERM;813net_spot = host_cat_array[host_spot];814if (net_spot >= CIPSO_V4_INV_CAT)815return -EPERM;816break;817}818if (net_spot >= net_clen_bits)819return -ENOSPC;820netlbl_bitmap_setbit(net_cat, net_spot, 1);821822if (net_spot > net_spot_max)823net_spot_max = net_spot;824}825826if (++net_spot_max % 8)827return net_spot_max / 8 + 1;828return net_spot_max / 8;829}830831/**832* cipso_v4_map_cat_rbm_ntoh - Perform a category mapping from network to host833* @doi_def: the DOI definition834* @net_cat: the category bitmap in network/CIPSO format835* @net_cat_len: the length of the CIPSO bitmap in bytes836* @secattr: the security attributes837*838* Description:839* Perform a label mapping to translate a CIPSO bitmap to the correct local840* MLS category bitmap using the given DOI definition. Returns zero on841* success, negative values on failure.842*843*/844static int cipso_v4_map_cat_rbm_ntoh(const struct cipso_v4_doi *doi_def,845const unsigned char *net_cat,846u32 net_cat_len,847struct netlbl_lsm_secattr *secattr)848{849int ret_val;850int net_spot = -1;851u32 host_spot = CIPSO_V4_INV_CAT;852u32 net_clen_bits = net_cat_len * 8;853u32 net_cat_size = 0;854u32 *net_cat_array = NULL;855856if (doi_def->type == CIPSO_V4_MAP_TRANS) {857net_cat_size = doi_def->map.std->cat.cipso_size;858net_cat_array = doi_def->map.std->cat.cipso;859}860861for (;;) {862net_spot = netlbl_bitmap_walk(net_cat,863net_clen_bits,864net_spot + 1,8651);866if (net_spot < 0)867return 0;868869switch (doi_def->type) {870case CIPSO_V4_MAP_PASS:871host_spot = net_spot;872break;873case CIPSO_V4_MAP_TRANS:874if (net_spot >= net_cat_size)875return -EPERM;876host_spot = net_cat_array[net_spot];877if (host_spot >= CIPSO_V4_INV_CAT)878return -EPERM;879break;880}881ret_val = netlbl_catmap_setbit(&secattr->attr.mls.cat,882host_spot,883GFP_ATOMIC);884if (ret_val != 0)885return ret_val;886}887888return -EINVAL;889}890891/**892* cipso_v4_map_cat_enum_valid - Checks to see if the categories are valid893* @doi_def: the DOI definition894* @enumcat: category list895* @enumcat_len: length of the category list in bytes896*897* Description:898* Checks the given categories against the given DOI definition and returns a899* negative value if any of the categories do not have a valid mapping and a900* zero value if all of the categories are valid.901*902*/903static int cipso_v4_map_cat_enum_valid(const struct cipso_v4_doi *doi_def,904const unsigned char *enumcat,905u32 enumcat_len)906{907u16 cat;908int cat_prev = -1;909u32 iter;910911if (doi_def->type != CIPSO_V4_MAP_PASS || enumcat_len & 0x01)912return -EFAULT;913914for (iter = 0; iter < enumcat_len; iter += 2) {915cat = get_unaligned_be16(&enumcat[iter]);916if (cat <= cat_prev)917return -EFAULT;918cat_prev = cat;919}920921return 0;922}923924/**925* cipso_v4_map_cat_enum_hton - Perform a category mapping from host to network926* @doi_def: the DOI definition927* @secattr: the security attributes928* @net_cat: the zero'd out category list in network/CIPSO format929* @net_cat_len: the length of the CIPSO category list in bytes930*931* Description:932* Perform a label mapping to translate a local MLS category bitmap to the933* correct CIPSO category list using the given DOI definition. Returns the934* size in bytes of the network category bitmap on success, negative values935* otherwise.936*937*/938static int cipso_v4_map_cat_enum_hton(const struct cipso_v4_doi *doi_def,939const struct netlbl_lsm_secattr *secattr,940unsigned char *net_cat,941u32 net_cat_len)942{943int cat = -1;944u32 cat_iter = 0;945946for (;;) {947cat = netlbl_catmap_walk(secattr->attr.mls.cat, cat + 1);948if (cat < 0)949break;950if ((cat_iter + 2) > net_cat_len)951return -ENOSPC;952953*((__be16 *)&net_cat[cat_iter]) = htons(cat);954cat_iter += 2;955}956957return cat_iter;958}959960/**961* cipso_v4_map_cat_enum_ntoh - Perform a category mapping from network to host962* @doi_def: the DOI definition963* @net_cat: the category list in network/CIPSO format964* @net_cat_len: the length of the CIPSO bitmap in bytes965* @secattr: the security attributes966*967* Description:968* Perform a label mapping to translate a CIPSO category list to the correct969* local MLS category bitmap using the given DOI definition. Returns zero on970* success, negative values on failure.971*972*/973static int cipso_v4_map_cat_enum_ntoh(const struct cipso_v4_doi *doi_def,974const unsigned char *net_cat,975u32 net_cat_len,976struct netlbl_lsm_secattr *secattr)977{978int ret_val;979u32 iter;980981for (iter = 0; iter < net_cat_len; iter += 2) {982ret_val = netlbl_catmap_setbit(&secattr->attr.mls.cat,983get_unaligned_be16(&net_cat[iter]),984GFP_ATOMIC);985if (ret_val != 0)986return ret_val;987}988989return 0;990}991992/**993* cipso_v4_map_cat_rng_valid - Checks to see if the categories are valid994* @doi_def: the DOI definition995* @rngcat: category list996* @rngcat_len: length of the category list in bytes997*998* Description:999* Checks the given categories against the given DOI definition and returns a1000* negative value if any of the categories do not have a valid mapping and a1001* zero value if all of the categories are valid.1002*1003*/1004static int cipso_v4_map_cat_rng_valid(const struct cipso_v4_doi *doi_def,1005const unsigned char *rngcat,1006u32 rngcat_len)1007{1008u16 cat_high;1009u16 cat_low;1010u32 cat_prev = CIPSO_V4_MAX_REM_CATS + 1;1011u32 iter;10121013if (doi_def->type != CIPSO_V4_MAP_PASS || rngcat_len & 0x01)1014return -EFAULT;10151016for (iter = 0; iter < rngcat_len; iter += 4) {1017cat_high = get_unaligned_be16(&rngcat[iter]);1018if ((iter + 4) <= rngcat_len)1019cat_low = get_unaligned_be16(&rngcat[iter + 2]);1020else1021cat_low = 0;10221023if (cat_high > cat_prev)1024return -EFAULT;10251026cat_prev = cat_low;1027}10281029return 0;1030}10311032/**1033* cipso_v4_map_cat_rng_hton - Perform a category mapping from host to network1034* @doi_def: the DOI definition1035* @secattr: the security attributes1036* @net_cat: the zero'd out category list in network/CIPSO format1037* @net_cat_len: the length of the CIPSO category list in bytes1038*1039* Description:1040* Perform a label mapping to translate a local MLS category bitmap to the1041* correct CIPSO category list using the given DOI definition. Returns the1042* size in bytes of the network category bitmap on success, negative values1043* otherwise.1044*1045*/1046static int cipso_v4_map_cat_rng_hton(const struct cipso_v4_doi *doi_def,1047const struct netlbl_lsm_secattr *secattr,1048unsigned char *net_cat,1049u32 net_cat_len)1050{1051int iter = -1;1052u16 array[CIPSO_V4_TAG_RNG_CAT_MAX * 2];1053u32 array_cnt = 0;1054u32 cat_size = 0;10551056/* make sure we don't overflow the 'array[]' variable */1057if (net_cat_len >1058(CIPSO_V4_OPT_LEN_MAX - CIPSO_V4_HDR_LEN - CIPSO_V4_TAG_RNG_BLEN))1059return -ENOSPC;10601061for (;;) {1062iter = netlbl_catmap_walk(secattr->attr.mls.cat, iter + 1);1063if (iter < 0)1064break;1065cat_size += (iter == 0 ? 0 : sizeof(u16));1066if (cat_size > net_cat_len)1067return -ENOSPC;1068array[array_cnt++] = iter;10691070iter = netlbl_catmap_walkrng(secattr->attr.mls.cat, iter);1071if (iter < 0)1072return -EFAULT;1073cat_size += sizeof(u16);1074if (cat_size > net_cat_len)1075return -ENOSPC;1076array[array_cnt++] = iter;1077}10781079for (iter = 0; array_cnt > 0;) {1080*((__be16 *)&net_cat[iter]) = htons(array[--array_cnt]);1081iter += 2;1082array_cnt--;1083if (array[array_cnt] != 0) {1084*((__be16 *)&net_cat[iter]) = htons(array[array_cnt]);1085iter += 2;1086}1087}10881089return cat_size;1090}10911092/**1093* cipso_v4_map_cat_rng_ntoh - Perform a category mapping from network to host1094* @doi_def: the DOI definition1095* @net_cat: the category list in network/CIPSO format1096* @net_cat_len: the length of the CIPSO bitmap in bytes1097* @secattr: the security attributes1098*1099* Description:1100* Perform a label mapping to translate a CIPSO category list to the correct1101* local MLS category bitmap using the given DOI definition. Returns zero on1102* success, negative values on failure.1103*1104*/1105static int cipso_v4_map_cat_rng_ntoh(const struct cipso_v4_doi *doi_def,1106const unsigned char *net_cat,1107u32 net_cat_len,1108struct netlbl_lsm_secattr *secattr)1109{1110int ret_val;1111u32 net_iter;1112u16 cat_low;1113u16 cat_high;11141115for (net_iter = 0; net_iter < net_cat_len; net_iter += 4) {1116cat_high = get_unaligned_be16(&net_cat[net_iter]);1117if ((net_iter + 4) <= net_cat_len)1118cat_low = get_unaligned_be16(&net_cat[net_iter + 2]);1119else1120cat_low = 0;11211122ret_val = netlbl_catmap_setrng(&secattr->attr.mls.cat,1123cat_low,1124cat_high,1125GFP_ATOMIC);1126if (ret_val != 0)1127return ret_val;1128}11291130return 0;1131}11321133/*1134* Protocol Handling Functions1135*/11361137/**1138* cipso_v4_gentag_hdr - Generate a CIPSO option header1139* @doi_def: the DOI definition1140* @len: the total tag length in bytes, not including this header1141* @buf: the CIPSO option buffer1142*1143* Description:1144* Write a CIPSO header into the beginning of @buffer.1145*1146*/1147static void cipso_v4_gentag_hdr(const struct cipso_v4_doi *doi_def,1148unsigned char *buf,1149u32 len)1150{1151buf[0] = IPOPT_CIPSO;1152buf[1] = CIPSO_V4_HDR_LEN + len;1153put_unaligned_be32(doi_def->doi, &buf[2]);1154}11551156/**1157* cipso_v4_gentag_rbm - Generate a CIPSO restricted bitmap tag (type #1)1158* @doi_def: the DOI definition1159* @secattr: the security attributes1160* @buffer: the option buffer1161* @buffer_len: length of buffer in bytes1162*1163* Description:1164* Generate a CIPSO option using the restricted bitmap tag, tag type #1. The1165* actual buffer length may be larger than the indicated size due to1166* translation between host and network category bitmaps. Returns the size of1167* the tag on success, negative values on failure.1168*1169*/1170static int cipso_v4_gentag_rbm(const struct cipso_v4_doi *doi_def,1171const struct netlbl_lsm_secattr *secattr,1172unsigned char *buffer,1173u32 buffer_len)1174{1175int ret_val;1176u32 tag_len;1177u32 level;11781179if ((secattr->flags & NETLBL_SECATTR_MLS_LVL) == 0)1180return -EPERM;11811182ret_val = cipso_v4_map_lvl_hton(doi_def,1183secattr->attr.mls.lvl,1184&level);1185if (ret_val != 0)1186return ret_val;11871188if (secattr->flags & NETLBL_SECATTR_MLS_CAT) {1189ret_val = cipso_v4_map_cat_rbm_hton(doi_def,1190secattr,1191&buffer[4],1192buffer_len - 4);1193if (ret_val < 0)1194return ret_val;11951196/* This will send packets using the "optimized" format when1197* possible as specified in section 3.4.2.6 of the1198* CIPSO draft. */1199if (READ_ONCE(cipso_v4_rbm_optfmt) && ret_val > 0 &&1200ret_val <= 10)1201tag_len = 14;1202else1203tag_len = 4 + ret_val;1204} else1205tag_len = 4;12061207buffer[0] = CIPSO_V4_TAG_RBITMAP;1208buffer[1] = tag_len;1209buffer[3] = level;12101211return tag_len;1212}12131214/**1215* cipso_v4_parsetag_rbm - Parse a CIPSO restricted bitmap tag1216* @doi_def: the DOI definition1217* @tag: the CIPSO tag1218* @secattr: the security attributes1219*1220* Description:1221* Parse a CIPSO restricted bitmap tag (tag type #1) and return the security1222* attributes in @secattr. Return zero on success, negatives values on1223* failure.1224*1225*/1226static int cipso_v4_parsetag_rbm(const struct cipso_v4_doi *doi_def,1227const unsigned char *tag,1228struct netlbl_lsm_secattr *secattr)1229{1230int ret_val;1231u8 tag_len = tag[1];1232u32 level;12331234ret_val = cipso_v4_map_lvl_ntoh(doi_def, tag[3], &level);1235if (ret_val != 0)1236return ret_val;1237secattr->attr.mls.lvl = level;1238secattr->flags |= NETLBL_SECATTR_MLS_LVL;12391240if (tag_len > 4) {1241ret_val = cipso_v4_map_cat_rbm_ntoh(doi_def,1242&tag[4],1243tag_len - 4,1244secattr);1245if (ret_val != 0) {1246netlbl_catmap_free(secattr->attr.mls.cat);1247return ret_val;1248}12491250if (secattr->attr.mls.cat)1251secattr->flags |= NETLBL_SECATTR_MLS_CAT;1252}12531254return 0;1255}12561257/**1258* cipso_v4_gentag_enum - Generate a CIPSO enumerated tag (type #2)1259* @doi_def: the DOI definition1260* @secattr: the security attributes1261* @buffer: the option buffer1262* @buffer_len: length of buffer in bytes1263*1264* Description:1265* Generate a CIPSO option using the enumerated tag, tag type #2. Returns the1266* size of the tag on success, negative values on failure.1267*1268*/1269static int cipso_v4_gentag_enum(const struct cipso_v4_doi *doi_def,1270const struct netlbl_lsm_secattr *secattr,1271unsigned char *buffer,1272u32 buffer_len)1273{1274int ret_val;1275u32 tag_len;1276u32 level;12771278if (!(secattr->flags & NETLBL_SECATTR_MLS_LVL))1279return -EPERM;12801281ret_val = cipso_v4_map_lvl_hton(doi_def,1282secattr->attr.mls.lvl,1283&level);1284if (ret_val != 0)1285return ret_val;12861287if (secattr->flags & NETLBL_SECATTR_MLS_CAT) {1288ret_val = cipso_v4_map_cat_enum_hton(doi_def,1289secattr,1290&buffer[4],1291buffer_len - 4);1292if (ret_val < 0)1293return ret_val;12941295tag_len = 4 + ret_val;1296} else1297tag_len = 4;12981299buffer[0] = CIPSO_V4_TAG_ENUM;1300buffer[1] = tag_len;1301buffer[3] = level;13021303return tag_len;1304}13051306/**1307* cipso_v4_parsetag_enum - Parse a CIPSO enumerated tag1308* @doi_def: the DOI definition1309* @tag: the CIPSO tag1310* @secattr: the security attributes1311*1312* Description:1313* Parse a CIPSO enumerated tag (tag type #2) and return the security1314* attributes in @secattr. Return zero on success, negatives values on1315* failure.1316*1317*/1318static int cipso_v4_parsetag_enum(const struct cipso_v4_doi *doi_def,1319const unsigned char *tag,1320struct netlbl_lsm_secattr *secattr)1321{1322int ret_val;1323u8 tag_len = tag[1];1324u32 level;13251326ret_val = cipso_v4_map_lvl_ntoh(doi_def, tag[3], &level);1327if (ret_val != 0)1328return ret_val;1329secattr->attr.mls.lvl = level;1330secattr->flags |= NETLBL_SECATTR_MLS_LVL;13311332if (tag_len > 4) {1333ret_val = cipso_v4_map_cat_enum_ntoh(doi_def,1334&tag[4],1335tag_len - 4,1336secattr);1337if (ret_val != 0) {1338netlbl_catmap_free(secattr->attr.mls.cat);1339return ret_val;1340}13411342secattr->flags |= NETLBL_SECATTR_MLS_CAT;1343}13441345return 0;1346}13471348/**1349* cipso_v4_gentag_rng - Generate a CIPSO ranged tag (type #5)1350* @doi_def: the DOI definition1351* @secattr: the security attributes1352* @buffer: the option buffer1353* @buffer_len: length of buffer in bytes1354*1355* Description:1356* Generate a CIPSO option using the ranged tag, tag type #5. Returns the1357* size of the tag on success, negative values on failure.1358*1359*/1360static int cipso_v4_gentag_rng(const struct cipso_v4_doi *doi_def,1361const struct netlbl_lsm_secattr *secattr,1362unsigned char *buffer,1363u32 buffer_len)1364{1365int ret_val;1366u32 tag_len;1367u32 level;13681369if (!(secattr->flags & NETLBL_SECATTR_MLS_LVL))1370return -EPERM;13711372ret_val = cipso_v4_map_lvl_hton(doi_def,1373secattr->attr.mls.lvl,1374&level);1375if (ret_val != 0)1376return ret_val;13771378if (secattr->flags & NETLBL_SECATTR_MLS_CAT) {1379ret_val = cipso_v4_map_cat_rng_hton(doi_def,1380secattr,1381&buffer[4],1382buffer_len - 4);1383if (ret_val < 0)1384return ret_val;13851386tag_len = 4 + ret_val;1387} else1388tag_len = 4;13891390buffer[0] = CIPSO_V4_TAG_RANGE;1391buffer[1] = tag_len;1392buffer[3] = level;13931394return tag_len;1395}13961397/**1398* cipso_v4_parsetag_rng - Parse a CIPSO ranged tag1399* @doi_def: the DOI definition1400* @tag: the CIPSO tag1401* @secattr: the security attributes1402*1403* Description:1404* Parse a CIPSO ranged tag (tag type #5) and return the security attributes1405* in @secattr. Return zero on success, negatives values on failure.1406*1407*/1408static int cipso_v4_parsetag_rng(const struct cipso_v4_doi *doi_def,1409const unsigned char *tag,1410struct netlbl_lsm_secattr *secattr)1411{1412int ret_val;1413u8 tag_len = tag[1];1414u32 level;14151416ret_val = cipso_v4_map_lvl_ntoh(doi_def, tag[3], &level);1417if (ret_val != 0)1418return ret_val;1419secattr->attr.mls.lvl = level;1420secattr->flags |= NETLBL_SECATTR_MLS_LVL;14211422if (tag_len > 4) {1423ret_val = cipso_v4_map_cat_rng_ntoh(doi_def,1424&tag[4],1425tag_len - 4,1426secattr);1427if (ret_val != 0) {1428netlbl_catmap_free(secattr->attr.mls.cat);1429return ret_val;1430}14311432if (secattr->attr.mls.cat)1433secattr->flags |= NETLBL_SECATTR_MLS_CAT;1434}14351436return 0;1437}14381439/**1440* cipso_v4_gentag_loc - Generate a CIPSO local tag (non-standard)1441* @doi_def: the DOI definition1442* @secattr: the security attributes1443* @buffer: the option buffer1444* @buffer_len: length of buffer in bytes1445*1446* Description:1447* Generate a CIPSO option using the local tag. Returns the size of the tag1448* on success, negative values on failure.1449*1450*/1451static int cipso_v4_gentag_loc(const struct cipso_v4_doi *doi_def,1452const struct netlbl_lsm_secattr *secattr,1453unsigned char *buffer,1454u32 buffer_len)1455{1456if (!(secattr->flags & NETLBL_SECATTR_SECID))1457return -EPERM;14581459buffer[0] = CIPSO_V4_TAG_LOCAL;1460buffer[1] = CIPSO_V4_TAG_LOC_BLEN;1461*(u32 *)&buffer[2] = secattr->attr.secid;14621463return CIPSO_V4_TAG_LOC_BLEN;1464}14651466/**1467* cipso_v4_parsetag_loc - Parse a CIPSO local tag1468* @doi_def: the DOI definition1469* @tag: the CIPSO tag1470* @secattr: the security attributes1471*1472* Description:1473* Parse a CIPSO local tag and return the security attributes in @secattr.1474* Return zero on success, negatives values on failure.1475*1476*/1477static int cipso_v4_parsetag_loc(const struct cipso_v4_doi *doi_def,1478const unsigned char *tag,1479struct netlbl_lsm_secattr *secattr)1480{1481secattr->attr.secid = *(u32 *)&tag[2];1482secattr->flags |= NETLBL_SECATTR_SECID;14831484return 0;1485}14861487/**1488* cipso_v4_optptr - Find the CIPSO option in the packet1489* @skb: the packet1490*1491* Description:1492* Parse the packet's IP header looking for a CIPSO option. Returns a pointer1493* to the start of the CIPSO option on success, NULL if one is not found.1494*1495*/1496unsigned char *cipso_v4_optptr(const struct sk_buff *skb)1497{1498const struct iphdr *iph = ip_hdr(skb);1499unsigned char *optptr = (unsigned char *)&(ip_hdr(skb)[1]);1500int optlen;1501int taglen;15021503for (optlen = iph->ihl*4 - sizeof(struct iphdr); optlen > 1; ) {1504switch (optptr[0]) {1505case IPOPT_END:1506return NULL;1507case IPOPT_NOOP:1508taglen = 1;1509break;1510default:1511taglen = optptr[1];1512}1513if (!taglen || taglen > optlen)1514return NULL;1515if (optptr[0] == IPOPT_CIPSO)1516return optptr;15171518optlen -= taglen;1519optptr += taglen;1520}15211522return NULL;1523}15241525/**1526* cipso_v4_validate - Validate a CIPSO option1527* @skb: the packet1528* @option: the start of the option, on error it is set to point to the error1529*1530* Description:1531* This routine is called to validate a CIPSO option, it checks all of the1532* fields to ensure that they are at least valid, see the draft snippet below1533* for details. If the option is valid then a zero value is returned and1534* the value of @option is unchanged. If the option is invalid then a1535* non-zero value is returned and @option is adjusted to point to the1536* offending portion of the option. From the IETF draft ...1537*1538* "If any field within the CIPSO options, such as the DOI identifier, is not1539* recognized the IP datagram is discarded and an ICMP 'parameter problem'1540* (type 12) is generated and returned. The ICMP code field is set to 'bad1541* parameter' (code 0) and the pointer is set to the start of the CIPSO field1542* that is unrecognized."1543*1544*/1545int cipso_v4_validate(const struct sk_buff *skb, unsigned char **option)1546{1547unsigned char *opt = *option;1548unsigned char *tag;1549unsigned char opt_iter;1550unsigned char err_offset = 0;1551u8 opt_len;1552u8 tag_len;1553struct cipso_v4_doi *doi_def = NULL;1554u32 tag_iter;15551556/* caller already checks for length values that are too large */1557opt_len = opt[1];1558if (opt_len < 8) {1559err_offset = 1;1560goto validate_return;1561}15621563rcu_read_lock();1564doi_def = cipso_v4_doi_search(get_unaligned_be32(&opt[2]));1565if (!doi_def) {1566err_offset = 2;1567goto validate_return_locked;1568}15691570opt_iter = CIPSO_V4_HDR_LEN;1571tag = opt + opt_iter;1572while (opt_iter < opt_len) {1573for (tag_iter = 0; doi_def->tags[tag_iter] != tag[0];)1574if (doi_def->tags[tag_iter] == CIPSO_V4_TAG_INVALID ||1575++tag_iter == CIPSO_V4_TAG_MAXCNT) {1576err_offset = opt_iter;1577goto validate_return_locked;1578}15791580if (opt_iter + 1 == opt_len) {1581err_offset = opt_iter;1582goto validate_return_locked;1583}1584tag_len = tag[1];1585if (tag_len > (opt_len - opt_iter)) {1586err_offset = opt_iter + 1;1587goto validate_return_locked;1588}15891590switch (tag[0]) {1591case CIPSO_V4_TAG_RBITMAP:1592if (tag_len < CIPSO_V4_TAG_RBM_BLEN) {1593err_offset = opt_iter + 1;1594goto validate_return_locked;1595}15961597/* We are already going to do all the verification1598* necessary at the socket layer so from our point of1599* view it is safe to turn these checks off (and less1600* work), however, the CIPSO draft says we should do1601* all the CIPSO validations here but it doesn't1602* really specify _exactly_ what we need to validate1603* ... so, just make it a sysctl tunable. */1604if (READ_ONCE(cipso_v4_rbm_strictvalid)) {1605if (cipso_v4_map_lvl_valid(doi_def,1606tag[3]) < 0) {1607err_offset = opt_iter + 3;1608goto validate_return_locked;1609}1610if (tag_len > CIPSO_V4_TAG_RBM_BLEN &&1611cipso_v4_map_cat_rbm_valid(doi_def,1612&tag[4],1613tag_len - 4) < 0) {1614err_offset = opt_iter + 4;1615goto validate_return_locked;1616}1617}1618break;1619case CIPSO_V4_TAG_ENUM:1620if (tag_len < CIPSO_V4_TAG_ENUM_BLEN) {1621err_offset = opt_iter + 1;1622goto validate_return_locked;1623}16241625if (cipso_v4_map_lvl_valid(doi_def,1626tag[3]) < 0) {1627err_offset = opt_iter + 3;1628goto validate_return_locked;1629}1630if (tag_len > CIPSO_V4_TAG_ENUM_BLEN &&1631cipso_v4_map_cat_enum_valid(doi_def,1632&tag[4],1633tag_len - 4) < 0) {1634err_offset = opt_iter + 4;1635goto validate_return_locked;1636}1637break;1638case CIPSO_V4_TAG_RANGE:1639if (tag_len < CIPSO_V4_TAG_RNG_BLEN) {1640err_offset = opt_iter + 1;1641goto validate_return_locked;1642}16431644if (cipso_v4_map_lvl_valid(doi_def,1645tag[3]) < 0) {1646err_offset = opt_iter + 3;1647goto validate_return_locked;1648}1649if (tag_len > CIPSO_V4_TAG_RNG_BLEN &&1650cipso_v4_map_cat_rng_valid(doi_def,1651&tag[4],1652tag_len - 4) < 0) {1653err_offset = opt_iter + 4;1654goto validate_return_locked;1655}1656break;1657case CIPSO_V4_TAG_LOCAL:1658/* This is a non-standard tag that we only allow for1659* local connections, so if the incoming interface is1660* not the loopback device drop the packet. Further,1661* there is no legitimate reason for setting this from1662* userspace so reject it if skb is NULL. */1663if (!skb || !(skb->dev->flags & IFF_LOOPBACK)) {1664err_offset = opt_iter;1665goto validate_return_locked;1666}1667if (tag_len != CIPSO_V4_TAG_LOC_BLEN) {1668err_offset = opt_iter + 1;1669goto validate_return_locked;1670}1671break;1672default:1673err_offset = opt_iter;1674goto validate_return_locked;1675}16761677tag += tag_len;1678opt_iter += tag_len;1679}16801681validate_return_locked:1682rcu_read_unlock();1683validate_return:1684*option = opt + err_offset;1685return err_offset;1686}16871688/**1689* cipso_v4_error - Send the correct response for a bad packet1690* @skb: the packet1691* @error: the error code1692* @gateway: CIPSO gateway flag1693*1694* Description:1695* Based on the error code given in @error, send an ICMP error message back to1696* the originating host. From the IETF draft ...1697*1698* "If the contents of the CIPSO [option] are valid but the security label is1699* outside of the configured host or port label range, the datagram is1700* discarded and an ICMP 'destination unreachable' (type 3) is generated and1701* returned. The code field of the ICMP is set to 'communication with1702* destination network administratively prohibited' (code 9) or to1703* 'communication with destination host administratively prohibited'1704* (code 10). The value of the code is dependent on whether the originator1705* of the ICMP message is acting as a CIPSO host or a CIPSO gateway. The1706* recipient of the ICMP message MUST be able to handle either value. The1707* same procedure is performed if a CIPSO [option] can not be added to an1708* IP packet because it is too large to fit in the IP options area."1709*1710* "If the error is triggered by receipt of an ICMP message, the message is1711* discarded and no response is permitted (consistent with general ICMP1712* processing rules)."1713*1714*/1715void cipso_v4_error(struct sk_buff *skb, int error, u32 gateway)1716{1717unsigned char optbuf[sizeof(struct ip_options) + 40];1718struct ip_options *opt = (struct ip_options *)optbuf;1719int res;17201721if (ip_hdr(skb)->protocol == IPPROTO_ICMP || error != -EACCES)1722return;17231724/*1725* We might be called above the IP layer,1726* so we can not use icmp_send and IPCB here.1727*/17281729memset(opt, 0, sizeof(struct ip_options));1730opt->optlen = ip_hdr(skb)->ihl*4 - sizeof(struct iphdr);1731rcu_read_lock();1732res = __ip_options_compile(dev_net(skb->dev), opt, skb, NULL);1733rcu_read_unlock();17341735if (res)1736return;17371738if (gateway)1739__icmp_send(skb, ICMP_DEST_UNREACH, ICMP_NET_ANO, 0, opt);1740else1741__icmp_send(skb, ICMP_DEST_UNREACH, ICMP_HOST_ANO, 0, opt);1742}17431744/**1745* cipso_v4_genopt - Generate a CIPSO option1746* @buf: the option buffer1747* @buf_len: the size of opt_buf1748* @doi_def: the CIPSO DOI to use1749* @secattr: the security attributes1750*1751* Description:1752* Generate a CIPSO option using the DOI definition and security attributes1753* passed to the function. Returns the length of the option on success and1754* negative values on failure.1755*1756*/1757static int cipso_v4_genopt(unsigned char *buf, u32 buf_len,1758const struct cipso_v4_doi *doi_def,1759const struct netlbl_lsm_secattr *secattr)1760{1761int ret_val;1762u32 iter;17631764if (buf_len <= CIPSO_V4_HDR_LEN)1765return -ENOSPC;17661767/* XXX - This code assumes only one tag per CIPSO option which isn't1768* really a good assumption to make but since we only support the MAC1769* tags right now it is a safe assumption. */1770iter = 0;1771do {1772memset(buf, 0, buf_len);1773switch (doi_def->tags[iter]) {1774case CIPSO_V4_TAG_RBITMAP:1775ret_val = cipso_v4_gentag_rbm(doi_def,1776secattr,1777&buf[CIPSO_V4_HDR_LEN],1778buf_len - CIPSO_V4_HDR_LEN);1779break;1780case CIPSO_V4_TAG_ENUM:1781ret_val = cipso_v4_gentag_enum(doi_def,1782secattr,1783&buf[CIPSO_V4_HDR_LEN],1784buf_len - CIPSO_V4_HDR_LEN);1785break;1786case CIPSO_V4_TAG_RANGE:1787ret_val = cipso_v4_gentag_rng(doi_def,1788secattr,1789&buf[CIPSO_V4_HDR_LEN],1790buf_len - CIPSO_V4_HDR_LEN);1791break;1792case CIPSO_V4_TAG_LOCAL:1793ret_val = cipso_v4_gentag_loc(doi_def,1794secattr,1795&buf[CIPSO_V4_HDR_LEN],1796buf_len - CIPSO_V4_HDR_LEN);1797break;1798default:1799return -EPERM;1800}18011802iter++;1803} while (ret_val < 0 &&1804iter < CIPSO_V4_TAG_MAXCNT &&1805doi_def->tags[iter] != CIPSO_V4_TAG_INVALID);1806if (ret_val < 0)1807return ret_val;1808cipso_v4_gentag_hdr(doi_def, buf, ret_val);1809return CIPSO_V4_HDR_LEN + ret_val;1810}18111812static int cipso_v4_get_actual_opt_len(const unsigned char *data, int len)1813{1814int iter = 0, optlen = 0;18151816/* determining the new total option length is tricky because of1817* the padding necessary, the only thing i can think to do at1818* this point is walk the options one-by-one, skipping the1819* padding at the end to determine the actual option size and1820* from there we can determine the new total option length1821*/1822while (iter < len) {1823if (data[iter] == IPOPT_END) {1824break;1825} else if (data[iter] == IPOPT_NOP) {1826iter++;1827} else {1828iter += data[iter + 1];1829optlen = iter;1830}1831}1832return optlen;1833}18341835/**1836* cipso_v4_sock_setattr - Add a CIPSO option to a socket1837* @sk: the socket1838* @doi_def: the CIPSO DOI to use1839* @secattr: the specific security attributes of the socket1840* @sk_locked: true if caller holds the socket lock1841*1842* Description:1843* Set the CIPSO option on the given socket using the DOI definition and1844* security attributes passed to the function. This function requires1845* exclusive access to @sk, which means it either needs to be in the1846* process of being created or locked. Returns zero on success and negative1847* values on failure.1848*1849*/1850int cipso_v4_sock_setattr(struct sock *sk,1851const struct cipso_v4_doi *doi_def,1852const struct netlbl_lsm_secattr *secattr,1853bool sk_locked)1854{1855int ret_val = -EPERM;1856unsigned char *buf = NULL;1857u32 buf_len;1858u32 opt_len;1859struct ip_options_rcu *old, *opt = NULL;1860struct inet_sock *sk_inet;1861struct inet_connection_sock *sk_conn;18621863/* In the case of sock_create_lite(), the sock->sk field is not1864* defined yet but it is not a problem as the only users of these1865* "lite" PF_INET sockets are functions which do an accept() call1866* afterwards so we will label the socket as part of the accept(). */1867if (!sk)1868return 0;18691870/* We allocate the maximum CIPSO option size here so we are probably1871* being a little wasteful, but it makes our life _much_ easier later1872* on and after all we are only talking about 40 bytes. */1873buf_len = CIPSO_V4_OPT_LEN_MAX;1874buf = kmalloc(buf_len, GFP_ATOMIC);1875if (!buf) {1876ret_val = -ENOMEM;1877goto socket_setattr_failure;1878}18791880ret_val = cipso_v4_genopt(buf, buf_len, doi_def, secattr);1881if (ret_val < 0)1882goto socket_setattr_failure;1883buf_len = ret_val;18841885/* We can't use ip_options_get() directly because it makes a call to1886* ip_options_get_alloc() which allocates memory with GFP_KERNEL and1887* we won't always have CAP_NET_RAW even though we _always_ want to1888* set the IPOPT_CIPSO option. */1889opt_len = (buf_len + 3) & ~3;1890opt = kzalloc(sizeof(*opt) + opt_len, GFP_ATOMIC);1891if (!opt) {1892ret_val = -ENOMEM;1893goto socket_setattr_failure;1894}1895memcpy(opt->opt.__data, buf, buf_len);1896opt->opt.optlen = opt_len;1897opt->opt.cipso = sizeof(struct iphdr);1898kfree(buf);1899buf = NULL;19001901sk_inet = inet_sk(sk);19021903old = rcu_dereference_protected(sk_inet->inet_opt, sk_locked);1904if (inet_test_bit(IS_ICSK, sk)) {1905sk_conn = inet_csk(sk);1906if (old)1907sk_conn->icsk_ext_hdr_len -= old->opt.optlen;1908sk_conn->icsk_ext_hdr_len += opt->opt.optlen;1909sk_conn->icsk_sync_mss(sk, sk_conn->icsk_pmtu_cookie);1910}1911rcu_assign_pointer(sk_inet->inet_opt, opt);1912if (old)1913kfree_rcu(old, rcu);19141915return 0;19161917socket_setattr_failure:1918kfree(buf);1919kfree(opt);1920return ret_val;1921}19221923/**1924* cipso_v4_req_setattr - Add a CIPSO option to a connection request socket1925* @req: the connection request socket1926* @doi_def: the CIPSO DOI to use1927* @secattr: the specific security attributes of the socket1928*1929* Description:1930* Set the CIPSO option on the given socket using the DOI definition and1931* security attributes passed to the function. Returns zero on success and1932* negative values on failure.1933*1934*/1935int cipso_v4_req_setattr(struct request_sock *req,1936const struct cipso_v4_doi *doi_def,1937const struct netlbl_lsm_secattr *secattr)1938{1939int ret_val = -EPERM;1940unsigned char *buf = NULL;1941u32 buf_len;1942u32 opt_len;1943struct ip_options_rcu *opt = NULL;1944struct inet_request_sock *req_inet;19451946/* We allocate the maximum CIPSO option size here so we are probably1947* being a little wasteful, but it makes our life _much_ easier later1948* on and after all we are only talking about 40 bytes. */1949buf_len = CIPSO_V4_OPT_LEN_MAX;1950buf = kmalloc(buf_len, GFP_ATOMIC);1951if (!buf) {1952ret_val = -ENOMEM;1953goto req_setattr_failure;1954}19551956ret_val = cipso_v4_genopt(buf, buf_len, doi_def, secattr);1957if (ret_val < 0)1958goto req_setattr_failure;1959buf_len = ret_val;19601961/* We can't use ip_options_get() directly because it makes a call to1962* ip_options_get_alloc() which allocates memory with GFP_KERNEL and1963* we won't always have CAP_NET_RAW even though we _always_ want to1964* set the IPOPT_CIPSO option. */1965opt_len = (buf_len + 3) & ~3;1966opt = kzalloc(sizeof(*opt) + opt_len, GFP_ATOMIC);1967if (!opt) {1968ret_val = -ENOMEM;1969goto req_setattr_failure;1970}1971memcpy(opt->opt.__data, buf, buf_len);1972opt->opt.optlen = opt_len;1973opt->opt.cipso = sizeof(struct iphdr);1974kfree(buf);1975buf = NULL;19761977req_inet = inet_rsk(req);1978opt = unrcu_pointer(xchg(&req_inet->ireq_opt, RCU_INITIALIZER(opt)));1979if (opt)1980kfree_rcu(opt, rcu);19811982return 0;19831984req_setattr_failure:1985kfree(buf);1986kfree(opt);1987return ret_val;1988}19891990/**1991* cipso_v4_delopt - Delete the CIPSO option from a set of IP options1992* @opt_ptr: IP option pointer1993*1994* Description:1995* Deletes the CIPSO IP option from a set of IP options and makes the necessary1996* adjustments to the IP option structure. Returns zero on success, negative1997* values on failure.1998*1999*/2000static int cipso_v4_delopt(struct ip_options_rcu __rcu **opt_ptr)2001{2002struct ip_options_rcu *opt = rcu_dereference_protected(*opt_ptr, 1);2003int hdr_delta = 0;20042005if (!opt || opt->opt.cipso == 0)2006return 0;2007if (opt->opt.srr || opt->opt.rr || opt->opt.ts || opt->opt.router_alert) {2008u8 cipso_len;2009u8 cipso_off;2010unsigned char *cipso_ptr;2011int optlen_new;20122013cipso_off = opt->opt.cipso - sizeof(struct iphdr);2014cipso_ptr = &opt->opt.__data[cipso_off];2015cipso_len = cipso_ptr[1];20162017if (opt->opt.srr > opt->opt.cipso)2018opt->opt.srr -= cipso_len;2019if (opt->opt.rr > opt->opt.cipso)2020opt->opt.rr -= cipso_len;2021if (opt->opt.ts > opt->opt.cipso)2022opt->opt.ts -= cipso_len;2023if (opt->opt.router_alert > opt->opt.cipso)2024opt->opt.router_alert -= cipso_len;2025opt->opt.cipso = 0;20262027memmove(cipso_ptr, cipso_ptr + cipso_len,2028opt->opt.optlen - cipso_off - cipso_len);20292030optlen_new = cipso_v4_get_actual_opt_len(opt->opt.__data,2031opt->opt.optlen);2032hdr_delta = opt->opt.optlen;2033opt->opt.optlen = (optlen_new + 3) & ~3;2034hdr_delta -= opt->opt.optlen;2035} else {2036/* only the cipso option was present on the socket so we can2037* remove the entire option struct */2038*opt_ptr = NULL;2039hdr_delta = opt->opt.optlen;2040kfree_rcu(opt, rcu);2041}20422043return hdr_delta;2044}20452046/**2047* cipso_v4_sock_delattr - Delete the CIPSO option from a socket2048* @sk: the socket2049*2050* Description:2051* Removes the CIPSO option from a socket, if present.2052*2053*/2054void cipso_v4_sock_delattr(struct sock *sk)2055{2056struct inet_sock *sk_inet;2057int hdr_delta;20582059sk_inet = inet_sk(sk);20602061hdr_delta = cipso_v4_delopt(&sk_inet->inet_opt);2062if (inet_test_bit(IS_ICSK, sk) && hdr_delta > 0) {2063struct inet_connection_sock *sk_conn = inet_csk(sk);2064sk_conn->icsk_ext_hdr_len -= hdr_delta;2065sk_conn->icsk_sync_mss(sk, sk_conn->icsk_pmtu_cookie);2066}2067}20682069/**2070* cipso_v4_req_delattr - Delete the CIPSO option from a request socket2071* @req: the request socket2072*2073* Description:2074* Removes the CIPSO option from a request socket, if present.2075*2076*/2077void cipso_v4_req_delattr(struct request_sock *req)2078{2079cipso_v4_delopt(&inet_rsk(req)->ireq_opt);2080}20812082/**2083* cipso_v4_getattr - Helper function for the cipso_v4_*_getattr functions2084* @cipso: the CIPSO v4 option2085* @secattr: the security attributes2086*2087* Description:2088* Inspect @cipso and return the security attributes in @secattr. Returns zero2089* on success and negative values on failure.2090*2091*/2092int cipso_v4_getattr(const unsigned char *cipso,2093struct netlbl_lsm_secattr *secattr)2094{2095int ret_val = -ENOMSG;2096u32 doi;2097struct cipso_v4_doi *doi_def;20982099if (cipso_v4_cache_check(cipso, cipso[1], secattr) == 0)2100return 0;21012102doi = get_unaligned_be32(&cipso[2]);2103rcu_read_lock();2104doi_def = cipso_v4_doi_search(doi);2105if (!doi_def)2106goto getattr_return;2107/* XXX - This code assumes only one tag per CIPSO option which isn't2108* really a good assumption to make but since we only support the MAC2109* tags right now it is a safe assumption. */2110switch (cipso[6]) {2111case CIPSO_V4_TAG_RBITMAP:2112ret_val = cipso_v4_parsetag_rbm(doi_def, &cipso[6], secattr);2113break;2114case CIPSO_V4_TAG_ENUM:2115ret_val = cipso_v4_parsetag_enum(doi_def, &cipso[6], secattr);2116break;2117case CIPSO_V4_TAG_RANGE:2118ret_val = cipso_v4_parsetag_rng(doi_def, &cipso[6], secattr);2119break;2120case CIPSO_V4_TAG_LOCAL:2121ret_val = cipso_v4_parsetag_loc(doi_def, &cipso[6], secattr);2122break;2123}2124if (ret_val == 0)2125secattr->type = NETLBL_NLTYPE_CIPSOV4;21262127getattr_return:2128rcu_read_unlock();2129return ret_val;2130}21312132/**2133* cipso_v4_sock_getattr - Get the security attributes from a sock2134* @sk: the sock2135* @secattr: the security attributes2136*2137* Description:2138* Query @sk to see if there is a CIPSO option attached to the sock and if2139* there is return the CIPSO security attributes in @secattr. This function2140* requires that @sk be locked, or privately held, but it does not do any2141* locking itself. Returns zero on success and negative values on failure.2142*2143*/2144int cipso_v4_sock_getattr(struct sock *sk, struct netlbl_lsm_secattr *secattr)2145{2146struct ip_options_rcu *opt;2147int res = -ENOMSG;21482149rcu_read_lock();2150opt = rcu_dereference(inet_sk(sk)->inet_opt);2151if (opt && opt->opt.cipso)2152res = cipso_v4_getattr(opt->opt.__data +2153opt->opt.cipso -2154sizeof(struct iphdr),2155secattr);2156rcu_read_unlock();2157return res;2158}21592160/**2161* cipso_v4_skbuff_setattr - Set the CIPSO option on a packet2162* @skb: the packet2163* @doi_def: the DOI structure2164* @secattr: the security attributes2165*2166* Description:2167* Set the CIPSO option on the given packet based on the security attributes.2168* Returns a pointer to the IP header on success and NULL on failure.2169*2170*/2171int cipso_v4_skbuff_setattr(struct sk_buff *skb,2172const struct cipso_v4_doi *doi_def,2173const struct netlbl_lsm_secattr *secattr)2174{2175int ret_val;2176struct iphdr *iph;2177struct ip_options *opt = &IPCB(skb)->opt;2178unsigned char buf[CIPSO_V4_OPT_LEN_MAX];2179u32 buf_len = CIPSO_V4_OPT_LEN_MAX;2180u32 opt_len;2181int len_delta;21822183ret_val = cipso_v4_genopt(buf, buf_len, doi_def, secattr);2184if (ret_val < 0)2185return ret_val;2186buf_len = ret_val;2187opt_len = (buf_len + 3) & ~3;21882189/* we overwrite any existing options to ensure that we have enough2190* room for the CIPSO option, the reason is that we _need_ to guarantee2191* that the security label is applied to the packet - we do the same2192* thing when using the socket options and it hasn't caused a problem,2193* if we need to we can always revisit this choice later */21942195len_delta = opt_len - opt->optlen;2196/* if we don't ensure enough headroom we could panic on the skb_push()2197* call below so make sure we have enough, we are also "mangling" the2198* packet so we should probably do a copy-on-write call anyway */2199ret_val = skb_cow(skb, skb_headroom(skb) + len_delta);2200if (ret_val < 0)2201return ret_val;22022203if (len_delta > 0) {2204/* we assume that the header + opt->optlen have already been2205* "pushed" in ip_options_build() or similar */2206iph = ip_hdr(skb);2207skb_push(skb, len_delta);2208memmove((char *)iph - len_delta, iph, iph->ihl << 2);2209skb_reset_network_header(skb);2210iph = ip_hdr(skb);2211} else if (len_delta < 0) {2212iph = ip_hdr(skb);2213memset(iph + 1, IPOPT_NOP, opt->optlen);2214} else2215iph = ip_hdr(skb);22162217if (opt->optlen > 0)2218memset(opt, 0, sizeof(*opt));2219opt->optlen = opt_len;2220opt->cipso = sizeof(struct iphdr);2221opt->is_changed = 1;22222223/* we have to do the following because we are being called from a2224* netfilter hook which means the packet already has had the header2225* fields populated and the checksum calculated - yes this means we2226* are doing more work than needed but we do it to keep the core2227* stack clean and tidy */2228memcpy(iph + 1, buf, buf_len);2229if (opt_len > buf_len)2230memset((char *)(iph + 1) + buf_len, 0, opt_len - buf_len);2231if (len_delta != 0) {2232iph->ihl = 5 + (opt_len >> 2);2233iph_set_totlen(iph, skb->len);2234}2235ip_send_check(iph);22362237return 0;2238}22392240/**2241* cipso_v4_skbuff_delattr - Delete any CIPSO options from a packet2242* @skb: the packet2243*2244* Description:2245* Removes any and all CIPSO options from the given packet. Returns zero on2246* success, negative values on failure.2247*2248*/2249int cipso_v4_skbuff_delattr(struct sk_buff *skb)2250{2251int ret_val, cipso_len, hdr_len_actual, new_hdr_len_actual, new_hdr_len,2252hdr_len_delta;2253struct iphdr *iph;2254struct ip_options *opt = &IPCB(skb)->opt;2255unsigned char *cipso_ptr;22562257if (opt->cipso == 0)2258return 0;22592260/* since we are changing the packet we should make a copy */2261ret_val = skb_cow(skb, skb_headroom(skb));2262if (ret_val < 0)2263return ret_val;22642265iph = ip_hdr(skb);2266cipso_ptr = (unsigned char *)iph + opt->cipso;2267cipso_len = cipso_ptr[1];22682269hdr_len_actual = sizeof(struct iphdr) +2270cipso_v4_get_actual_opt_len((unsigned char *)(iph + 1),2271opt->optlen);2272new_hdr_len_actual = hdr_len_actual - cipso_len;2273new_hdr_len = (new_hdr_len_actual + 3) & ~3;2274hdr_len_delta = (iph->ihl << 2) - new_hdr_len;22752276/* 1. shift any options after CIPSO to the left */2277memmove(cipso_ptr, cipso_ptr + cipso_len,2278new_hdr_len_actual - opt->cipso);2279/* 2. move the whole IP header to its new place */2280memmove((unsigned char *)iph + hdr_len_delta, iph, new_hdr_len_actual);2281/* 3. adjust the skb layout */2282skb_pull(skb, hdr_len_delta);2283skb_reset_network_header(skb);2284iph = ip_hdr(skb);2285/* 4. re-fill new padding with IPOPT_END (may now be longer) */2286memset((unsigned char *)iph + new_hdr_len_actual, IPOPT_END,2287new_hdr_len - new_hdr_len_actual);22882289opt->optlen -= hdr_len_delta;2290opt->cipso = 0;2291opt->is_changed = 1;2292if (hdr_len_delta != 0) {2293iph->ihl = new_hdr_len >> 2;2294iph_set_totlen(iph, skb->len);2295}2296ip_send_check(iph);22972298return 0;2299}23002301/*2302* Setup Functions2303*/23042305/**2306* cipso_v4_init - Initialize the CIPSO module2307*2308* Description:2309* Initialize the CIPSO module and prepare it for use. Returns zero on success2310* and negative values on failure.2311*2312*/2313static int __init cipso_v4_init(void)2314{2315int ret_val;23162317ret_val = cipso_v4_cache_init();2318if (ret_val != 0)2319panic("Failed to initialize the CIPSO/IPv4 cache (%d)\n",2320ret_val);23212322return 0;2323}23242325subsys_initcall(cipso_v4_init);232623272328