cipso_ipv4.c 52 KB

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