/*1* NetLabel Kernel API2*3* This file defines the kernel API for the NetLabel system. The NetLabel4* system manages static and dynamic label mappings for network protocols such5* as CIPSO and RIPSO.6*7* Author: Paul Moore <[email protected]>8*9*/1011/*12* (c) Copyright Hewlett-Packard Development Company, L.P., 2006, 200813*14* This program is free software; you can redistribute it and/or modify15* it under the terms of the GNU General Public License as published by16* the Free Software Foundation; either version 2 of the License, or17* (at your option) any later version.18*19* This program is distributed in the hope that it will be useful,20* but WITHOUT ANY WARRANTY; without even the implied warranty of21* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See22* the GNU General Public License for more details.23*24* You should have received a copy of the GNU General Public License25* along with this program; if not, write to the Free Software26* Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA27*28*/2930#include <linux/init.h>31#include <linux/types.h>32#include <linux/slab.h>33#include <linux/audit.h>34#include <linux/in.h>35#include <linux/in6.h>36#include <net/ip.h>37#include <net/ipv6.h>38#include <net/netlabel.h>39#include <net/cipso_ipv4.h>40#include <asm/bug.h>41#include <asm/atomic.h>4243#include "netlabel_domainhash.h"44#include "netlabel_unlabeled.h"45#include "netlabel_cipso_v4.h"46#include "netlabel_user.h"47#include "netlabel_mgmt.h"48#include "netlabel_addrlist.h"4950/*51* Configuration Functions52*/5354/**55* netlbl_cfg_map_del - Remove a NetLabel/LSM domain mapping56* @domain: the domain mapping to remove57* @family: address family58* @addr: IP address59* @mask: IP address mask60* @audit_info: NetLabel audit information61*62* Description:63* Removes a NetLabel/LSM domain mapping. A @domain value of NULL causes the64* default domain mapping to be removed. Returns zero on success, negative65* values on failure.66*67*/68int netlbl_cfg_map_del(const char *domain,69u16 family,70const void *addr,71const void *mask,72struct netlbl_audit *audit_info)73{74if (addr == NULL && mask == NULL) {75return netlbl_domhsh_remove(domain, audit_info);76} else if (addr != NULL && mask != NULL) {77switch (family) {78case AF_INET:79return netlbl_domhsh_remove_af4(domain, addr, mask,80audit_info);81default:82return -EPFNOSUPPORT;83}84} else85return -EINVAL;86}8788/**89* netlbl_cfg_unlbl_map_add - Add a new unlabeled mapping90* @domain: the domain mapping to add91* @family: address family92* @addr: IP address93* @mask: IP address mask94* @audit_info: NetLabel audit information95*96* Description:97* Adds a new unlabeled NetLabel/LSM domain mapping. A @domain value of NULL98* causes a new default domain mapping to be added. Returns zero on success,99* negative values on failure.100*101*/102int netlbl_cfg_unlbl_map_add(const char *domain,103u16 family,104const void *addr,105const void *mask,106struct netlbl_audit *audit_info)107{108int ret_val = -ENOMEM;109struct netlbl_dom_map *entry;110struct netlbl_domaddr_map *addrmap = NULL;111struct netlbl_domaddr4_map *map4 = NULL;112struct netlbl_domaddr6_map *map6 = NULL;113const struct in_addr *addr4, *mask4;114const struct in6_addr *addr6, *mask6;115116entry = kzalloc(sizeof(*entry), GFP_ATOMIC);117if (entry == NULL)118return -ENOMEM;119if (domain != NULL) {120entry->domain = kstrdup(domain, GFP_ATOMIC);121if (entry->domain == NULL)122goto cfg_unlbl_map_add_failure;123}124125if (addr == NULL && mask == NULL)126entry->type = NETLBL_NLTYPE_UNLABELED;127else if (addr != NULL && mask != NULL) {128addrmap = kzalloc(sizeof(*addrmap), GFP_ATOMIC);129if (addrmap == NULL)130goto cfg_unlbl_map_add_failure;131INIT_LIST_HEAD(&addrmap->list4);132INIT_LIST_HEAD(&addrmap->list6);133134switch (family) {135case AF_INET:136addr4 = addr;137mask4 = mask;138map4 = kzalloc(sizeof(*map4), GFP_ATOMIC);139if (map4 == NULL)140goto cfg_unlbl_map_add_failure;141map4->type = NETLBL_NLTYPE_UNLABELED;142map4->list.addr = addr4->s_addr & mask4->s_addr;143map4->list.mask = mask4->s_addr;144map4->list.valid = 1;145ret_val = netlbl_af4list_add(&map4->list,146&addrmap->list4);147if (ret_val != 0)148goto cfg_unlbl_map_add_failure;149break;150case AF_INET6:151addr6 = addr;152mask6 = mask;153map6 = kzalloc(sizeof(*map6), GFP_ATOMIC);154if (map6 == NULL)155goto cfg_unlbl_map_add_failure;156map6->type = NETLBL_NLTYPE_UNLABELED;157ipv6_addr_copy(&map6->list.addr, addr6);158map6->list.addr.s6_addr32[0] &= mask6->s6_addr32[0];159map6->list.addr.s6_addr32[1] &= mask6->s6_addr32[1];160map6->list.addr.s6_addr32[2] &= mask6->s6_addr32[2];161map6->list.addr.s6_addr32[3] &= mask6->s6_addr32[3];162ipv6_addr_copy(&map6->list.mask, mask6);163map6->list.valid = 1;164ret_val = netlbl_af4list_add(&map4->list,165&addrmap->list4);166if (ret_val != 0)167goto cfg_unlbl_map_add_failure;168break;169default:170goto cfg_unlbl_map_add_failure;171break;172}173174entry->type_def.addrsel = addrmap;175entry->type = NETLBL_NLTYPE_ADDRSELECT;176} else {177ret_val = -EINVAL;178goto cfg_unlbl_map_add_failure;179}180181ret_val = netlbl_domhsh_add(entry, audit_info);182if (ret_val != 0)183goto cfg_unlbl_map_add_failure;184185return 0;186187cfg_unlbl_map_add_failure:188kfree(entry->domain);189kfree(entry);190kfree(addrmap);191kfree(map4);192kfree(map6);193return ret_val;194}195196197/**198* netlbl_cfg_unlbl_static_add - Adds a new static label199* @net: network namespace200* @dev_name: interface name201* @addr: IP address in network byte order (struct in[6]_addr)202* @mask: address mask in network byte order (struct in[6]_addr)203* @family: address family204* @secid: LSM secid value for the entry205* @audit_info: NetLabel audit information206*207* Description:208* Adds a new NetLabel static label to be used when protocol provided labels209* are not present on incoming traffic. If @dev_name is NULL then the default210* interface will be used. Returns zero on success, negative values on failure.211*212*/213int netlbl_cfg_unlbl_static_add(struct net *net,214const char *dev_name,215const void *addr,216const void *mask,217u16 family,218u32 secid,219struct netlbl_audit *audit_info)220{221u32 addr_len;222223switch (family) {224case AF_INET:225addr_len = sizeof(struct in_addr);226break;227case AF_INET6:228addr_len = sizeof(struct in6_addr);229break;230default:231return -EPFNOSUPPORT;232}233234return netlbl_unlhsh_add(net,235dev_name, addr, mask, addr_len,236secid, audit_info);237}238239/**240* netlbl_cfg_unlbl_static_del - Removes an existing static label241* @net: network namespace242* @dev_name: interface name243* @addr: IP address in network byte order (struct in[6]_addr)244* @mask: address mask in network byte order (struct in[6]_addr)245* @family: address family246* @secid: LSM secid value for the entry247* @audit_info: NetLabel audit information248*249* Description:250* Removes an existing NetLabel static label used when protocol provided labels251* are not present on incoming traffic. If @dev_name is NULL then the default252* interface will be used. Returns zero on success, negative values on failure.253*254*/255int netlbl_cfg_unlbl_static_del(struct net *net,256const char *dev_name,257const void *addr,258const void *mask,259u16 family,260struct netlbl_audit *audit_info)261{262u32 addr_len;263264switch (family) {265case AF_INET:266addr_len = sizeof(struct in_addr);267break;268case AF_INET6:269addr_len = sizeof(struct in6_addr);270break;271default:272return -EPFNOSUPPORT;273}274275return netlbl_unlhsh_remove(net,276dev_name, addr, mask, addr_len,277audit_info);278}279280/**281* netlbl_cfg_cipsov4_add - Add a new CIPSOv4 DOI definition282* @doi_def: CIPSO DOI definition283* @audit_info: NetLabel audit information284*285* Description:286* Add a new CIPSO DOI definition as defined by @doi_def. Returns zero on287* success and negative values on failure.288*289*/290int netlbl_cfg_cipsov4_add(struct cipso_v4_doi *doi_def,291struct netlbl_audit *audit_info)292{293return cipso_v4_doi_add(doi_def, audit_info);294}295296/**297* netlbl_cfg_cipsov4_del - Remove an existing CIPSOv4 DOI definition298* @doi: CIPSO DOI299* @audit_info: NetLabel audit information300*301* Description:302* Remove an existing CIPSO DOI definition matching @doi. Returns zero on303* success and negative values on failure.304*305*/306void netlbl_cfg_cipsov4_del(u32 doi, struct netlbl_audit *audit_info)307{308cipso_v4_doi_remove(doi, audit_info);309}310311/**312* netlbl_cfg_cipsov4_map_add - Add a new CIPSOv4 DOI mapping313* @doi: the CIPSO DOI314* @domain: the domain mapping to add315* @addr: IP address316* @mask: IP address mask317* @audit_info: NetLabel audit information318*319* Description:320* Add a new NetLabel/LSM domain mapping for the given CIPSO DOI to the NetLabel321* subsystem. A @domain value of NULL adds a new default domain mapping.322* Returns zero on success, negative values on failure.323*324*/325int netlbl_cfg_cipsov4_map_add(u32 doi,326const char *domain,327const struct in_addr *addr,328const struct in_addr *mask,329struct netlbl_audit *audit_info)330{331int ret_val = -ENOMEM;332struct cipso_v4_doi *doi_def;333struct netlbl_dom_map *entry;334struct netlbl_domaddr_map *addrmap = NULL;335struct netlbl_domaddr4_map *addrinfo = NULL;336337doi_def = cipso_v4_doi_getdef(doi);338if (doi_def == NULL)339return -ENOENT;340341entry = kzalloc(sizeof(*entry), GFP_ATOMIC);342if (entry == NULL)343return -ENOMEM;344if (domain != NULL) {345entry->domain = kstrdup(domain, GFP_ATOMIC);346if (entry->domain == NULL)347goto cfg_cipsov4_map_add_failure;348}349350if (addr == NULL && mask == NULL) {351entry->type_def.cipsov4 = doi_def;352entry->type = NETLBL_NLTYPE_CIPSOV4;353} else if (addr != NULL && mask != NULL) {354addrmap = kzalloc(sizeof(*addrmap), GFP_ATOMIC);355if (addrmap == NULL)356goto cfg_cipsov4_map_add_failure;357INIT_LIST_HEAD(&addrmap->list4);358INIT_LIST_HEAD(&addrmap->list6);359360addrinfo = kzalloc(sizeof(*addrinfo), GFP_ATOMIC);361if (addrinfo == NULL)362goto cfg_cipsov4_map_add_failure;363addrinfo->type_def.cipsov4 = doi_def;364addrinfo->type = NETLBL_NLTYPE_CIPSOV4;365addrinfo->list.addr = addr->s_addr & mask->s_addr;366addrinfo->list.mask = mask->s_addr;367addrinfo->list.valid = 1;368ret_val = netlbl_af4list_add(&addrinfo->list, &addrmap->list4);369if (ret_val != 0)370goto cfg_cipsov4_map_add_failure;371372entry->type_def.addrsel = addrmap;373entry->type = NETLBL_NLTYPE_ADDRSELECT;374} else {375ret_val = -EINVAL;376goto cfg_cipsov4_map_add_failure;377}378379ret_val = netlbl_domhsh_add(entry, audit_info);380if (ret_val != 0)381goto cfg_cipsov4_map_add_failure;382383return 0;384385cfg_cipsov4_map_add_failure:386cipso_v4_doi_putdef(doi_def);387kfree(entry->domain);388kfree(entry);389kfree(addrmap);390kfree(addrinfo);391return ret_val;392}393394/*395* Security Attribute Functions396*/397398/**399* netlbl_secattr_catmap_walk - Walk a LSM secattr catmap looking for a bit400* @catmap: the category bitmap401* @offset: the offset to start searching at, in bits402*403* Description:404* This function walks a LSM secattr category bitmap starting at @offset and405* returns the spot of the first set bit or -ENOENT if no bits are set.406*407*/408int netlbl_secattr_catmap_walk(struct netlbl_lsm_secattr_catmap *catmap,409u32 offset)410{411struct netlbl_lsm_secattr_catmap *iter = catmap;412u32 node_idx;413u32 node_bit;414NETLBL_CATMAP_MAPTYPE bitmap;415416if (offset > iter->startbit) {417while (offset >= (iter->startbit + NETLBL_CATMAP_SIZE)) {418iter = iter->next;419if (iter == NULL)420return -ENOENT;421}422node_idx = (offset - iter->startbit) / NETLBL_CATMAP_MAPSIZE;423node_bit = offset - iter->startbit -424(NETLBL_CATMAP_MAPSIZE * node_idx);425} else {426node_idx = 0;427node_bit = 0;428}429bitmap = iter->bitmap[node_idx] >> node_bit;430431for (;;) {432if (bitmap != 0) {433while ((bitmap & NETLBL_CATMAP_BIT) == 0) {434bitmap >>= 1;435node_bit++;436}437return iter->startbit +438(NETLBL_CATMAP_MAPSIZE * node_idx) + node_bit;439}440if (++node_idx >= NETLBL_CATMAP_MAPCNT) {441if (iter->next != NULL) {442iter = iter->next;443node_idx = 0;444} else445return -ENOENT;446}447bitmap = iter->bitmap[node_idx];448node_bit = 0;449}450451return -ENOENT;452}453454/**455* netlbl_secattr_catmap_walk_rng - Find the end of a string of set bits456* @catmap: the category bitmap457* @offset: the offset to start searching at, in bits458*459* Description:460* This function walks a LSM secattr category bitmap starting at @offset and461* returns the spot of the first cleared bit or -ENOENT if the offset is past462* the end of the bitmap.463*464*/465int netlbl_secattr_catmap_walk_rng(struct netlbl_lsm_secattr_catmap *catmap,466u32 offset)467{468struct netlbl_lsm_secattr_catmap *iter = catmap;469u32 node_idx;470u32 node_bit;471NETLBL_CATMAP_MAPTYPE bitmask;472NETLBL_CATMAP_MAPTYPE bitmap;473474if (offset > iter->startbit) {475while (offset >= (iter->startbit + NETLBL_CATMAP_SIZE)) {476iter = iter->next;477if (iter == NULL)478return -ENOENT;479}480node_idx = (offset - iter->startbit) / NETLBL_CATMAP_MAPSIZE;481node_bit = offset - iter->startbit -482(NETLBL_CATMAP_MAPSIZE * node_idx);483} else {484node_idx = 0;485node_bit = 0;486}487bitmask = NETLBL_CATMAP_BIT << node_bit;488489for (;;) {490bitmap = iter->bitmap[node_idx];491while (bitmask != 0 && (bitmap & bitmask) != 0) {492bitmask <<= 1;493node_bit++;494}495496if (bitmask != 0)497return iter->startbit +498(NETLBL_CATMAP_MAPSIZE * node_idx) +499node_bit - 1;500else if (++node_idx >= NETLBL_CATMAP_MAPCNT) {501if (iter->next == NULL)502return iter->startbit + NETLBL_CATMAP_SIZE - 1;503iter = iter->next;504node_idx = 0;505}506bitmask = NETLBL_CATMAP_BIT;507node_bit = 0;508}509510return -ENOENT;511}512513/**514* netlbl_secattr_catmap_setbit - Set a bit in a LSM secattr catmap515* @catmap: the category bitmap516* @bit: the bit to set517* @flags: memory allocation flags518*519* Description:520* Set the bit specified by @bit in @catmap. Returns zero on success,521* negative values on failure.522*523*/524int netlbl_secattr_catmap_setbit(struct netlbl_lsm_secattr_catmap *catmap,525u32 bit,526gfp_t flags)527{528struct netlbl_lsm_secattr_catmap *iter = catmap;529u32 node_bit;530u32 node_idx;531532while (iter->next != NULL &&533bit >= (iter->startbit + NETLBL_CATMAP_SIZE))534iter = iter->next;535if (bit >= (iter->startbit + NETLBL_CATMAP_SIZE)) {536iter->next = netlbl_secattr_catmap_alloc(flags);537if (iter->next == NULL)538return -ENOMEM;539iter = iter->next;540iter->startbit = bit & ~(NETLBL_CATMAP_SIZE - 1);541}542543/* gcc always rounds to zero when doing integer division */544node_idx = (bit - iter->startbit) / NETLBL_CATMAP_MAPSIZE;545node_bit = bit - iter->startbit - (NETLBL_CATMAP_MAPSIZE * node_idx);546iter->bitmap[node_idx] |= NETLBL_CATMAP_BIT << node_bit;547548return 0;549}550551/**552* netlbl_secattr_catmap_setrng - Set a range of bits in a LSM secattr catmap553* @catmap: the category bitmap554* @start: the starting bit555* @end: the last bit in the string556* @flags: memory allocation flags557*558* Description:559* Set a range of bits, starting at @start and ending with @end. Returns zero560* on success, negative values on failure.561*562*/563int netlbl_secattr_catmap_setrng(struct netlbl_lsm_secattr_catmap *catmap,564u32 start,565u32 end,566gfp_t flags)567{568int ret_val = 0;569struct netlbl_lsm_secattr_catmap *iter = catmap;570u32 iter_max_spot;571u32 spot;572573/* XXX - This could probably be made a bit faster by combining writes574* to the catmap instead of setting a single bit each time, but for575* right now skipping to the start of the range in the catmap should576* be a nice improvement over calling the individual setbit function577* repeatedly from a loop. */578579while (iter->next != NULL &&580start >= (iter->startbit + NETLBL_CATMAP_SIZE))581iter = iter->next;582iter_max_spot = iter->startbit + NETLBL_CATMAP_SIZE;583584for (spot = start; spot <= end && ret_val == 0; spot++) {585if (spot >= iter_max_spot && iter->next != NULL) {586iter = iter->next;587iter_max_spot = iter->startbit + NETLBL_CATMAP_SIZE;588}589ret_val = netlbl_secattr_catmap_setbit(iter, spot, GFP_ATOMIC);590}591592return ret_val;593}594595/*596* LSM Functions597*/598599/**600* netlbl_enabled - Determine if the NetLabel subsystem is enabled601*602* Description:603* The LSM can use this function to determine if it should use NetLabel604* security attributes in it's enforcement mechanism. Currently, NetLabel is605* considered to be enabled when it's configuration contains a valid setup for606* at least one labeled protocol (i.e. NetLabel can understand incoming607* labeled packets of at least one type); otherwise NetLabel is considered to608* be disabled.609*610*/611int netlbl_enabled(void)612{613/* At some point we probably want to expose this mechanism to the user614* as well so that admins can toggle NetLabel regardless of the615* configuration */616return (atomic_read(&netlabel_mgmt_protocount) > 0);617}618619/**620* netlbl_sock_setattr - Label a socket using the correct protocol621* @sk: the socket to label622* @family: protocol family623* @secattr: the security attributes624*625* Description:626* Attach the correct label to the given socket using the security attributes627* specified in @secattr. This function requires exclusive access to @sk,628* which means it either needs to be in the process of being created or locked.629* Returns zero on success, -EDESTADDRREQ if the domain is configured to use630* network address selectors (can't blindly label the socket), and negative631* values on all other failures.632*633*/634int netlbl_sock_setattr(struct sock *sk,635u16 family,636const struct netlbl_lsm_secattr *secattr)637{638int ret_val;639struct netlbl_dom_map *dom_entry;640641rcu_read_lock();642dom_entry = netlbl_domhsh_getentry(secattr->domain);643if (dom_entry == NULL) {644ret_val = -ENOENT;645goto socket_setattr_return;646}647switch (family) {648case AF_INET:649switch (dom_entry->type) {650case NETLBL_NLTYPE_ADDRSELECT:651ret_val = -EDESTADDRREQ;652break;653case NETLBL_NLTYPE_CIPSOV4:654ret_val = cipso_v4_sock_setattr(sk,655dom_entry->type_def.cipsov4,656secattr);657break;658case NETLBL_NLTYPE_UNLABELED:659ret_val = 0;660break;661default:662ret_val = -ENOENT;663}664break;665#if defined(CONFIG_IPV6) || defined(CONFIG_IPV6_MODULE)666case AF_INET6:667/* since we don't support any IPv6 labeling protocols right668* now we can optimize everything away until we do */669ret_val = 0;670break;671#endif /* IPv6 */672default:673ret_val = -EPROTONOSUPPORT;674}675676socket_setattr_return:677rcu_read_unlock();678return ret_val;679}680681/**682* netlbl_sock_delattr - Delete all the NetLabel labels on a socket683* @sk: the socket684*685* Description:686* Remove all the NetLabel labeling from @sk. The caller is responsible for687* ensuring that @sk is locked.688*689*/690void netlbl_sock_delattr(struct sock *sk)691{692cipso_v4_sock_delattr(sk);693}694695/**696* netlbl_sock_getattr - Determine the security attributes of a sock697* @sk: the sock698* @secattr: the security attributes699*700* Description:701* Examines the given sock to see if any NetLabel style labeling has been702* applied to the sock, if so it parses the socket label and returns the703* security attributes in @secattr. Returns zero on success, negative values704* on failure.705*706*/707int netlbl_sock_getattr(struct sock *sk,708struct netlbl_lsm_secattr *secattr)709{710int ret_val;711712switch (sk->sk_family) {713case AF_INET:714ret_val = cipso_v4_sock_getattr(sk, secattr);715break;716#if defined(CONFIG_IPV6) || defined(CONFIG_IPV6_MODULE)717case AF_INET6:718ret_val = -ENOMSG;719break;720#endif /* IPv6 */721default:722ret_val = -EPROTONOSUPPORT;723}724725return ret_val;726}727728/**729* netlbl_conn_setattr - Label a connected socket using the correct protocol730* @sk: the socket to label731* @addr: the destination address732* @secattr: the security attributes733*734* Description:735* Attach the correct label to the given connected socket using the security736* attributes specified in @secattr. The caller is responsible for ensuring737* that @sk is locked. Returns zero on success, negative values on failure.738*739*/740int netlbl_conn_setattr(struct sock *sk,741struct sockaddr *addr,742const struct netlbl_lsm_secattr *secattr)743{744int ret_val;745struct sockaddr_in *addr4;746struct netlbl_domaddr4_map *af4_entry;747748rcu_read_lock();749switch (addr->sa_family) {750case AF_INET:751addr4 = (struct sockaddr_in *)addr;752af4_entry = netlbl_domhsh_getentry_af4(secattr->domain,753addr4->sin_addr.s_addr);754if (af4_entry == NULL) {755ret_val = -ENOENT;756goto conn_setattr_return;757}758switch (af4_entry->type) {759case NETLBL_NLTYPE_CIPSOV4:760ret_val = cipso_v4_sock_setattr(sk,761af4_entry->type_def.cipsov4,762secattr);763break;764case NETLBL_NLTYPE_UNLABELED:765/* just delete the protocols we support for right now766* but we could remove other protocols if needed */767cipso_v4_sock_delattr(sk);768ret_val = 0;769break;770default:771ret_val = -ENOENT;772}773break;774#if defined(CONFIG_IPV6) || defined(CONFIG_IPV6_MODULE)775case AF_INET6:776/* since we don't support any IPv6 labeling protocols right777* now we can optimize everything away until we do */778ret_val = 0;779break;780#endif /* IPv6 */781default:782ret_val = -EPROTONOSUPPORT;783}784785conn_setattr_return:786rcu_read_unlock();787return ret_val;788}789790/**791* netlbl_req_setattr - Label a request socket using the correct protocol792* @req: the request socket to label793* @secattr: the security attributes794*795* Description:796* Attach the correct label to the given socket using the security attributes797* specified in @secattr. Returns zero on success, negative values on failure.798*799*/800int netlbl_req_setattr(struct request_sock *req,801const struct netlbl_lsm_secattr *secattr)802{803int ret_val;804struct netlbl_dom_map *dom_entry;805struct netlbl_domaddr4_map *af4_entry;806u32 proto_type;807struct cipso_v4_doi *proto_cv4;808809rcu_read_lock();810dom_entry = netlbl_domhsh_getentry(secattr->domain);811if (dom_entry == NULL) {812ret_val = -ENOENT;813goto req_setattr_return;814}815switch (req->rsk_ops->family) {816case AF_INET:817if (dom_entry->type == NETLBL_NLTYPE_ADDRSELECT) {818struct inet_request_sock *req_inet = inet_rsk(req);819af4_entry = netlbl_domhsh_getentry_af4(secattr->domain,820req_inet->rmt_addr);821if (af4_entry == NULL) {822ret_val = -ENOENT;823goto req_setattr_return;824}825proto_type = af4_entry->type;826proto_cv4 = af4_entry->type_def.cipsov4;827} else {828proto_type = dom_entry->type;829proto_cv4 = dom_entry->type_def.cipsov4;830}831switch (proto_type) {832case NETLBL_NLTYPE_CIPSOV4:833ret_val = cipso_v4_req_setattr(req, proto_cv4, secattr);834break;835case NETLBL_NLTYPE_UNLABELED:836/* just delete the protocols we support for right now837* but we could remove other protocols if needed */838cipso_v4_req_delattr(req);839ret_val = 0;840break;841default:842ret_val = -ENOENT;843}844break;845#if defined(CONFIG_IPV6) || defined(CONFIG_IPV6_MODULE)846case AF_INET6:847/* since we don't support any IPv6 labeling protocols right848* now we can optimize everything away until we do */849ret_val = 0;850break;851#endif /* IPv6 */852default:853ret_val = -EPROTONOSUPPORT;854}855856req_setattr_return:857rcu_read_unlock();858return ret_val;859}860861/**862* netlbl_req_delattr - Delete all the NetLabel labels on a socket863* @req: the socket864*865* Description:866* Remove all the NetLabel labeling from @req.867*868*/869void netlbl_req_delattr(struct request_sock *req)870{871cipso_v4_req_delattr(req);872}873874/**875* netlbl_skbuff_setattr - Label a packet using the correct protocol876* @skb: the packet877* @family: protocol family878* @secattr: the security attributes879*880* Description:881* Attach the correct label to the given packet using the security attributes882* specified in @secattr. Returns zero on success, negative values on failure.883*884*/885int netlbl_skbuff_setattr(struct sk_buff *skb,886u16 family,887const struct netlbl_lsm_secattr *secattr)888{889int ret_val;890struct iphdr *hdr4;891struct netlbl_domaddr4_map *af4_entry;892893rcu_read_lock();894switch (family) {895case AF_INET:896hdr4 = ip_hdr(skb);897af4_entry = netlbl_domhsh_getentry_af4(secattr->domain,898hdr4->daddr);899if (af4_entry == NULL) {900ret_val = -ENOENT;901goto skbuff_setattr_return;902}903switch (af4_entry->type) {904case NETLBL_NLTYPE_CIPSOV4:905ret_val = cipso_v4_skbuff_setattr(skb,906af4_entry->type_def.cipsov4,907secattr);908break;909case NETLBL_NLTYPE_UNLABELED:910/* just delete the protocols we support for right now911* but we could remove other protocols if needed */912ret_val = cipso_v4_skbuff_delattr(skb);913break;914default:915ret_val = -ENOENT;916}917break;918#if defined(CONFIG_IPV6) || defined(CONFIG_IPV6_MODULE)919case AF_INET6:920/* since we don't support any IPv6 labeling protocols right921* now we can optimize everything away until we do */922ret_val = 0;923break;924#endif /* IPv6 */925default:926ret_val = -EPROTONOSUPPORT;927}928929skbuff_setattr_return:930rcu_read_unlock();931return ret_val;932}933934/**935* netlbl_skbuff_getattr - Determine the security attributes of a packet936* @skb: the packet937* @family: protocol family938* @secattr: the security attributes939*940* Description:941* Examines the given packet to see if a recognized form of packet labeling942* is present, if so it parses the packet label and returns the security943* attributes in @secattr. Returns zero on success, negative values on944* failure.945*946*/947int netlbl_skbuff_getattr(const struct sk_buff *skb,948u16 family,949struct netlbl_lsm_secattr *secattr)950{951switch (family) {952case AF_INET:953if (CIPSO_V4_OPTEXIST(skb) &&954cipso_v4_skbuff_getattr(skb, secattr) == 0)955return 0;956break;957#if defined(CONFIG_IPV6) || defined(CONFIG_IPV6_MODULE)958case AF_INET6:959break;960#endif /* IPv6 */961}962963return netlbl_unlabel_getattr(skb, family, secattr);964}965966/**967* netlbl_skbuff_err - Handle a LSM error on a sk_buff968* @skb: the packet969* @error: the error code970* @gateway: true if host is acting as a gateway, false otherwise971*972* Description:973* Deal with a LSM problem when handling the packet in @skb, typically this is974* a permission denied problem (-EACCES). The correct action is determined975* according to the packet's labeling protocol.976*977*/978void netlbl_skbuff_err(struct sk_buff *skb, int error, int gateway)979{980if (CIPSO_V4_OPTEXIST(skb))981cipso_v4_error(skb, error, gateway);982}983984/**985* netlbl_cache_invalidate - Invalidate all of the NetLabel protocol caches986*987* Description:988* For all of the NetLabel protocols that support some form of label mapping989* cache, invalidate the cache. Returns zero on success, negative values on990* error.991*992*/993void netlbl_cache_invalidate(void)994{995cipso_v4_cache_invalidate();996}997998/**999* netlbl_cache_add - Add an entry to a NetLabel protocol cache1000* @skb: the packet1001* @secattr: the packet's security attributes1002*1003* Description:1004* Add the LSM security attributes for the given packet to the underlying1005* NetLabel protocol's label mapping cache. Returns zero on success, negative1006* values on error.1007*1008*/1009int netlbl_cache_add(const struct sk_buff *skb,1010const struct netlbl_lsm_secattr *secattr)1011{1012if ((secattr->flags & NETLBL_SECATTR_CACHE) == 0)1013return -ENOMSG;10141015if (CIPSO_V4_OPTEXIST(skb))1016return cipso_v4_cache_add(skb, secattr);10171018return -ENOMSG;1019}10201021/*1022* Protocol Engine Functions1023*/10241025/**1026* netlbl_audit_start - Start an audit message1027* @type: audit message type1028* @audit_info: NetLabel audit information1029*1030* Description:1031* Start an audit message using the type specified in @type and fill the audit1032* message with some fields common to all NetLabel audit messages. This1033* function should only be used by protocol engines, not LSMs. Returns a1034* pointer to the audit buffer on success, NULL on failure.1035*1036*/1037struct audit_buffer *netlbl_audit_start(int type,1038struct netlbl_audit *audit_info)1039{1040return netlbl_audit_start_common(type, audit_info);1041}10421043/*1044* Setup Functions1045*/10461047/**1048* netlbl_init - Initialize NetLabel1049*1050* Description:1051* Perform the required NetLabel initialization before first use.1052*1053*/1054static int __init netlbl_init(void)1055{1056int ret_val;10571058printk(KERN_INFO "NetLabel: Initializing\n");1059printk(KERN_INFO "NetLabel: domain hash size = %u\n",1060(1 << NETLBL_DOMHSH_BITSIZE));1061printk(KERN_INFO "NetLabel: protocols ="1062" UNLABELED"1063" CIPSOv4"1064"\n");10651066ret_val = netlbl_domhsh_init(NETLBL_DOMHSH_BITSIZE);1067if (ret_val != 0)1068goto init_failure;10691070ret_val = netlbl_unlabel_init(NETLBL_UNLHSH_BITSIZE);1071if (ret_val != 0)1072goto init_failure;10731074ret_val = netlbl_netlink_init();1075if (ret_val != 0)1076goto init_failure;10771078ret_val = netlbl_unlabel_defconf();1079if (ret_val != 0)1080goto init_failure;1081printk(KERN_INFO "NetLabel: unlabeled traffic allowed by default\n");10821083return 0;10841085init_failure:1086panic("NetLabel: failed to initialize properly (%d)\n", ret_val);1087}10881089subsys_initcall(netlbl_init);109010911092