cipso_ipv4.c 61 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583158415851586158715881589159015911592159315941595159615971598159916001601160216031604160516061607160816091610161116121613161416151616161716181619162016211622162316241625162616271628162916301631163216331634163516361637163816391640164116421643164416451646164716481649165016511652165316541655165616571658165916601661166216631664166516661667166816691670167116721673167416751676167716781679168016811682168316841685168616871688168916901691169216931694169516961697169816991700170117021703170417051706170717081709171017111712171317141715171617171718171917201721172217231724172517261727172817291730173117321733173417351736173717381739174017411742174317441745174617471748174917501751175217531754175517561757175817591760176117621763176417651766176717681769177017711772177317741775177617771778177917801781178217831784178517861787178817891790179117921793179417951796179717981799180018011802180318041805180618071808180918101811181218131814181518161817181818191820182118221823182418251826182718281829183018311832183318341835183618371838183918401841184218431844184518461847184818491850185118521853185418551856185718581859186018611862186318641865186618671868186918701871187218731874187518761877187818791880188118821883188418851886188718881889189018911892189318941895189618971898189919001901190219031904190519061907190819091910191119121913191419151916191719181919192019211922192319241925192619271928192919301931193219331934193519361937193819391940194119421943194419451946194719481949195019511952195319541955195619571958195919601961196219631964196519661967196819691970197119721973197419751976197719781979198019811982198319841985198619871988198919901991199219931994199519961997199819992000200120022003200420052006200720082009201020112012201320142015201620172018201920202021202220232024202520262027202820292030203120322033203420352036203720382039204020412042204320442045204620472048204920502051205220532054205520562057205820592060206120622063206420652066206720682069207020712072207320742075207620772078207920802081208220832084208520862087208820892090209120922093209420952096209720982099210021012102210321042105210621072108210921102111211221132114211521162117211821192120212121222123212421252126212721282129213021312132213321342135213621372138213921402141214221432144214521462147214821492150215121522153215421552156215721582159216021612162216321642165216621672168216921702171217221732174217521762177217821792180218121822183218421852186218721882189219021912192219321942195219621972198219922002201220222032204220522062207220822092210221122122213221422152216221722182219222022212222222322242225222622272228222922302231223222332234223522362237223822392240224122422243224422452246224722482249225022512252225322542255225622572258225922602261226222632264226522662267226822692270227122722273227422752276227722782279228022812282228322842285228622872288
  1. /*
  2. * CIPSO - Commercial IP Security Option
  3. *
  4. * This is an implementation of the CIPSO 2.2 protocol as specified in
  5. * draft-ietf-cipso-ipsecurity-01.txt with additional tag types as found in
  6. * FIPS-188. While CIPSO never became a full IETF RFC standard many vendors
  7. * have chosen to adopt the protocol and over the years it has become a
  8. * de-facto standard for labeled networking.
  9. *
  10. * The CIPSO draft specification can be found in the kernel's Documentation
  11. * directory as well as the following URL:
  12. * http://tools.ietf.org/id/draft-ietf-cipso-ipsecurity-01.txt
  13. * The FIPS-188 specification can be found at the following URL:
  14. * http://www.itl.nist.gov/fipspubs/fip188.htm
  15. *
  16. * Author: Paul Moore <paul.moore@hp.com>
  17. *
  18. */
  19. /*
  20. * (c) Copyright Hewlett-Packard Development Company, L.P., 2006, 2008
  21. *
  22. * This program is free software; you can redistribute it and/or modify
  23. * it under the terms of the GNU General Public License as published by
  24. * the Free Software Foundation; either version 2 of the License, or
  25. * (at your option) any later version.
  26. *
  27. * This program is distributed in the hope that it will be useful,
  28. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  29. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See
  30. * the GNU General Public License for more details.
  31. *
  32. * You should have received a copy of the GNU General Public License
  33. * along with this program; if not, see <http://www.gnu.org/licenses/>.
  34. *
  35. */
  36. #include <linux/init.h>
  37. #include <linux/types.h>
  38. #include <linux/rcupdate.h>
  39. #include <linux/list.h>
  40. #include <linux/spinlock.h>
  41. #include <linux/string.h>
  42. #include <linux/jhash.h>
  43. #include <linux/audit.h>
  44. #include <linux/slab.h>
  45. #include <net/ip.h>
  46. #include <net/icmp.h>
  47. #include <net/tcp.h>
  48. #include <net/netlabel.h>
  49. #include <net/cipso_ipv4.h>
  50. #include <linux/atomic.h>
  51. #include <linux/bug.h>
  52. #include <asm/unaligned.h>
  53. /* List of available DOI definitions */
  54. /* XXX - This currently assumes a minimal number of different DOIs in use,
  55. * if in practice there are a lot of different DOIs this list should
  56. * probably be turned into a hash table or something similar so we
  57. * can do quick lookups. */
  58. static DEFINE_SPINLOCK(cipso_v4_doi_list_lock);
  59. static LIST_HEAD(cipso_v4_doi_list);
  60. /* Label mapping cache */
  61. int cipso_v4_cache_enabled = 1;
  62. int cipso_v4_cache_bucketsize = 10;
  63. #define CIPSO_V4_CACHE_BUCKETBITS 7
  64. #define CIPSO_V4_CACHE_BUCKETS (1 << CIPSO_V4_CACHE_BUCKETBITS)
  65. #define CIPSO_V4_CACHE_REORDERLIMIT 10
  66. struct cipso_v4_map_cache_bkt {
  67. spinlock_t lock;
  68. u32 size;
  69. struct list_head list;
  70. };
  71. struct cipso_v4_map_cache_entry {
  72. u32 hash;
  73. unsigned char *key;
  74. size_t key_len;
  75. struct netlbl_lsm_cache *lsm_data;
  76. u32 activity;
  77. struct list_head list;
  78. };
  79. static struct cipso_v4_map_cache_bkt *cipso_v4_cache;
  80. /* Restricted bitmap (tag #1) flags */
  81. int cipso_v4_rbm_optfmt = 0;
  82. int cipso_v4_rbm_strictvalid = 1;
  83. /*
  84. * Protocol Constants
  85. */
  86. /* Maximum size of the CIPSO IP option, derived from the fact that the maximum
  87. * IPv4 header size is 60 bytes and the base IPv4 header is 20 bytes long. */
  88. #define CIPSO_V4_OPT_LEN_MAX 40
  89. /* Length of the base CIPSO option, this includes the option type (1 byte), the
  90. * option length (1 byte), and the DOI (4 bytes). */
  91. #define CIPSO_V4_HDR_LEN 6
  92. /* Base length of the restrictive category bitmap tag (tag #1). */
  93. #define CIPSO_V4_TAG_RBM_BLEN 4
  94. /* Base length of the enumerated category tag (tag #2). */
  95. #define CIPSO_V4_TAG_ENUM_BLEN 4
  96. /* Base length of the ranged categories bitmap tag (tag #5). */
  97. #define CIPSO_V4_TAG_RNG_BLEN 4
  98. /* The maximum number of category ranges permitted in the ranged category tag
  99. * (tag #5). You may note that the IETF draft states that the maximum number
  100. * of category ranges is 7, but if the low end of the last category range is
  101. * zero then it is possible to fit 8 category ranges because the zero should
  102. * be omitted. */
  103. #define CIPSO_V4_TAG_RNG_CAT_MAX 8
  104. /* Base length of the local tag (non-standard tag).
  105. * Tag definition (may change between kernel versions)
  106. *
  107. * 0 8 16 24 32
  108. * +----------+----------+----------+----------+
  109. * | 10000000 | 00000110 | 32-bit secid value |
  110. * +----------+----------+----------+----------+
  111. * | in (host byte order)|
  112. * +----------+----------+
  113. *
  114. */
  115. #define CIPSO_V4_TAG_LOC_BLEN 6
  116. /*
  117. * Helper Functions
  118. */
  119. /**
  120. * cipso_v4_cache_entry_free - Frees a cache entry
  121. * @entry: the entry to free
  122. *
  123. * Description:
  124. * This function frees the memory associated with a cache entry including the
  125. * LSM cache data if there are no longer any users, i.e. reference count == 0.
  126. *
  127. */
  128. static void cipso_v4_cache_entry_free(struct cipso_v4_map_cache_entry *entry)
  129. {
  130. if (entry->lsm_data)
  131. netlbl_secattr_cache_free(entry->lsm_data);
  132. kfree(entry->key);
  133. kfree(entry);
  134. }
  135. /**
  136. * cipso_v4_map_cache_hash - Hashing function for the CIPSO cache
  137. * @key: the hash key
  138. * @key_len: the length of the key in bytes
  139. *
  140. * Description:
  141. * The CIPSO tag hashing function. Returns a 32-bit hash value.
  142. *
  143. */
  144. static u32 cipso_v4_map_cache_hash(const unsigned char *key, u32 key_len)
  145. {
  146. return jhash(key, key_len, 0);
  147. }
  148. /*
  149. * Label Mapping Cache Functions
  150. */
  151. /**
  152. * cipso_v4_cache_init - Initialize the CIPSO cache
  153. *
  154. * Description:
  155. * Initializes the CIPSO label mapping cache, this function should be called
  156. * before any of the other functions defined in this file. Returns zero on
  157. * success, negative values on error.
  158. *
  159. */
  160. static int __init cipso_v4_cache_init(void)
  161. {
  162. u32 iter;
  163. cipso_v4_cache = kcalloc(CIPSO_V4_CACHE_BUCKETS,
  164. sizeof(struct cipso_v4_map_cache_bkt),
  165. GFP_KERNEL);
  166. if (!cipso_v4_cache)
  167. return -ENOMEM;
  168. for (iter = 0; iter < CIPSO_V4_CACHE_BUCKETS; iter++) {
  169. spin_lock_init(&cipso_v4_cache[iter].lock);
  170. cipso_v4_cache[iter].size = 0;
  171. INIT_LIST_HEAD(&cipso_v4_cache[iter].list);
  172. }
  173. return 0;
  174. }
  175. /**
  176. * cipso_v4_cache_invalidate - Invalidates the current CIPSO cache
  177. *
  178. * Description:
  179. * Invalidates and frees any entries in the CIPSO cache. Returns zero on
  180. * success and negative values on failure.
  181. *
  182. */
  183. void cipso_v4_cache_invalidate(void)
  184. {
  185. struct cipso_v4_map_cache_entry *entry, *tmp_entry;
  186. u32 iter;
  187. for (iter = 0; iter < CIPSO_V4_CACHE_BUCKETS; iter++) {
  188. spin_lock_bh(&cipso_v4_cache[iter].lock);
  189. list_for_each_entry_safe(entry,
  190. tmp_entry,
  191. &cipso_v4_cache[iter].list, list) {
  192. list_del(&entry->list);
  193. cipso_v4_cache_entry_free(entry);
  194. }
  195. cipso_v4_cache[iter].size = 0;
  196. spin_unlock_bh(&cipso_v4_cache[iter].lock);
  197. }
  198. }
  199. /**
  200. * cipso_v4_cache_check - Check the CIPSO cache for a label mapping
  201. * @key: the buffer to check
  202. * @key_len: buffer length in bytes
  203. * @secattr: the security attribute struct to use
  204. *
  205. * Description:
  206. * This function checks the cache to see if a label mapping already exists for
  207. * the given key. If there is a match then the cache is adjusted and the
  208. * @secattr struct is populated with the correct LSM security attributes. The
  209. * cache is adjusted in the following manner if the entry is not already the
  210. * first in the cache bucket:
  211. *
  212. * 1. The cache entry's activity counter is incremented
  213. * 2. The previous (higher ranking) entry's activity counter is decremented
  214. * 3. If the difference between the two activity counters is geater than
  215. * CIPSO_V4_CACHE_REORDERLIMIT the two entries are swapped
  216. *
  217. * Returns zero on success, -ENOENT for a cache miss, and other negative values
  218. * on error.
  219. *
  220. */
  221. static int cipso_v4_cache_check(const unsigned char *key,
  222. u32 key_len,
  223. struct netlbl_lsm_secattr *secattr)
  224. {
  225. u32 bkt;
  226. struct cipso_v4_map_cache_entry *entry;
  227. struct cipso_v4_map_cache_entry *prev_entry = NULL;
  228. u32 hash;
  229. if (!cipso_v4_cache_enabled)
  230. return -ENOENT;
  231. hash = cipso_v4_map_cache_hash(key, key_len);
  232. bkt = hash & (CIPSO_V4_CACHE_BUCKETS - 1);
  233. spin_lock_bh(&cipso_v4_cache[bkt].lock);
  234. list_for_each_entry(entry, &cipso_v4_cache[bkt].list, list) {
  235. if (entry->hash == hash &&
  236. entry->key_len == key_len &&
  237. memcmp(entry->key, key, key_len) == 0) {
  238. entry->activity += 1;
  239. atomic_inc(&entry->lsm_data->refcount);
  240. secattr->cache = entry->lsm_data;
  241. secattr->flags |= NETLBL_SECATTR_CACHE;
  242. secattr->type = NETLBL_NLTYPE_CIPSOV4;
  243. if (!prev_entry) {
  244. spin_unlock_bh(&cipso_v4_cache[bkt].lock);
  245. return 0;
  246. }
  247. if (prev_entry->activity > 0)
  248. prev_entry->activity -= 1;
  249. if (entry->activity > prev_entry->activity &&
  250. entry->activity - prev_entry->activity >
  251. CIPSO_V4_CACHE_REORDERLIMIT) {
  252. __list_del(entry->list.prev, entry->list.next);
  253. __list_add(&entry->list,
  254. prev_entry->list.prev,
  255. &prev_entry->list);
  256. }
  257. spin_unlock_bh(&cipso_v4_cache[bkt].lock);
  258. return 0;
  259. }
  260. prev_entry = entry;
  261. }
  262. spin_unlock_bh(&cipso_v4_cache[bkt].lock);
  263. return -ENOENT;
  264. }
  265. /**
  266. * cipso_v4_cache_add - Add an entry to the CIPSO cache
  267. * @skb: the packet
  268. * @secattr: the packet's security attributes
  269. *
  270. * Description:
  271. * Add a new entry into the CIPSO label mapping cache. Add the new entry to
  272. * head of the cache bucket's list, if the cache bucket is out of room remove
  273. * the last entry in the list first. It is important to note that there is
  274. * currently no checking for duplicate keys. Returns zero on success,
  275. * negative values on failure.
  276. *
  277. */
  278. int cipso_v4_cache_add(const unsigned char *cipso_ptr,
  279. const struct netlbl_lsm_secattr *secattr)
  280. {
  281. int ret_val = -EPERM;
  282. u32 bkt;
  283. struct cipso_v4_map_cache_entry *entry = NULL;
  284. struct cipso_v4_map_cache_entry *old_entry = NULL;
  285. u32 cipso_ptr_len;
  286. if (!cipso_v4_cache_enabled || cipso_v4_cache_bucketsize <= 0)
  287. return 0;
  288. cipso_ptr_len = cipso_ptr[1];
  289. entry = kzalloc(sizeof(*entry), GFP_ATOMIC);
  290. if (!entry)
  291. return -ENOMEM;
  292. entry->key = kmemdup(cipso_ptr, cipso_ptr_len, GFP_ATOMIC);
  293. if (!entry->key) {
  294. ret_val = -ENOMEM;
  295. goto cache_add_failure;
  296. }
  297. entry->key_len = cipso_ptr_len;
  298. entry->hash = cipso_v4_map_cache_hash(cipso_ptr, cipso_ptr_len);
  299. atomic_inc(&secattr->cache->refcount);
  300. entry->lsm_data = secattr->cache;
  301. bkt = entry->hash & (CIPSO_V4_CACHE_BUCKETS - 1);
  302. spin_lock_bh(&cipso_v4_cache[bkt].lock);
  303. if (cipso_v4_cache[bkt].size < cipso_v4_cache_bucketsize) {
  304. list_add(&entry->list, &cipso_v4_cache[bkt].list);
  305. cipso_v4_cache[bkt].size += 1;
  306. } else {
  307. old_entry = list_entry(cipso_v4_cache[bkt].list.prev,
  308. struct cipso_v4_map_cache_entry, list);
  309. list_del(&old_entry->list);
  310. list_add(&entry->list, &cipso_v4_cache[bkt].list);
  311. cipso_v4_cache_entry_free(old_entry);
  312. }
  313. spin_unlock_bh(&cipso_v4_cache[bkt].lock);
  314. return 0;
  315. cache_add_failure:
  316. if (entry)
  317. cipso_v4_cache_entry_free(entry);
  318. return ret_val;
  319. }
  320. /*
  321. * DOI List Functions
  322. */
  323. /**
  324. * cipso_v4_doi_search - Searches for a DOI definition
  325. * @doi: the DOI to search for
  326. *
  327. * Description:
  328. * Search the DOI definition list for a DOI definition with a DOI value that
  329. * matches @doi. The caller is responsible for calling rcu_read_[un]lock().
  330. * Returns a pointer to the DOI definition on success and NULL on failure.
  331. */
  332. static struct cipso_v4_doi *cipso_v4_doi_search(u32 doi)
  333. {
  334. struct cipso_v4_doi *iter;
  335. list_for_each_entry_rcu(iter, &cipso_v4_doi_list, list)
  336. if (iter->doi == doi && atomic_read(&iter->refcount))
  337. return iter;
  338. return NULL;
  339. }
  340. /**
  341. * cipso_v4_doi_add - Add a new DOI to the CIPSO protocol engine
  342. * @doi_def: the DOI structure
  343. * @audit_info: NetLabel audit information
  344. *
  345. * Description:
  346. * The caller defines a new DOI for use by the CIPSO engine and calls this
  347. * function to add it to the list of acceptable domains. The caller must
  348. * ensure that the mapping table specified in @doi_def->map meets all of the
  349. * requirements of the mapping type (see cipso_ipv4.h for details). Returns
  350. * zero on success and non-zero on failure.
  351. *
  352. */
  353. int cipso_v4_doi_add(struct cipso_v4_doi *doi_def,
  354. struct netlbl_audit *audit_info)
  355. {
  356. int ret_val = -EINVAL;
  357. u32 iter;
  358. u32 doi;
  359. u32 doi_type;
  360. struct audit_buffer *audit_buf;
  361. doi = doi_def->doi;
  362. doi_type = doi_def->type;
  363. if (doi_def->doi == CIPSO_V4_DOI_UNKNOWN)
  364. goto doi_add_return;
  365. for (iter = 0; iter < CIPSO_V4_TAG_MAXCNT; iter++) {
  366. switch (doi_def->tags[iter]) {
  367. case CIPSO_V4_TAG_RBITMAP:
  368. break;
  369. case CIPSO_V4_TAG_RANGE:
  370. case CIPSO_V4_TAG_ENUM:
  371. if (doi_def->type != CIPSO_V4_MAP_PASS)
  372. goto doi_add_return;
  373. break;
  374. case CIPSO_V4_TAG_LOCAL:
  375. if (doi_def->type != CIPSO_V4_MAP_LOCAL)
  376. goto doi_add_return;
  377. break;
  378. case CIPSO_V4_TAG_INVALID:
  379. if (iter == 0)
  380. goto doi_add_return;
  381. break;
  382. default:
  383. goto doi_add_return;
  384. }
  385. }
  386. atomic_set(&doi_def->refcount, 1);
  387. spin_lock(&cipso_v4_doi_list_lock);
  388. if (cipso_v4_doi_search(doi_def->doi)) {
  389. spin_unlock(&cipso_v4_doi_list_lock);
  390. ret_val = -EEXIST;
  391. goto doi_add_return;
  392. }
  393. list_add_tail_rcu(&doi_def->list, &cipso_v4_doi_list);
  394. spin_unlock(&cipso_v4_doi_list_lock);
  395. ret_val = 0;
  396. doi_add_return:
  397. audit_buf = netlbl_audit_start(AUDIT_MAC_CIPSOV4_ADD, audit_info);
  398. if (audit_buf) {
  399. const char *type_str;
  400. switch (doi_type) {
  401. case CIPSO_V4_MAP_TRANS:
  402. type_str = "trans";
  403. break;
  404. case CIPSO_V4_MAP_PASS:
  405. type_str = "pass";
  406. break;
  407. case CIPSO_V4_MAP_LOCAL:
  408. type_str = "local";
  409. break;
  410. default:
  411. type_str = "(unknown)";
  412. }
  413. audit_log_format(audit_buf,
  414. " cipso_doi=%u cipso_type=%s res=%u",
  415. doi, type_str, ret_val == 0 ? 1 : 0);
  416. audit_log_end(audit_buf);
  417. }
  418. return ret_val;
  419. }
  420. /**
  421. * cipso_v4_doi_free - Frees a DOI definition
  422. * @doi_def: the DOI definition
  423. *
  424. * Description:
  425. * This function frees all of the memory associated with a DOI definition.
  426. *
  427. */
  428. void cipso_v4_doi_free(struct cipso_v4_doi *doi_def)
  429. {
  430. if (!doi_def)
  431. return;
  432. switch (doi_def->type) {
  433. case CIPSO_V4_MAP_TRANS:
  434. kfree(doi_def->map.std->lvl.cipso);
  435. kfree(doi_def->map.std->lvl.local);
  436. kfree(doi_def->map.std->cat.cipso);
  437. kfree(doi_def->map.std->cat.local);
  438. break;
  439. }
  440. kfree(doi_def);
  441. }
  442. /**
  443. * cipso_v4_doi_free_rcu - Frees a DOI definition via the RCU pointer
  444. * @entry: the entry's RCU field
  445. *
  446. * Description:
  447. * This function is designed to be used as a callback to the call_rcu()
  448. * function so that the memory allocated to the DOI definition can be released
  449. * safely.
  450. *
  451. */
  452. static void cipso_v4_doi_free_rcu(struct rcu_head *entry)
  453. {
  454. struct cipso_v4_doi *doi_def;
  455. doi_def = container_of(entry, struct cipso_v4_doi, rcu);
  456. cipso_v4_doi_free(doi_def);
  457. }
  458. /**
  459. * cipso_v4_doi_remove - Remove an existing DOI from the CIPSO protocol engine
  460. * @doi: the DOI value
  461. * @audit_secid: the LSM secid to use in the audit message
  462. *
  463. * Description:
  464. * Removes a DOI definition from the CIPSO engine. The NetLabel routines will
  465. * be called to release their own LSM domain mappings as well as our own
  466. * domain list. Returns zero on success and negative values on failure.
  467. *
  468. */
  469. int cipso_v4_doi_remove(u32 doi, struct netlbl_audit *audit_info)
  470. {
  471. int ret_val;
  472. struct cipso_v4_doi *doi_def;
  473. struct audit_buffer *audit_buf;
  474. spin_lock(&cipso_v4_doi_list_lock);
  475. doi_def = cipso_v4_doi_search(doi);
  476. if (!doi_def) {
  477. spin_unlock(&cipso_v4_doi_list_lock);
  478. ret_val = -ENOENT;
  479. goto doi_remove_return;
  480. }
  481. if (!atomic_dec_and_test(&doi_def->refcount)) {
  482. spin_unlock(&cipso_v4_doi_list_lock);
  483. ret_val = -EBUSY;
  484. goto doi_remove_return;
  485. }
  486. list_del_rcu(&doi_def->list);
  487. spin_unlock(&cipso_v4_doi_list_lock);
  488. cipso_v4_cache_invalidate();
  489. call_rcu(&doi_def->rcu, cipso_v4_doi_free_rcu);
  490. ret_val = 0;
  491. doi_remove_return:
  492. audit_buf = netlbl_audit_start(AUDIT_MAC_CIPSOV4_DEL, audit_info);
  493. if (audit_buf) {
  494. audit_log_format(audit_buf,
  495. " cipso_doi=%u res=%u",
  496. doi, ret_val == 0 ? 1 : 0);
  497. audit_log_end(audit_buf);
  498. }
  499. return ret_val;
  500. }
  501. /**
  502. * cipso_v4_doi_getdef - Returns a reference to a valid DOI definition
  503. * @doi: the DOI value
  504. *
  505. * Description:
  506. * Searches for a valid DOI definition and if one is found it is returned to
  507. * the caller. Otherwise NULL is returned. The caller must ensure that
  508. * rcu_read_lock() is held while accessing the returned definition and the DOI
  509. * definition reference count is decremented when the caller is done.
  510. *
  511. */
  512. struct cipso_v4_doi *cipso_v4_doi_getdef(u32 doi)
  513. {
  514. struct cipso_v4_doi *doi_def;
  515. rcu_read_lock();
  516. doi_def = cipso_v4_doi_search(doi);
  517. if (!doi_def)
  518. goto doi_getdef_return;
  519. if (!atomic_inc_not_zero(&doi_def->refcount))
  520. doi_def = NULL;
  521. doi_getdef_return:
  522. rcu_read_unlock();
  523. return doi_def;
  524. }
  525. /**
  526. * cipso_v4_doi_putdef - Releases a reference for the given DOI definition
  527. * @doi_def: the DOI definition
  528. *
  529. * Description:
  530. * Releases a DOI definition reference obtained from cipso_v4_doi_getdef().
  531. *
  532. */
  533. void cipso_v4_doi_putdef(struct cipso_v4_doi *doi_def)
  534. {
  535. if (!doi_def)
  536. return;
  537. if (!atomic_dec_and_test(&doi_def->refcount))
  538. return;
  539. spin_lock(&cipso_v4_doi_list_lock);
  540. list_del_rcu(&doi_def->list);
  541. spin_unlock(&cipso_v4_doi_list_lock);
  542. cipso_v4_cache_invalidate();
  543. call_rcu(&doi_def->rcu, cipso_v4_doi_free_rcu);
  544. }
  545. /**
  546. * cipso_v4_doi_walk - Iterate through the DOI definitions
  547. * @skip_cnt: skip past this number of DOI definitions, updated
  548. * @callback: callback for each DOI definition
  549. * @cb_arg: argument for the callback function
  550. *
  551. * Description:
  552. * Iterate over the DOI definition list, skipping the first @skip_cnt entries.
  553. * For each entry call @callback, if @callback returns a negative value stop
  554. * 'walking' through the list and return. Updates the value in @skip_cnt upon
  555. * return. Returns zero on success, negative values on failure.
  556. *
  557. */
  558. int cipso_v4_doi_walk(u32 *skip_cnt,
  559. int (*callback) (struct cipso_v4_doi *doi_def, void *arg),
  560. void *cb_arg)
  561. {
  562. int ret_val = -ENOENT;
  563. u32 doi_cnt = 0;
  564. struct cipso_v4_doi *iter_doi;
  565. rcu_read_lock();
  566. list_for_each_entry_rcu(iter_doi, &cipso_v4_doi_list, list)
  567. if (atomic_read(&iter_doi->refcount) > 0) {
  568. if (doi_cnt++ < *skip_cnt)
  569. continue;
  570. ret_val = callback(iter_doi, cb_arg);
  571. if (ret_val < 0) {
  572. doi_cnt--;
  573. goto doi_walk_return;
  574. }
  575. }
  576. doi_walk_return:
  577. rcu_read_unlock();
  578. *skip_cnt = doi_cnt;
  579. return ret_val;
  580. }
  581. /*
  582. * Label Mapping Functions
  583. */
  584. /**
  585. * cipso_v4_map_lvl_valid - Checks to see if the given level is understood
  586. * @doi_def: the DOI definition
  587. * @level: the level to check
  588. *
  589. * Description:
  590. * Checks the given level against the given DOI definition and returns a
  591. * negative value if the level does not have a valid mapping and a zero value
  592. * if the level is defined by the DOI.
  593. *
  594. */
  595. static int cipso_v4_map_lvl_valid(const struct cipso_v4_doi *doi_def, u8 level)
  596. {
  597. switch (doi_def->type) {
  598. case CIPSO_V4_MAP_PASS:
  599. return 0;
  600. case CIPSO_V4_MAP_TRANS:
  601. if (doi_def->map.std->lvl.cipso[level] < CIPSO_V4_INV_LVL)
  602. return 0;
  603. break;
  604. }
  605. return -EFAULT;
  606. }
  607. /**
  608. * cipso_v4_map_lvl_hton - Perform a level mapping from the host to the network
  609. * @doi_def: the DOI definition
  610. * @host_lvl: the host MLS level
  611. * @net_lvl: the network/CIPSO MLS level
  612. *
  613. * Description:
  614. * Perform a label mapping to translate a local MLS level to the correct
  615. * CIPSO level using the given DOI definition. Returns zero on success,
  616. * negative values otherwise.
  617. *
  618. */
  619. static int cipso_v4_map_lvl_hton(const struct cipso_v4_doi *doi_def,
  620. u32 host_lvl,
  621. u32 *net_lvl)
  622. {
  623. switch (doi_def->type) {
  624. case CIPSO_V4_MAP_PASS:
  625. *net_lvl = host_lvl;
  626. return 0;
  627. case CIPSO_V4_MAP_TRANS:
  628. if (host_lvl < doi_def->map.std->lvl.local_size &&
  629. doi_def->map.std->lvl.local[host_lvl] < CIPSO_V4_INV_LVL) {
  630. *net_lvl = doi_def->map.std->lvl.local[host_lvl];
  631. return 0;
  632. }
  633. return -EPERM;
  634. }
  635. return -EINVAL;
  636. }
  637. /**
  638. * cipso_v4_map_lvl_ntoh - Perform a level mapping from the network to the host
  639. * @doi_def: the DOI definition
  640. * @net_lvl: the network/CIPSO MLS level
  641. * @host_lvl: the host MLS level
  642. *
  643. * Description:
  644. * Perform a label mapping to translate a CIPSO level to the correct local MLS
  645. * level using the given DOI definition. Returns zero on success, negative
  646. * values otherwise.
  647. *
  648. */
  649. static int cipso_v4_map_lvl_ntoh(const struct cipso_v4_doi *doi_def,
  650. u32 net_lvl,
  651. u32 *host_lvl)
  652. {
  653. struct cipso_v4_std_map_tbl *map_tbl;
  654. switch (doi_def->type) {
  655. case CIPSO_V4_MAP_PASS:
  656. *host_lvl = net_lvl;
  657. return 0;
  658. case CIPSO_V4_MAP_TRANS:
  659. map_tbl = doi_def->map.std;
  660. if (net_lvl < map_tbl->lvl.cipso_size &&
  661. map_tbl->lvl.cipso[net_lvl] < CIPSO_V4_INV_LVL) {
  662. *host_lvl = doi_def->map.std->lvl.cipso[net_lvl];
  663. return 0;
  664. }
  665. return -EPERM;
  666. }
  667. return -EINVAL;
  668. }
  669. /**
  670. * cipso_v4_map_cat_rbm_valid - Checks to see if the category bitmap is valid
  671. * @doi_def: the DOI definition
  672. * @bitmap: category bitmap
  673. * @bitmap_len: bitmap length in bytes
  674. *
  675. * Description:
  676. * Checks the given category bitmap against the given DOI definition and
  677. * returns a negative value if any of the categories in the bitmap do not have
  678. * a valid mapping and a zero value if all of the categories are valid.
  679. *
  680. */
  681. static int cipso_v4_map_cat_rbm_valid(const struct cipso_v4_doi *doi_def,
  682. const unsigned char *bitmap,
  683. u32 bitmap_len)
  684. {
  685. int cat = -1;
  686. u32 bitmap_len_bits = bitmap_len * 8;
  687. u32 cipso_cat_size;
  688. u32 *cipso_array;
  689. switch (doi_def->type) {
  690. case CIPSO_V4_MAP_PASS:
  691. return 0;
  692. case CIPSO_V4_MAP_TRANS:
  693. cipso_cat_size = doi_def->map.std->cat.cipso_size;
  694. cipso_array = doi_def->map.std->cat.cipso;
  695. for (;;) {
  696. cat = netlbl_bitmap_walk(bitmap,
  697. bitmap_len_bits,
  698. cat + 1,
  699. 1);
  700. if (cat < 0)
  701. break;
  702. if (cat >= cipso_cat_size ||
  703. cipso_array[cat] >= CIPSO_V4_INV_CAT)
  704. return -EFAULT;
  705. }
  706. if (cat == -1)
  707. return 0;
  708. break;
  709. }
  710. return -EFAULT;
  711. }
  712. /**
  713. * cipso_v4_map_cat_rbm_hton - Perform a category mapping from host to network
  714. * @doi_def: the DOI definition
  715. * @secattr: the security attributes
  716. * @net_cat: the zero'd out category bitmap in network/CIPSO format
  717. * @net_cat_len: the length of the CIPSO bitmap in bytes
  718. *
  719. * Description:
  720. * Perform a label mapping to translate a local MLS category bitmap to the
  721. * correct CIPSO bitmap using the given DOI definition. Returns the minimum
  722. * size in bytes of the network bitmap on success, negative values otherwise.
  723. *
  724. */
  725. static int cipso_v4_map_cat_rbm_hton(const struct cipso_v4_doi *doi_def,
  726. const struct netlbl_lsm_secattr *secattr,
  727. unsigned char *net_cat,
  728. u32 net_cat_len)
  729. {
  730. int host_spot = -1;
  731. u32 net_spot = CIPSO_V4_INV_CAT;
  732. u32 net_spot_max = 0;
  733. u32 net_clen_bits = net_cat_len * 8;
  734. u32 host_cat_size = 0;
  735. u32 *host_cat_array = NULL;
  736. if (doi_def->type == CIPSO_V4_MAP_TRANS) {
  737. host_cat_size = doi_def->map.std->cat.local_size;
  738. host_cat_array = doi_def->map.std->cat.local;
  739. }
  740. for (;;) {
  741. host_spot = netlbl_catmap_walk(secattr->attr.mls.cat,
  742. host_spot + 1);
  743. if (host_spot < 0)
  744. break;
  745. switch (doi_def->type) {
  746. case CIPSO_V4_MAP_PASS:
  747. net_spot = host_spot;
  748. break;
  749. case CIPSO_V4_MAP_TRANS:
  750. if (host_spot >= host_cat_size)
  751. return -EPERM;
  752. net_spot = host_cat_array[host_spot];
  753. if (net_spot >= CIPSO_V4_INV_CAT)
  754. return -EPERM;
  755. break;
  756. }
  757. if (net_spot >= net_clen_bits)
  758. return -ENOSPC;
  759. netlbl_bitmap_setbit(net_cat, net_spot, 1);
  760. if (net_spot > net_spot_max)
  761. net_spot_max = net_spot;
  762. }
  763. if (++net_spot_max % 8)
  764. return net_spot_max / 8 + 1;
  765. return net_spot_max / 8;
  766. }
  767. /**
  768. * cipso_v4_map_cat_rbm_ntoh - Perform a category mapping from network to host
  769. * @doi_def: the DOI definition
  770. * @net_cat: the category bitmap in network/CIPSO format
  771. * @net_cat_len: the length of the CIPSO bitmap in bytes
  772. * @secattr: the security attributes
  773. *
  774. * Description:
  775. * Perform a label mapping to translate a CIPSO bitmap to the correct local
  776. * MLS category bitmap using the given DOI definition. Returns zero on
  777. * success, negative values on failure.
  778. *
  779. */
  780. static int cipso_v4_map_cat_rbm_ntoh(const struct cipso_v4_doi *doi_def,
  781. const unsigned char *net_cat,
  782. u32 net_cat_len,
  783. struct netlbl_lsm_secattr *secattr)
  784. {
  785. int ret_val;
  786. int net_spot = -1;
  787. u32 host_spot = CIPSO_V4_INV_CAT;
  788. u32 net_clen_bits = net_cat_len * 8;
  789. u32 net_cat_size = 0;
  790. u32 *net_cat_array = NULL;
  791. if (doi_def->type == CIPSO_V4_MAP_TRANS) {
  792. net_cat_size = doi_def->map.std->cat.cipso_size;
  793. net_cat_array = doi_def->map.std->cat.cipso;
  794. }
  795. for (;;) {
  796. net_spot = netlbl_bitmap_walk(net_cat,
  797. net_clen_bits,
  798. net_spot + 1,
  799. 1);
  800. if (net_spot < 0) {
  801. if (net_spot == -2)
  802. return -EFAULT;
  803. return 0;
  804. }
  805. switch (doi_def->type) {
  806. case CIPSO_V4_MAP_PASS:
  807. host_spot = net_spot;
  808. break;
  809. case CIPSO_V4_MAP_TRANS:
  810. if (net_spot >= net_cat_size)
  811. return -EPERM;
  812. host_spot = net_cat_array[net_spot];
  813. if (host_spot >= CIPSO_V4_INV_CAT)
  814. return -EPERM;
  815. break;
  816. }
  817. ret_val = netlbl_catmap_setbit(&secattr->attr.mls.cat,
  818. host_spot,
  819. GFP_ATOMIC);
  820. if (ret_val != 0)
  821. return ret_val;
  822. }
  823. return -EINVAL;
  824. }
  825. /**
  826. * cipso_v4_map_cat_enum_valid - Checks to see if the categories are valid
  827. * @doi_def: the DOI definition
  828. * @enumcat: category list
  829. * @enumcat_len: length of the category list in bytes
  830. *
  831. * Description:
  832. * Checks the given categories against the given DOI definition and returns a
  833. * negative value if any of the categories do not have a valid mapping and a
  834. * zero value if all of the categories are valid.
  835. *
  836. */
  837. static int cipso_v4_map_cat_enum_valid(const struct cipso_v4_doi *doi_def,
  838. const unsigned char *enumcat,
  839. u32 enumcat_len)
  840. {
  841. u16 cat;
  842. int cat_prev = -1;
  843. u32 iter;
  844. if (doi_def->type != CIPSO_V4_MAP_PASS || enumcat_len & 0x01)
  845. return -EFAULT;
  846. for (iter = 0; iter < enumcat_len; iter += 2) {
  847. cat = get_unaligned_be16(&enumcat[iter]);
  848. if (cat <= cat_prev)
  849. return -EFAULT;
  850. cat_prev = cat;
  851. }
  852. return 0;
  853. }
  854. /**
  855. * cipso_v4_map_cat_enum_hton - Perform a category mapping from host to network
  856. * @doi_def: the DOI definition
  857. * @secattr: the security attributes
  858. * @net_cat: the zero'd out category list in network/CIPSO format
  859. * @net_cat_len: the length of the CIPSO category list in bytes
  860. *
  861. * Description:
  862. * Perform a label mapping to translate a local MLS category bitmap to the
  863. * correct CIPSO category list using the given DOI definition. Returns the
  864. * size in bytes of the network category bitmap on success, negative values
  865. * otherwise.
  866. *
  867. */
  868. static int cipso_v4_map_cat_enum_hton(const struct cipso_v4_doi *doi_def,
  869. const struct netlbl_lsm_secattr *secattr,
  870. unsigned char *net_cat,
  871. u32 net_cat_len)
  872. {
  873. int cat = -1;
  874. u32 cat_iter = 0;
  875. for (;;) {
  876. cat = netlbl_catmap_walk(secattr->attr.mls.cat, cat + 1);
  877. if (cat < 0)
  878. break;
  879. if ((cat_iter + 2) > net_cat_len)
  880. return -ENOSPC;
  881. *((__be16 *)&net_cat[cat_iter]) = htons(cat);
  882. cat_iter += 2;
  883. }
  884. return cat_iter;
  885. }
  886. /**
  887. * cipso_v4_map_cat_enum_ntoh - Perform a category mapping from network to host
  888. * @doi_def: the DOI definition
  889. * @net_cat: the category list in network/CIPSO format
  890. * @net_cat_len: the length of the CIPSO bitmap in bytes
  891. * @secattr: the security attributes
  892. *
  893. * Description:
  894. * Perform a label mapping to translate a CIPSO category list to the correct
  895. * local MLS category bitmap using the given DOI definition. Returns zero on
  896. * success, negative values on failure.
  897. *
  898. */
  899. static int cipso_v4_map_cat_enum_ntoh(const struct cipso_v4_doi *doi_def,
  900. const unsigned char *net_cat,
  901. u32 net_cat_len,
  902. struct netlbl_lsm_secattr *secattr)
  903. {
  904. int ret_val;
  905. u32 iter;
  906. for (iter = 0; iter < net_cat_len; iter += 2) {
  907. ret_val = netlbl_catmap_setbit(&secattr->attr.mls.cat,
  908. get_unaligned_be16(&net_cat[iter]),
  909. GFP_ATOMIC);
  910. if (ret_val != 0)
  911. return ret_val;
  912. }
  913. return 0;
  914. }
  915. /**
  916. * cipso_v4_map_cat_rng_valid - Checks to see if the categories are valid
  917. * @doi_def: the DOI definition
  918. * @rngcat: category list
  919. * @rngcat_len: length of the category list in bytes
  920. *
  921. * Description:
  922. * Checks the given categories against the given DOI definition and returns a
  923. * negative value if any of the categories do not have a valid mapping and a
  924. * zero value if all of the categories are valid.
  925. *
  926. */
  927. static int cipso_v4_map_cat_rng_valid(const struct cipso_v4_doi *doi_def,
  928. const unsigned char *rngcat,
  929. u32 rngcat_len)
  930. {
  931. u16 cat_high;
  932. u16 cat_low;
  933. u32 cat_prev = CIPSO_V4_MAX_REM_CATS + 1;
  934. u32 iter;
  935. if (doi_def->type != CIPSO_V4_MAP_PASS || rngcat_len & 0x01)
  936. return -EFAULT;
  937. for (iter = 0; iter < rngcat_len; iter += 4) {
  938. cat_high = get_unaligned_be16(&rngcat[iter]);
  939. if ((iter + 4) <= rngcat_len)
  940. cat_low = get_unaligned_be16(&rngcat[iter + 2]);
  941. else
  942. cat_low = 0;
  943. if (cat_high > cat_prev)
  944. return -EFAULT;
  945. cat_prev = cat_low;
  946. }
  947. return 0;
  948. }
  949. /**
  950. * cipso_v4_map_cat_rng_hton - Perform a category mapping from host to network
  951. * @doi_def: the DOI definition
  952. * @secattr: the security attributes
  953. * @net_cat: the zero'd out category list in network/CIPSO format
  954. * @net_cat_len: the length of the CIPSO category list in bytes
  955. *
  956. * Description:
  957. * Perform a label mapping to translate a local MLS category bitmap to the
  958. * correct CIPSO category list using the given DOI definition. Returns the
  959. * size in bytes of the network category bitmap on success, negative values
  960. * otherwise.
  961. *
  962. */
  963. static int cipso_v4_map_cat_rng_hton(const struct cipso_v4_doi *doi_def,
  964. const struct netlbl_lsm_secattr *secattr,
  965. unsigned char *net_cat,
  966. u32 net_cat_len)
  967. {
  968. int iter = -1;
  969. u16 array[CIPSO_V4_TAG_RNG_CAT_MAX * 2];
  970. u32 array_cnt = 0;
  971. u32 cat_size = 0;
  972. /* make sure we don't overflow the 'array[]' variable */
  973. if (net_cat_len >
  974. (CIPSO_V4_OPT_LEN_MAX - CIPSO_V4_HDR_LEN - CIPSO_V4_TAG_RNG_BLEN))
  975. return -ENOSPC;
  976. for (;;) {
  977. iter = netlbl_catmap_walk(secattr->attr.mls.cat, iter + 1);
  978. if (iter < 0)
  979. break;
  980. cat_size += (iter == 0 ? 0 : sizeof(u16));
  981. if (cat_size > net_cat_len)
  982. return -ENOSPC;
  983. array[array_cnt++] = iter;
  984. iter = netlbl_catmap_walkrng(secattr->attr.mls.cat, iter);
  985. if (iter < 0)
  986. return -EFAULT;
  987. cat_size += sizeof(u16);
  988. if (cat_size > net_cat_len)
  989. return -ENOSPC;
  990. array[array_cnt++] = iter;
  991. }
  992. for (iter = 0; array_cnt > 0;) {
  993. *((__be16 *)&net_cat[iter]) = htons(array[--array_cnt]);
  994. iter += 2;
  995. array_cnt--;
  996. if (array[array_cnt] != 0) {
  997. *((__be16 *)&net_cat[iter]) = htons(array[array_cnt]);
  998. iter += 2;
  999. }
  1000. }
  1001. return cat_size;
  1002. }
  1003. /**
  1004. * cipso_v4_map_cat_rng_ntoh - Perform a category mapping from network to host
  1005. * @doi_def: the DOI definition
  1006. * @net_cat: the category list in network/CIPSO format
  1007. * @net_cat_len: the length of the CIPSO bitmap in bytes
  1008. * @secattr: the security attributes
  1009. *
  1010. * Description:
  1011. * Perform a label mapping to translate a CIPSO category list to the correct
  1012. * local MLS category bitmap using the given DOI definition. Returns zero on
  1013. * success, negative values on failure.
  1014. *
  1015. */
  1016. static int cipso_v4_map_cat_rng_ntoh(const struct cipso_v4_doi *doi_def,
  1017. const unsigned char *net_cat,
  1018. u32 net_cat_len,
  1019. struct netlbl_lsm_secattr *secattr)
  1020. {
  1021. int ret_val;
  1022. u32 net_iter;
  1023. u16 cat_low;
  1024. u16 cat_high;
  1025. for (net_iter = 0; net_iter < net_cat_len; net_iter += 4) {
  1026. cat_high = get_unaligned_be16(&net_cat[net_iter]);
  1027. if ((net_iter + 4) <= net_cat_len)
  1028. cat_low = get_unaligned_be16(&net_cat[net_iter + 2]);
  1029. else
  1030. cat_low = 0;
  1031. ret_val = netlbl_catmap_setrng(&secattr->attr.mls.cat,
  1032. cat_low,
  1033. cat_high,
  1034. GFP_ATOMIC);
  1035. if (ret_val != 0)
  1036. return ret_val;
  1037. }
  1038. return 0;
  1039. }
  1040. /*
  1041. * Protocol Handling Functions
  1042. */
  1043. /**
  1044. * cipso_v4_gentag_hdr - Generate a CIPSO option header
  1045. * @doi_def: the DOI definition
  1046. * @len: the total tag length in bytes, not including this header
  1047. * @buf: the CIPSO option buffer
  1048. *
  1049. * Description:
  1050. * Write a CIPSO header into the beginning of @buffer.
  1051. *
  1052. */
  1053. static void cipso_v4_gentag_hdr(const struct cipso_v4_doi *doi_def,
  1054. unsigned char *buf,
  1055. u32 len)
  1056. {
  1057. buf[0] = IPOPT_CIPSO;
  1058. buf[1] = CIPSO_V4_HDR_LEN + len;
  1059. *(__be32 *)&buf[2] = htonl(doi_def->doi);
  1060. }
  1061. /**
  1062. * cipso_v4_gentag_rbm - Generate a CIPSO restricted bitmap tag (type #1)
  1063. * @doi_def: the DOI definition
  1064. * @secattr: the security attributes
  1065. * @buffer: the option buffer
  1066. * @buffer_len: length of buffer in bytes
  1067. *
  1068. * Description:
  1069. * Generate a CIPSO option using the restricted bitmap tag, tag type #1. The
  1070. * actual buffer length may be larger than the indicated size due to
  1071. * translation between host and network category bitmaps. Returns the size of
  1072. * the tag on success, negative values on failure.
  1073. *
  1074. */
  1075. static int cipso_v4_gentag_rbm(const struct cipso_v4_doi *doi_def,
  1076. const struct netlbl_lsm_secattr *secattr,
  1077. unsigned char *buffer,
  1078. u32 buffer_len)
  1079. {
  1080. int ret_val;
  1081. u32 tag_len;
  1082. u32 level;
  1083. if ((secattr->flags & NETLBL_SECATTR_MLS_LVL) == 0)
  1084. return -EPERM;
  1085. ret_val = cipso_v4_map_lvl_hton(doi_def,
  1086. secattr->attr.mls.lvl,
  1087. &level);
  1088. if (ret_val != 0)
  1089. return ret_val;
  1090. if (secattr->flags & NETLBL_SECATTR_MLS_CAT) {
  1091. ret_val = cipso_v4_map_cat_rbm_hton(doi_def,
  1092. secattr,
  1093. &buffer[4],
  1094. buffer_len - 4);
  1095. if (ret_val < 0)
  1096. return ret_val;
  1097. /* This will send packets using the "optimized" format when
  1098. * possible as specified in section 3.4.2.6 of the
  1099. * CIPSO draft. */
  1100. if (cipso_v4_rbm_optfmt && ret_val > 0 && ret_val <= 10)
  1101. tag_len = 14;
  1102. else
  1103. tag_len = 4 + ret_val;
  1104. } else
  1105. tag_len = 4;
  1106. buffer[0] = CIPSO_V4_TAG_RBITMAP;
  1107. buffer[1] = tag_len;
  1108. buffer[3] = level;
  1109. return tag_len;
  1110. }
  1111. /**
  1112. * cipso_v4_parsetag_rbm - Parse a CIPSO restricted bitmap tag
  1113. * @doi_def: the DOI definition
  1114. * @tag: the CIPSO tag
  1115. * @secattr: the security attributes
  1116. *
  1117. * Description:
  1118. * Parse a CIPSO restricted bitmap tag (tag type #1) and return the security
  1119. * attributes in @secattr. Return zero on success, negatives values on
  1120. * failure.
  1121. *
  1122. */
  1123. static int cipso_v4_parsetag_rbm(const struct cipso_v4_doi *doi_def,
  1124. const unsigned char *tag,
  1125. struct netlbl_lsm_secattr *secattr)
  1126. {
  1127. int ret_val;
  1128. u8 tag_len = tag[1];
  1129. u32 level;
  1130. ret_val = cipso_v4_map_lvl_ntoh(doi_def, tag[3], &level);
  1131. if (ret_val != 0)
  1132. return ret_val;
  1133. secattr->attr.mls.lvl = level;
  1134. secattr->flags |= NETLBL_SECATTR_MLS_LVL;
  1135. if (tag_len > 4) {
  1136. ret_val = cipso_v4_map_cat_rbm_ntoh(doi_def,
  1137. &tag[4],
  1138. tag_len - 4,
  1139. secattr);
  1140. if (ret_val != 0) {
  1141. netlbl_catmap_free(secattr->attr.mls.cat);
  1142. return ret_val;
  1143. }
  1144. secattr->flags |= NETLBL_SECATTR_MLS_CAT;
  1145. }
  1146. return 0;
  1147. }
  1148. /**
  1149. * cipso_v4_gentag_enum - Generate a CIPSO enumerated tag (type #2)
  1150. * @doi_def: the DOI definition
  1151. * @secattr: the security attributes
  1152. * @buffer: the option buffer
  1153. * @buffer_len: length of buffer in bytes
  1154. *
  1155. * Description:
  1156. * Generate a CIPSO option using the enumerated tag, tag type #2. Returns the
  1157. * size of the tag on success, negative values on failure.
  1158. *
  1159. */
  1160. static int cipso_v4_gentag_enum(const struct cipso_v4_doi *doi_def,
  1161. const struct netlbl_lsm_secattr *secattr,
  1162. unsigned char *buffer,
  1163. u32 buffer_len)
  1164. {
  1165. int ret_val;
  1166. u32 tag_len;
  1167. u32 level;
  1168. if (!(secattr->flags & NETLBL_SECATTR_MLS_LVL))
  1169. return -EPERM;
  1170. ret_val = cipso_v4_map_lvl_hton(doi_def,
  1171. secattr->attr.mls.lvl,
  1172. &level);
  1173. if (ret_val != 0)
  1174. return ret_val;
  1175. if (secattr->flags & NETLBL_SECATTR_MLS_CAT) {
  1176. ret_val = cipso_v4_map_cat_enum_hton(doi_def,
  1177. secattr,
  1178. &buffer[4],
  1179. buffer_len - 4);
  1180. if (ret_val < 0)
  1181. return ret_val;
  1182. tag_len = 4 + ret_val;
  1183. } else
  1184. tag_len = 4;
  1185. buffer[0] = CIPSO_V4_TAG_ENUM;
  1186. buffer[1] = tag_len;
  1187. buffer[3] = level;
  1188. return tag_len;
  1189. }
  1190. /**
  1191. * cipso_v4_parsetag_enum - Parse a CIPSO enumerated tag
  1192. * @doi_def: the DOI definition
  1193. * @tag: the CIPSO tag
  1194. * @secattr: the security attributes
  1195. *
  1196. * Description:
  1197. * Parse a CIPSO enumerated tag (tag type #2) and return the security
  1198. * attributes in @secattr. Return zero on success, negatives values on
  1199. * failure.
  1200. *
  1201. */
  1202. static int cipso_v4_parsetag_enum(const struct cipso_v4_doi *doi_def,
  1203. const unsigned char *tag,
  1204. struct netlbl_lsm_secattr *secattr)
  1205. {
  1206. int ret_val;
  1207. u8 tag_len = tag[1];
  1208. u32 level;
  1209. ret_val = cipso_v4_map_lvl_ntoh(doi_def, tag[3], &level);
  1210. if (ret_val != 0)
  1211. return ret_val;
  1212. secattr->attr.mls.lvl = level;
  1213. secattr->flags |= NETLBL_SECATTR_MLS_LVL;
  1214. if (tag_len > 4) {
  1215. ret_val = cipso_v4_map_cat_enum_ntoh(doi_def,
  1216. &tag[4],
  1217. tag_len - 4,
  1218. secattr);
  1219. if (ret_val != 0) {
  1220. netlbl_catmap_free(secattr->attr.mls.cat);
  1221. return ret_val;
  1222. }
  1223. secattr->flags |= NETLBL_SECATTR_MLS_CAT;
  1224. }
  1225. return 0;
  1226. }
  1227. /**
  1228. * cipso_v4_gentag_rng - Generate a CIPSO ranged tag (type #5)
  1229. * @doi_def: the DOI definition
  1230. * @secattr: the security attributes
  1231. * @buffer: the option buffer
  1232. * @buffer_len: length of buffer in bytes
  1233. *
  1234. * Description:
  1235. * Generate a CIPSO option using the ranged tag, tag type #5. Returns the
  1236. * size of the tag on success, negative values on failure.
  1237. *
  1238. */
  1239. static int cipso_v4_gentag_rng(const struct cipso_v4_doi *doi_def,
  1240. const struct netlbl_lsm_secattr *secattr,
  1241. unsigned char *buffer,
  1242. u32 buffer_len)
  1243. {
  1244. int ret_val;
  1245. u32 tag_len;
  1246. u32 level;
  1247. if (!(secattr->flags & NETLBL_SECATTR_MLS_LVL))
  1248. return -EPERM;
  1249. ret_val = cipso_v4_map_lvl_hton(doi_def,
  1250. secattr->attr.mls.lvl,
  1251. &level);
  1252. if (ret_val != 0)
  1253. return ret_val;
  1254. if (secattr->flags & NETLBL_SECATTR_MLS_CAT) {
  1255. ret_val = cipso_v4_map_cat_rng_hton(doi_def,
  1256. secattr,
  1257. &buffer[4],
  1258. buffer_len - 4);
  1259. if (ret_val < 0)
  1260. return ret_val;
  1261. tag_len = 4 + ret_val;
  1262. } else
  1263. tag_len = 4;
  1264. buffer[0] = CIPSO_V4_TAG_RANGE;
  1265. buffer[1] = tag_len;
  1266. buffer[3] = level;
  1267. return tag_len;
  1268. }
  1269. /**
  1270. * cipso_v4_parsetag_rng - Parse a CIPSO ranged tag
  1271. * @doi_def: the DOI definition
  1272. * @tag: the CIPSO tag
  1273. * @secattr: the security attributes
  1274. *
  1275. * Description:
  1276. * Parse a CIPSO ranged tag (tag type #5) and return the security attributes
  1277. * in @secattr. Return zero on success, negatives values on failure.
  1278. *
  1279. */
  1280. static int cipso_v4_parsetag_rng(const struct cipso_v4_doi *doi_def,
  1281. const unsigned char *tag,
  1282. struct netlbl_lsm_secattr *secattr)
  1283. {
  1284. int ret_val;
  1285. u8 tag_len = tag[1];
  1286. u32 level;
  1287. ret_val = cipso_v4_map_lvl_ntoh(doi_def, tag[3], &level);
  1288. if (ret_val != 0)
  1289. return ret_val;
  1290. secattr->attr.mls.lvl = level;
  1291. secattr->flags |= NETLBL_SECATTR_MLS_LVL;
  1292. if (tag_len > 4) {
  1293. ret_val = cipso_v4_map_cat_rng_ntoh(doi_def,
  1294. &tag[4],
  1295. tag_len - 4,
  1296. secattr);
  1297. if (ret_val != 0) {
  1298. netlbl_catmap_free(secattr->attr.mls.cat);
  1299. return ret_val;
  1300. }
  1301. secattr->flags |= NETLBL_SECATTR_MLS_CAT;
  1302. }
  1303. return 0;
  1304. }
  1305. /**
  1306. * cipso_v4_gentag_loc - Generate a CIPSO local tag (non-standard)
  1307. * @doi_def: the DOI definition
  1308. * @secattr: the security attributes
  1309. * @buffer: the option buffer
  1310. * @buffer_len: length of buffer in bytes
  1311. *
  1312. * Description:
  1313. * Generate a CIPSO option using the local tag. Returns the size of the tag
  1314. * on success, negative values on failure.
  1315. *
  1316. */
  1317. static int cipso_v4_gentag_loc(const struct cipso_v4_doi *doi_def,
  1318. const struct netlbl_lsm_secattr *secattr,
  1319. unsigned char *buffer,
  1320. u32 buffer_len)
  1321. {
  1322. if (!(secattr->flags & NETLBL_SECATTR_SECID))
  1323. return -EPERM;
  1324. buffer[0] = CIPSO_V4_TAG_LOCAL;
  1325. buffer[1] = CIPSO_V4_TAG_LOC_BLEN;
  1326. *(u32 *)&buffer[2] = secattr->attr.secid;
  1327. return CIPSO_V4_TAG_LOC_BLEN;
  1328. }
  1329. /**
  1330. * cipso_v4_parsetag_loc - Parse a CIPSO local tag
  1331. * @doi_def: the DOI definition
  1332. * @tag: the CIPSO tag
  1333. * @secattr: the security attributes
  1334. *
  1335. * Description:
  1336. * Parse a CIPSO local tag and return the security attributes in @secattr.
  1337. * Return zero on success, negatives values on failure.
  1338. *
  1339. */
  1340. static int cipso_v4_parsetag_loc(const struct cipso_v4_doi *doi_def,
  1341. const unsigned char *tag,
  1342. struct netlbl_lsm_secattr *secattr)
  1343. {
  1344. secattr->attr.secid = *(u32 *)&tag[2];
  1345. secattr->flags |= NETLBL_SECATTR_SECID;
  1346. return 0;
  1347. }
  1348. /**
  1349. * cipso_v4_optptr - Find the CIPSO option in the packet
  1350. * @skb: the packet
  1351. *
  1352. * Description:
  1353. * Parse the packet's IP header looking for a CIPSO option. Returns a pointer
  1354. * to the start of the CIPSO option on success, NULL if one if not found.
  1355. *
  1356. */
  1357. unsigned char *cipso_v4_optptr(const struct sk_buff *skb)
  1358. {
  1359. const struct iphdr *iph = ip_hdr(skb);
  1360. unsigned char *optptr = (unsigned char *)&(ip_hdr(skb)[1]);
  1361. int optlen;
  1362. int taglen;
  1363. for (optlen = iph->ihl*4 - sizeof(struct iphdr); optlen > 0; ) {
  1364. if (optptr[0] == IPOPT_CIPSO)
  1365. return optptr;
  1366. taglen = optptr[1];
  1367. optlen -= taglen;
  1368. optptr += taglen;
  1369. }
  1370. return NULL;
  1371. }
  1372. /**
  1373. * cipso_v4_validate - Validate a CIPSO option
  1374. * @option: the start of the option, on error it is set to point to the error
  1375. *
  1376. * Description:
  1377. * This routine is called to validate a CIPSO option, it checks all of the
  1378. * fields to ensure that they are at least valid, see the draft snippet below
  1379. * for details. If the option is valid then a zero value is returned and
  1380. * the value of @option is unchanged. If the option is invalid then a
  1381. * non-zero value is returned and @option is adjusted to point to the
  1382. * offending portion of the option. From the IETF draft ...
  1383. *
  1384. * "If any field within the CIPSO options, such as the DOI identifier, is not
  1385. * recognized the IP datagram is discarded and an ICMP 'parameter problem'
  1386. * (type 12) is generated and returned. The ICMP code field is set to 'bad
  1387. * parameter' (code 0) and the pointer is set to the start of the CIPSO field
  1388. * that is unrecognized."
  1389. *
  1390. */
  1391. int cipso_v4_validate(const struct sk_buff *skb, unsigned char **option)
  1392. {
  1393. unsigned char *opt = *option;
  1394. unsigned char *tag;
  1395. unsigned char opt_iter;
  1396. unsigned char err_offset = 0;
  1397. u8 opt_len;
  1398. u8 tag_len;
  1399. struct cipso_v4_doi *doi_def = NULL;
  1400. u32 tag_iter;
  1401. /* caller already checks for length values that are too large */
  1402. opt_len = opt[1];
  1403. if (opt_len < 8) {
  1404. err_offset = 1;
  1405. goto validate_return;
  1406. }
  1407. rcu_read_lock();
  1408. doi_def = cipso_v4_doi_search(get_unaligned_be32(&opt[2]));
  1409. if (!doi_def) {
  1410. err_offset = 2;
  1411. goto validate_return_locked;
  1412. }
  1413. opt_iter = CIPSO_V4_HDR_LEN;
  1414. tag = opt + opt_iter;
  1415. while (opt_iter < opt_len) {
  1416. for (tag_iter = 0; doi_def->tags[tag_iter] != tag[0];)
  1417. if (doi_def->tags[tag_iter] == CIPSO_V4_TAG_INVALID ||
  1418. ++tag_iter == CIPSO_V4_TAG_MAXCNT) {
  1419. err_offset = opt_iter;
  1420. goto validate_return_locked;
  1421. }
  1422. tag_len = tag[1];
  1423. if (tag_len > (opt_len - opt_iter)) {
  1424. err_offset = opt_iter + 1;
  1425. goto validate_return_locked;
  1426. }
  1427. switch (tag[0]) {
  1428. case CIPSO_V4_TAG_RBITMAP:
  1429. if (tag_len < CIPSO_V4_TAG_RBM_BLEN) {
  1430. err_offset = opt_iter + 1;
  1431. goto validate_return_locked;
  1432. }
  1433. /* We are already going to do all the verification
  1434. * necessary at the socket layer so from our point of
  1435. * view it is safe to turn these checks off (and less
  1436. * work), however, the CIPSO draft says we should do
  1437. * all the CIPSO validations here but it doesn't
  1438. * really specify _exactly_ what we need to validate
  1439. * ... so, just make it a sysctl tunable. */
  1440. if (cipso_v4_rbm_strictvalid) {
  1441. if (cipso_v4_map_lvl_valid(doi_def,
  1442. tag[3]) < 0) {
  1443. err_offset = opt_iter + 3;
  1444. goto validate_return_locked;
  1445. }
  1446. if (tag_len > CIPSO_V4_TAG_RBM_BLEN &&
  1447. cipso_v4_map_cat_rbm_valid(doi_def,
  1448. &tag[4],
  1449. tag_len - 4) < 0) {
  1450. err_offset = opt_iter + 4;
  1451. goto validate_return_locked;
  1452. }
  1453. }
  1454. break;
  1455. case CIPSO_V4_TAG_ENUM:
  1456. if (tag_len < CIPSO_V4_TAG_ENUM_BLEN) {
  1457. err_offset = opt_iter + 1;
  1458. goto validate_return_locked;
  1459. }
  1460. if (cipso_v4_map_lvl_valid(doi_def,
  1461. tag[3]) < 0) {
  1462. err_offset = opt_iter + 3;
  1463. goto validate_return_locked;
  1464. }
  1465. if (tag_len > CIPSO_V4_TAG_ENUM_BLEN &&
  1466. cipso_v4_map_cat_enum_valid(doi_def,
  1467. &tag[4],
  1468. tag_len - 4) < 0) {
  1469. err_offset = opt_iter + 4;
  1470. goto validate_return_locked;
  1471. }
  1472. break;
  1473. case CIPSO_V4_TAG_RANGE:
  1474. if (tag_len < CIPSO_V4_TAG_RNG_BLEN) {
  1475. err_offset = opt_iter + 1;
  1476. goto validate_return_locked;
  1477. }
  1478. if (cipso_v4_map_lvl_valid(doi_def,
  1479. tag[3]) < 0) {
  1480. err_offset = opt_iter + 3;
  1481. goto validate_return_locked;
  1482. }
  1483. if (tag_len > CIPSO_V4_TAG_RNG_BLEN &&
  1484. cipso_v4_map_cat_rng_valid(doi_def,
  1485. &tag[4],
  1486. tag_len - 4) < 0) {
  1487. err_offset = opt_iter + 4;
  1488. goto validate_return_locked;
  1489. }
  1490. break;
  1491. case CIPSO_V4_TAG_LOCAL:
  1492. /* This is a non-standard tag that we only allow for
  1493. * local connections, so if the incoming interface is
  1494. * not the loopback device drop the packet. Further,
  1495. * there is no legitimate reason for setting this from
  1496. * userspace so reject it if skb is NULL. */
  1497. if (!skb || !(skb->dev->flags & IFF_LOOPBACK)) {
  1498. err_offset = opt_iter;
  1499. goto validate_return_locked;
  1500. }
  1501. if (tag_len != CIPSO_V4_TAG_LOC_BLEN) {
  1502. err_offset = opt_iter + 1;
  1503. goto validate_return_locked;
  1504. }
  1505. break;
  1506. default:
  1507. err_offset = opt_iter;
  1508. goto validate_return_locked;
  1509. }
  1510. tag += tag_len;
  1511. opt_iter += tag_len;
  1512. }
  1513. validate_return_locked:
  1514. rcu_read_unlock();
  1515. validate_return:
  1516. *option = opt + err_offset;
  1517. return err_offset;
  1518. }
  1519. /**
  1520. * cipso_v4_error - Send the correct response for a bad packet
  1521. * @skb: the packet
  1522. * @error: the error code
  1523. * @gateway: CIPSO gateway flag
  1524. *
  1525. * Description:
  1526. * Based on the error code given in @error, send an ICMP error message back to
  1527. * the originating host. From the IETF draft ...
  1528. *
  1529. * "If the contents of the CIPSO [option] are valid but the security label is
  1530. * outside of the configured host or port label range, the datagram is
  1531. * discarded and an ICMP 'destination unreachable' (type 3) is generated and
  1532. * returned. The code field of the ICMP is set to 'communication with
  1533. * destination network administratively prohibited' (code 9) or to
  1534. * 'communication with destination host administratively prohibited'
  1535. * (code 10). The value of the code is dependent on whether the originator
  1536. * of the ICMP message is acting as a CIPSO host or a CIPSO gateway. The
  1537. * recipient of the ICMP message MUST be able to handle either value. The
  1538. * same procedure is performed if a CIPSO [option] can not be added to an
  1539. * IP packet because it is too large to fit in the IP options area."
  1540. *
  1541. * "If the error is triggered by receipt of an ICMP message, the message is
  1542. * discarded and no response is permitted (consistent with general ICMP
  1543. * processing rules)."
  1544. *
  1545. */
  1546. void cipso_v4_error(struct sk_buff *skb, int error, u32 gateway)
  1547. {
  1548. if (ip_hdr(skb)->protocol == IPPROTO_ICMP || error != -EACCES)
  1549. return;
  1550. if (gateway)
  1551. icmp_send(skb, ICMP_DEST_UNREACH, ICMP_NET_ANO, 0);
  1552. else
  1553. icmp_send(skb, ICMP_DEST_UNREACH, ICMP_HOST_ANO, 0);
  1554. }
  1555. /**
  1556. * cipso_v4_genopt - Generate a CIPSO option
  1557. * @buf: the option buffer
  1558. * @buf_len: the size of opt_buf
  1559. * @doi_def: the CIPSO DOI to use
  1560. * @secattr: the security attributes
  1561. *
  1562. * Description:
  1563. * Generate a CIPSO option using the DOI definition and security attributes
  1564. * passed to the function. Returns the length of the option on success and
  1565. * negative values on failure.
  1566. *
  1567. */
  1568. static int cipso_v4_genopt(unsigned char *buf, u32 buf_len,
  1569. const struct cipso_v4_doi *doi_def,
  1570. const struct netlbl_lsm_secattr *secattr)
  1571. {
  1572. int ret_val;
  1573. u32 iter;
  1574. if (buf_len <= CIPSO_V4_HDR_LEN)
  1575. return -ENOSPC;
  1576. /* XXX - This code assumes only one tag per CIPSO option which isn't
  1577. * really a good assumption to make but since we only support the MAC
  1578. * tags right now it is a safe assumption. */
  1579. iter = 0;
  1580. do {
  1581. memset(buf, 0, buf_len);
  1582. switch (doi_def->tags[iter]) {
  1583. case CIPSO_V4_TAG_RBITMAP:
  1584. ret_val = cipso_v4_gentag_rbm(doi_def,
  1585. secattr,
  1586. &buf[CIPSO_V4_HDR_LEN],
  1587. buf_len - CIPSO_V4_HDR_LEN);
  1588. break;
  1589. case CIPSO_V4_TAG_ENUM:
  1590. ret_val = cipso_v4_gentag_enum(doi_def,
  1591. secattr,
  1592. &buf[CIPSO_V4_HDR_LEN],
  1593. buf_len - CIPSO_V4_HDR_LEN);
  1594. break;
  1595. case CIPSO_V4_TAG_RANGE:
  1596. ret_val = cipso_v4_gentag_rng(doi_def,
  1597. secattr,
  1598. &buf[CIPSO_V4_HDR_LEN],
  1599. buf_len - CIPSO_V4_HDR_LEN);
  1600. break;
  1601. case CIPSO_V4_TAG_LOCAL:
  1602. ret_val = cipso_v4_gentag_loc(doi_def,
  1603. secattr,
  1604. &buf[CIPSO_V4_HDR_LEN],
  1605. buf_len - CIPSO_V4_HDR_LEN);
  1606. break;
  1607. default:
  1608. return -EPERM;
  1609. }
  1610. iter++;
  1611. } while (ret_val < 0 &&
  1612. iter < CIPSO_V4_TAG_MAXCNT &&
  1613. doi_def->tags[iter] != CIPSO_V4_TAG_INVALID);
  1614. if (ret_val < 0)
  1615. return ret_val;
  1616. cipso_v4_gentag_hdr(doi_def, buf, ret_val);
  1617. return CIPSO_V4_HDR_LEN + ret_val;
  1618. }
  1619. /**
  1620. * cipso_v4_sock_setattr - Add a CIPSO option to a socket
  1621. * @sk: the socket
  1622. * @doi_def: the CIPSO DOI to use
  1623. * @secattr: the specific security attributes of the socket
  1624. *
  1625. * Description:
  1626. * Set the CIPSO option on the given socket using the DOI definition and
  1627. * security attributes passed to the function. This function requires
  1628. * exclusive access to @sk, which means it either needs to be in the
  1629. * process of being created or locked. Returns zero on success and negative
  1630. * values on failure.
  1631. *
  1632. */
  1633. int cipso_v4_sock_setattr(struct sock *sk,
  1634. const struct cipso_v4_doi *doi_def,
  1635. const struct netlbl_lsm_secattr *secattr)
  1636. {
  1637. int ret_val = -EPERM;
  1638. unsigned char *buf = NULL;
  1639. u32 buf_len;
  1640. u32 opt_len;
  1641. struct ip_options_rcu *old, *opt = NULL;
  1642. struct inet_sock *sk_inet;
  1643. struct inet_connection_sock *sk_conn;
  1644. /* In the case of sock_create_lite(), the sock->sk field is not
  1645. * defined yet but it is not a problem as the only users of these
  1646. * "lite" PF_INET sockets are functions which do an accept() call
  1647. * afterwards so we will label the socket as part of the accept(). */
  1648. if (!sk)
  1649. return 0;
  1650. /* We allocate the maximum CIPSO option size here so we are probably
  1651. * being a little wasteful, but it makes our life _much_ easier later
  1652. * on and after all we are only talking about 40 bytes. */
  1653. buf_len = CIPSO_V4_OPT_LEN_MAX;
  1654. buf = kmalloc(buf_len, GFP_ATOMIC);
  1655. if (!buf) {
  1656. ret_val = -ENOMEM;
  1657. goto socket_setattr_failure;
  1658. }
  1659. ret_val = cipso_v4_genopt(buf, buf_len, doi_def, secattr);
  1660. if (ret_val < 0)
  1661. goto socket_setattr_failure;
  1662. buf_len = ret_val;
  1663. /* We can't use ip_options_get() directly because it makes a call to
  1664. * ip_options_get_alloc() which allocates memory with GFP_KERNEL and
  1665. * we won't always have CAP_NET_RAW even though we _always_ want to
  1666. * set the IPOPT_CIPSO option. */
  1667. opt_len = (buf_len + 3) & ~3;
  1668. opt = kzalloc(sizeof(*opt) + opt_len, GFP_ATOMIC);
  1669. if (!opt) {
  1670. ret_val = -ENOMEM;
  1671. goto socket_setattr_failure;
  1672. }
  1673. memcpy(opt->opt.__data, buf, buf_len);
  1674. opt->opt.optlen = opt_len;
  1675. opt->opt.cipso = sizeof(struct iphdr);
  1676. kfree(buf);
  1677. buf = NULL;
  1678. sk_inet = inet_sk(sk);
  1679. old = rcu_dereference_protected(sk_inet->inet_opt,
  1680. lockdep_sock_is_held(sk));
  1681. if (sk_inet->is_icsk) {
  1682. sk_conn = inet_csk(sk);
  1683. if (old)
  1684. sk_conn->icsk_ext_hdr_len -= old->opt.optlen;
  1685. sk_conn->icsk_ext_hdr_len += opt->opt.optlen;
  1686. sk_conn->icsk_sync_mss(sk, sk_conn->icsk_pmtu_cookie);
  1687. }
  1688. rcu_assign_pointer(sk_inet->inet_opt, opt);
  1689. if (old)
  1690. kfree_rcu(old, rcu);
  1691. return 0;
  1692. socket_setattr_failure:
  1693. kfree(buf);
  1694. kfree(opt);
  1695. return ret_val;
  1696. }
  1697. /**
  1698. * cipso_v4_req_setattr - Add a CIPSO option to a connection request socket
  1699. * @req: the connection request socket
  1700. * @doi_def: the CIPSO DOI to use
  1701. * @secattr: the specific security attributes of the socket
  1702. *
  1703. * Description:
  1704. * Set the CIPSO option on the given socket using the DOI definition and
  1705. * security attributes passed to the function. Returns zero on success and
  1706. * negative values on failure.
  1707. *
  1708. */
  1709. int cipso_v4_req_setattr(struct request_sock *req,
  1710. const struct cipso_v4_doi *doi_def,
  1711. const struct netlbl_lsm_secattr *secattr)
  1712. {
  1713. int ret_val = -EPERM;
  1714. unsigned char *buf = NULL;
  1715. u32 buf_len;
  1716. u32 opt_len;
  1717. struct ip_options_rcu *opt = NULL;
  1718. struct inet_request_sock *req_inet;
  1719. /* We allocate the maximum CIPSO option size here so we are probably
  1720. * being a little wasteful, but it makes our life _much_ easier later
  1721. * on and after all we are only talking about 40 bytes. */
  1722. buf_len = CIPSO_V4_OPT_LEN_MAX;
  1723. buf = kmalloc(buf_len, GFP_ATOMIC);
  1724. if (!buf) {
  1725. ret_val = -ENOMEM;
  1726. goto req_setattr_failure;
  1727. }
  1728. ret_val = cipso_v4_genopt(buf, buf_len, doi_def, secattr);
  1729. if (ret_val < 0)
  1730. goto req_setattr_failure;
  1731. buf_len = ret_val;
  1732. /* We can't use ip_options_get() directly because it makes a call to
  1733. * ip_options_get_alloc() which allocates memory with GFP_KERNEL and
  1734. * we won't always have CAP_NET_RAW even though we _always_ want to
  1735. * set the IPOPT_CIPSO option. */
  1736. opt_len = (buf_len + 3) & ~3;
  1737. opt = kzalloc(sizeof(*opt) + opt_len, GFP_ATOMIC);
  1738. if (!opt) {
  1739. ret_val = -ENOMEM;
  1740. goto req_setattr_failure;
  1741. }
  1742. memcpy(opt->opt.__data, buf, buf_len);
  1743. opt->opt.optlen = opt_len;
  1744. opt->opt.cipso = sizeof(struct iphdr);
  1745. kfree(buf);
  1746. buf = NULL;
  1747. req_inet = inet_rsk(req);
  1748. opt = xchg(&req_inet->opt, opt);
  1749. if (opt)
  1750. kfree_rcu(opt, rcu);
  1751. return 0;
  1752. req_setattr_failure:
  1753. kfree(buf);
  1754. kfree(opt);
  1755. return ret_val;
  1756. }
  1757. /**
  1758. * cipso_v4_delopt - Delete the CIPSO option from a set of IP options
  1759. * @opt_ptr: IP option pointer
  1760. *
  1761. * Description:
  1762. * Deletes the CIPSO IP option from a set of IP options and makes the necessary
  1763. * adjustments to the IP option structure. Returns zero on success, negative
  1764. * values on failure.
  1765. *
  1766. */
  1767. static int cipso_v4_delopt(struct ip_options_rcu **opt_ptr)
  1768. {
  1769. int hdr_delta = 0;
  1770. struct ip_options_rcu *opt = *opt_ptr;
  1771. if (opt->opt.srr || opt->opt.rr || opt->opt.ts || opt->opt.router_alert) {
  1772. u8 cipso_len;
  1773. u8 cipso_off;
  1774. unsigned char *cipso_ptr;
  1775. int iter;
  1776. int optlen_new;
  1777. cipso_off = opt->opt.cipso - sizeof(struct iphdr);
  1778. cipso_ptr = &opt->opt.__data[cipso_off];
  1779. cipso_len = cipso_ptr[1];
  1780. if (opt->opt.srr > opt->opt.cipso)
  1781. opt->opt.srr -= cipso_len;
  1782. if (opt->opt.rr > opt->opt.cipso)
  1783. opt->opt.rr -= cipso_len;
  1784. if (opt->opt.ts > opt->opt.cipso)
  1785. opt->opt.ts -= cipso_len;
  1786. if (opt->opt.router_alert > opt->opt.cipso)
  1787. opt->opt.router_alert -= cipso_len;
  1788. opt->opt.cipso = 0;
  1789. memmove(cipso_ptr, cipso_ptr + cipso_len,
  1790. opt->opt.optlen - cipso_off - cipso_len);
  1791. /* determining the new total option length is tricky because of
  1792. * the padding necessary, the only thing i can think to do at
  1793. * this point is walk the options one-by-one, skipping the
  1794. * padding at the end to determine the actual option size and
  1795. * from there we can determine the new total option length */
  1796. iter = 0;
  1797. optlen_new = 0;
  1798. while (iter < opt->opt.optlen)
  1799. if (opt->opt.__data[iter] != IPOPT_NOP) {
  1800. iter += opt->opt.__data[iter + 1];
  1801. optlen_new = iter;
  1802. } else
  1803. iter++;
  1804. hdr_delta = opt->opt.optlen;
  1805. opt->opt.optlen = (optlen_new + 3) & ~3;
  1806. hdr_delta -= opt->opt.optlen;
  1807. } else {
  1808. /* only the cipso option was present on the socket so we can
  1809. * remove the entire option struct */
  1810. *opt_ptr = NULL;
  1811. hdr_delta = opt->opt.optlen;
  1812. kfree_rcu(opt, rcu);
  1813. }
  1814. return hdr_delta;
  1815. }
  1816. /**
  1817. * cipso_v4_sock_delattr - Delete the CIPSO option from a socket
  1818. * @sk: the socket
  1819. *
  1820. * Description:
  1821. * Removes the CIPSO option from a socket, if present.
  1822. *
  1823. */
  1824. void cipso_v4_sock_delattr(struct sock *sk)
  1825. {
  1826. int hdr_delta;
  1827. struct ip_options_rcu *opt;
  1828. struct inet_sock *sk_inet;
  1829. sk_inet = inet_sk(sk);
  1830. opt = rcu_dereference_protected(sk_inet->inet_opt, 1);
  1831. if (!opt || opt->opt.cipso == 0)
  1832. return;
  1833. hdr_delta = cipso_v4_delopt(&sk_inet->inet_opt);
  1834. if (sk_inet->is_icsk && hdr_delta > 0) {
  1835. struct inet_connection_sock *sk_conn = inet_csk(sk);
  1836. sk_conn->icsk_ext_hdr_len -= hdr_delta;
  1837. sk_conn->icsk_sync_mss(sk, sk_conn->icsk_pmtu_cookie);
  1838. }
  1839. }
  1840. /**
  1841. * cipso_v4_req_delattr - Delete the CIPSO option from a request socket
  1842. * @reg: the request socket
  1843. *
  1844. * Description:
  1845. * Removes the CIPSO option from a request socket, if present.
  1846. *
  1847. */
  1848. void cipso_v4_req_delattr(struct request_sock *req)
  1849. {
  1850. struct ip_options_rcu *opt;
  1851. struct inet_request_sock *req_inet;
  1852. req_inet = inet_rsk(req);
  1853. opt = req_inet->opt;
  1854. if (!opt || opt->opt.cipso == 0)
  1855. return;
  1856. cipso_v4_delopt(&req_inet->opt);
  1857. }
  1858. /**
  1859. * cipso_v4_getattr - Helper function for the cipso_v4_*_getattr functions
  1860. * @cipso: the CIPSO v4 option
  1861. * @secattr: the security attributes
  1862. *
  1863. * Description:
  1864. * Inspect @cipso and return the security attributes in @secattr. Returns zero
  1865. * on success and negative values on failure.
  1866. *
  1867. */
  1868. int cipso_v4_getattr(const unsigned char *cipso,
  1869. struct netlbl_lsm_secattr *secattr)
  1870. {
  1871. int ret_val = -ENOMSG;
  1872. u32 doi;
  1873. struct cipso_v4_doi *doi_def;
  1874. if (cipso_v4_cache_check(cipso, cipso[1], secattr) == 0)
  1875. return 0;
  1876. doi = get_unaligned_be32(&cipso[2]);
  1877. rcu_read_lock();
  1878. doi_def = cipso_v4_doi_search(doi);
  1879. if (!doi_def)
  1880. goto getattr_return;
  1881. /* XXX - This code assumes only one tag per CIPSO option which isn't
  1882. * really a good assumption to make but since we only support the MAC
  1883. * tags right now it is a safe assumption. */
  1884. switch (cipso[6]) {
  1885. case CIPSO_V4_TAG_RBITMAP:
  1886. ret_val = cipso_v4_parsetag_rbm(doi_def, &cipso[6], secattr);
  1887. break;
  1888. case CIPSO_V4_TAG_ENUM:
  1889. ret_val = cipso_v4_parsetag_enum(doi_def, &cipso[6], secattr);
  1890. break;
  1891. case CIPSO_V4_TAG_RANGE:
  1892. ret_val = cipso_v4_parsetag_rng(doi_def, &cipso[6], secattr);
  1893. break;
  1894. case CIPSO_V4_TAG_LOCAL:
  1895. ret_val = cipso_v4_parsetag_loc(doi_def, &cipso[6], secattr);
  1896. break;
  1897. }
  1898. if (ret_val == 0)
  1899. secattr->type = NETLBL_NLTYPE_CIPSOV4;
  1900. getattr_return:
  1901. rcu_read_unlock();
  1902. return ret_val;
  1903. }
  1904. /**
  1905. * cipso_v4_sock_getattr - Get the security attributes from a sock
  1906. * @sk: the sock
  1907. * @secattr: the security attributes
  1908. *
  1909. * Description:
  1910. * Query @sk to see if there is a CIPSO option attached to the sock and if
  1911. * there is return the CIPSO security attributes in @secattr. This function
  1912. * requires that @sk be locked, or privately held, but it does not do any
  1913. * locking itself. Returns zero on success and negative values on failure.
  1914. *
  1915. */
  1916. int cipso_v4_sock_getattr(struct sock *sk, struct netlbl_lsm_secattr *secattr)
  1917. {
  1918. struct ip_options_rcu *opt;
  1919. int res = -ENOMSG;
  1920. rcu_read_lock();
  1921. opt = rcu_dereference(inet_sk(sk)->inet_opt);
  1922. if (opt && opt->opt.cipso)
  1923. res = cipso_v4_getattr(opt->opt.__data +
  1924. opt->opt.cipso -
  1925. sizeof(struct iphdr),
  1926. secattr);
  1927. rcu_read_unlock();
  1928. return res;
  1929. }
  1930. /**
  1931. * cipso_v4_skbuff_setattr - Set the CIPSO option on a packet
  1932. * @skb: the packet
  1933. * @secattr: the security attributes
  1934. *
  1935. * Description:
  1936. * Set the CIPSO option on the given packet based on the security attributes.
  1937. * Returns a pointer to the IP header on success and NULL on failure.
  1938. *
  1939. */
  1940. int cipso_v4_skbuff_setattr(struct sk_buff *skb,
  1941. const struct cipso_v4_doi *doi_def,
  1942. const struct netlbl_lsm_secattr *secattr)
  1943. {
  1944. int ret_val;
  1945. struct iphdr *iph;
  1946. struct ip_options *opt = &IPCB(skb)->opt;
  1947. unsigned char buf[CIPSO_V4_OPT_LEN_MAX];
  1948. u32 buf_len = CIPSO_V4_OPT_LEN_MAX;
  1949. u32 opt_len;
  1950. int len_delta;
  1951. ret_val = cipso_v4_genopt(buf, buf_len, doi_def, secattr);
  1952. if (ret_val < 0)
  1953. return ret_val;
  1954. buf_len = ret_val;
  1955. opt_len = (buf_len + 3) & ~3;
  1956. /* we overwrite any existing options to ensure that we have enough
  1957. * room for the CIPSO option, the reason is that we _need_ to guarantee
  1958. * that the security label is applied to the packet - we do the same
  1959. * thing when using the socket options and it hasn't caused a problem,
  1960. * if we need to we can always revisit this choice later */
  1961. len_delta = opt_len - opt->optlen;
  1962. /* if we don't ensure enough headroom we could panic on the skb_push()
  1963. * call below so make sure we have enough, we are also "mangling" the
  1964. * packet so we should probably do a copy-on-write call anyway */
  1965. ret_val = skb_cow(skb, skb_headroom(skb) + len_delta);
  1966. if (ret_val < 0)
  1967. return ret_val;
  1968. if (len_delta > 0) {
  1969. /* we assume that the header + opt->optlen have already been
  1970. * "pushed" in ip_options_build() or similar */
  1971. iph = ip_hdr(skb);
  1972. skb_push(skb, len_delta);
  1973. memmove((char *)iph - len_delta, iph, iph->ihl << 2);
  1974. skb_reset_network_header(skb);
  1975. iph = ip_hdr(skb);
  1976. } else if (len_delta < 0) {
  1977. iph = ip_hdr(skb);
  1978. memset(iph + 1, IPOPT_NOP, opt->optlen);
  1979. } else
  1980. iph = ip_hdr(skb);
  1981. if (opt->optlen > 0)
  1982. memset(opt, 0, sizeof(*opt));
  1983. opt->optlen = opt_len;
  1984. opt->cipso = sizeof(struct iphdr);
  1985. opt->is_changed = 1;
  1986. /* we have to do the following because we are being called from a
  1987. * netfilter hook which means the packet already has had the header
  1988. * fields populated and the checksum calculated - yes this means we
  1989. * are doing more work than needed but we do it to keep the core
  1990. * stack clean and tidy */
  1991. memcpy(iph + 1, buf, buf_len);
  1992. if (opt_len > buf_len)
  1993. memset((char *)(iph + 1) + buf_len, 0, opt_len - buf_len);
  1994. if (len_delta != 0) {
  1995. iph->ihl = 5 + (opt_len >> 2);
  1996. iph->tot_len = htons(skb->len);
  1997. }
  1998. ip_send_check(iph);
  1999. return 0;
  2000. }
  2001. /**
  2002. * cipso_v4_skbuff_delattr - Delete any CIPSO options from a packet
  2003. * @skb: the packet
  2004. *
  2005. * Description:
  2006. * Removes any and all CIPSO options from the given packet. Returns zero on
  2007. * success, negative values on failure.
  2008. *
  2009. */
  2010. int cipso_v4_skbuff_delattr(struct sk_buff *skb)
  2011. {
  2012. int ret_val;
  2013. struct iphdr *iph;
  2014. struct ip_options *opt = &IPCB(skb)->opt;
  2015. unsigned char *cipso_ptr;
  2016. if (opt->cipso == 0)
  2017. return 0;
  2018. /* since we are changing the packet we should make a copy */
  2019. ret_val = skb_cow(skb, skb_headroom(skb));
  2020. if (ret_val < 0)
  2021. return ret_val;
  2022. /* the easiest thing to do is just replace the cipso option with noop
  2023. * options since we don't change the size of the packet, although we
  2024. * still need to recalculate the checksum */
  2025. iph = ip_hdr(skb);
  2026. cipso_ptr = (unsigned char *)iph + opt->cipso;
  2027. memset(cipso_ptr, IPOPT_NOOP, cipso_ptr[1]);
  2028. opt->cipso = 0;
  2029. opt->is_changed = 1;
  2030. ip_send_check(iph);
  2031. return 0;
  2032. }
  2033. /*
  2034. * Setup Functions
  2035. */
  2036. /**
  2037. * cipso_v4_init - Initialize the CIPSO module
  2038. *
  2039. * Description:
  2040. * Initialize the CIPSO module and prepare it for use. Returns zero on success
  2041. * and negative values on failure.
  2042. *
  2043. */
  2044. static int __init cipso_v4_init(void)
  2045. {
  2046. int ret_val;
  2047. ret_val = cipso_v4_cache_init();
  2048. if (ret_val != 0)
  2049. panic("Failed to initialize the CIPSO/IPv4 cache (%d)\n",
  2050. ret_val);
  2051. return 0;
  2052. }
  2053. subsys_initcall(cipso_v4_init);