cipso_ipv4.c 56 KB

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