cipso_ipv4.c 52 KB

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