cipso_ipv4.c 59 KB

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