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. spin_lock(&cipso_v4_doi_list_lock);
  443. if (cipso_v4_doi_search(doi_def->doi) != NULL)
  444. goto doi_add_failure;
  445. list_add_tail_rcu(&doi_def->list, &cipso_v4_doi_list);
  446. spin_unlock(&cipso_v4_doi_list_lock);
  447. return 0;
  448. doi_add_failure:
  449. spin_unlock(&cipso_v4_doi_list_lock);
  450. return -EEXIST;
  451. }
  452. /**
  453. * cipso_v4_doi_free - Frees a DOI definition
  454. * @entry: the entry's RCU field
  455. *
  456. * Description:
  457. * This function frees all of the memory associated with a DOI definition.
  458. *
  459. */
  460. void cipso_v4_doi_free(struct cipso_v4_doi *doi_def)
  461. {
  462. if (doi_def == NULL)
  463. return;
  464. switch (doi_def->type) {
  465. case CIPSO_V4_MAP_TRANS:
  466. kfree(doi_def->map.std->lvl.cipso);
  467. kfree(doi_def->map.std->lvl.local);
  468. kfree(doi_def->map.std->cat.cipso);
  469. kfree(doi_def->map.std->cat.local);
  470. break;
  471. }
  472. kfree(doi_def);
  473. }
  474. /**
  475. * cipso_v4_doi_free_rcu - Frees a DOI definition via the RCU pointer
  476. * @entry: the entry's RCU field
  477. *
  478. * Description:
  479. * This function is designed to be used as a callback to the call_rcu()
  480. * function so that the memory allocated to the DOI definition can be released
  481. * safely.
  482. *
  483. */
  484. static void cipso_v4_doi_free_rcu(struct rcu_head *entry)
  485. {
  486. struct cipso_v4_doi *doi_def;
  487. doi_def = container_of(entry, struct cipso_v4_doi, rcu);
  488. cipso_v4_doi_free(doi_def);
  489. }
  490. /**
  491. * cipso_v4_doi_remove - Remove an existing DOI from the CIPSO protocol engine
  492. * @doi: the DOI value
  493. * @audit_secid: the LSM secid to use in the audit message
  494. *
  495. * Description:
  496. * Removes a DOI definition from the CIPSO engine. The NetLabel routines will
  497. * be called to release their own LSM domain mappings as well as our own
  498. * domain list. Returns zero on success and negative values on failure.
  499. *
  500. */
  501. int cipso_v4_doi_remove(u32 doi, struct netlbl_audit *audit_info)
  502. {
  503. struct cipso_v4_doi *doi_def;
  504. spin_lock(&cipso_v4_doi_list_lock);
  505. doi_def = cipso_v4_doi_search(doi);
  506. if (doi_def == NULL) {
  507. spin_unlock(&cipso_v4_doi_list_lock);
  508. return -ENOENT;
  509. }
  510. if (!atomic_dec_and_test(&doi_def->refcount)) {
  511. spin_unlock(&cipso_v4_doi_list_lock);
  512. return -EBUSY;
  513. }
  514. list_del_rcu(&doi_def->list);
  515. spin_unlock(&cipso_v4_doi_list_lock);
  516. cipso_v4_cache_invalidate();
  517. call_rcu(&doi_def->rcu, cipso_v4_doi_free_rcu);
  518. return 0;
  519. }
  520. /**
  521. * cipso_v4_doi_getdef - Returns a reference to a valid DOI definition
  522. * @doi: the DOI value
  523. *
  524. * Description:
  525. * Searches for a valid DOI definition and if one is found it is returned to
  526. * the caller. Otherwise NULL is returned. The caller must ensure that
  527. * rcu_read_lock() is held while accessing the returned definition and the DOI
  528. * definition reference count is decremented when the caller is done.
  529. *
  530. */
  531. struct cipso_v4_doi *cipso_v4_doi_getdef(u32 doi)
  532. {
  533. struct cipso_v4_doi *doi_def;
  534. rcu_read_lock();
  535. doi_def = cipso_v4_doi_search(doi);
  536. if (doi_def == NULL)
  537. goto doi_getdef_return;
  538. if (!atomic_inc_not_zero(&doi_def->refcount))
  539. doi_def = NULL;
  540. doi_getdef_return:
  541. rcu_read_unlock();
  542. return doi_def;
  543. }
  544. /**
  545. * cipso_v4_doi_putdef - Releases a reference for the given DOI definition
  546. * @doi_def: the DOI definition
  547. *
  548. * Description:
  549. * Releases a DOI definition reference obtained from cipso_v4_doi_getdef().
  550. *
  551. */
  552. void cipso_v4_doi_putdef(struct cipso_v4_doi *doi_def)
  553. {
  554. if (doi_def == NULL)
  555. return;
  556. if (!atomic_dec_and_test(&doi_def->refcount))
  557. return;
  558. spin_lock(&cipso_v4_doi_list_lock);
  559. list_del_rcu(&doi_def->list);
  560. spin_unlock(&cipso_v4_doi_list_lock);
  561. cipso_v4_cache_invalidate();
  562. call_rcu(&doi_def->rcu, cipso_v4_doi_free_rcu);
  563. }
  564. /**
  565. * cipso_v4_doi_walk - Iterate through the DOI definitions
  566. * @skip_cnt: skip past this number of DOI definitions, updated
  567. * @callback: callback for each DOI definition
  568. * @cb_arg: argument for the callback function
  569. *
  570. * Description:
  571. * Iterate over the DOI definition list, skipping the first @skip_cnt entries.
  572. * For each entry call @callback, if @callback returns a negative value stop
  573. * 'walking' through the list and return. Updates the value in @skip_cnt upon
  574. * return. Returns zero on success, negative values on failure.
  575. *
  576. */
  577. int cipso_v4_doi_walk(u32 *skip_cnt,
  578. int (*callback) (struct cipso_v4_doi *doi_def, void *arg),
  579. void *cb_arg)
  580. {
  581. int ret_val = -ENOENT;
  582. u32 doi_cnt = 0;
  583. struct cipso_v4_doi *iter_doi;
  584. rcu_read_lock();
  585. list_for_each_entry_rcu(iter_doi, &cipso_v4_doi_list, list)
  586. if (atomic_read(&iter_doi->refcount) > 0) {
  587. if (doi_cnt++ < *skip_cnt)
  588. continue;
  589. ret_val = callback(iter_doi, cb_arg);
  590. if (ret_val < 0) {
  591. doi_cnt--;
  592. goto doi_walk_return;
  593. }
  594. }
  595. doi_walk_return:
  596. rcu_read_unlock();
  597. *skip_cnt = doi_cnt;
  598. return ret_val;
  599. }
  600. /*
  601. * Label Mapping Functions
  602. */
  603. /**
  604. * cipso_v4_map_lvl_valid - Checks to see if the given level is understood
  605. * @doi_def: the DOI definition
  606. * @level: the level to check
  607. *
  608. * Description:
  609. * Checks the given level against the given DOI definition and returns a
  610. * negative value if the level does not have a valid mapping and a zero value
  611. * if the level is defined by the DOI.
  612. *
  613. */
  614. static int cipso_v4_map_lvl_valid(const struct cipso_v4_doi *doi_def, u8 level)
  615. {
  616. switch (doi_def->type) {
  617. case CIPSO_V4_MAP_PASS:
  618. return 0;
  619. case CIPSO_V4_MAP_TRANS:
  620. if (doi_def->map.std->lvl.cipso[level] < CIPSO_V4_INV_LVL)
  621. return 0;
  622. break;
  623. }
  624. return -EFAULT;
  625. }
  626. /**
  627. * cipso_v4_map_lvl_hton - Perform a level mapping from the host to the network
  628. * @doi_def: the DOI definition
  629. * @host_lvl: the host MLS level
  630. * @net_lvl: the network/CIPSO MLS level
  631. *
  632. * Description:
  633. * Perform a label mapping to translate a local MLS level to the correct
  634. * CIPSO level using the given DOI definition. Returns zero on success,
  635. * negative values otherwise.
  636. *
  637. */
  638. static int cipso_v4_map_lvl_hton(const struct cipso_v4_doi *doi_def,
  639. u32 host_lvl,
  640. u32 *net_lvl)
  641. {
  642. switch (doi_def->type) {
  643. case CIPSO_V4_MAP_PASS:
  644. *net_lvl = host_lvl;
  645. return 0;
  646. case CIPSO_V4_MAP_TRANS:
  647. if (host_lvl < doi_def->map.std->lvl.local_size &&
  648. doi_def->map.std->lvl.local[host_lvl] < CIPSO_V4_INV_LVL) {
  649. *net_lvl = doi_def->map.std->lvl.local[host_lvl];
  650. return 0;
  651. }
  652. return -EPERM;
  653. }
  654. return -EINVAL;
  655. }
  656. /**
  657. * cipso_v4_map_lvl_ntoh - Perform a level mapping from the network to the host
  658. * @doi_def: the DOI definition
  659. * @net_lvl: the network/CIPSO MLS level
  660. * @host_lvl: the host MLS level
  661. *
  662. * Description:
  663. * Perform a label mapping to translate a CIPSO level to the correct local MLS
  664. * level using the given DOI definition. Returns zero on success, negative
  665. * values otherwise.
  666. *
  667. */
  668. static int cipso_v4_map_lvl_ntoh(const struct cipso_v4_doi *doi_def,
  669. u32 net_lvl,
  670. u32 *host_lvl)
  671. {
  672. struct cipso_v4_std_map_tbl *map_tbl;
  673. switch (doi_def->type) {
  674. case CIPSO_V4_MAP_PASS:
  675. *host_lvl = net_lvl;
  676. return 0;
  677. case CIPSO_V4_MAP_TRANS:
  678. map_tbl = doi_def->map.std;
  679. if (net_lvl < map_tbl->lvl.cipso_size &&
  680. map_tbl->lvl.cipso[net_lvl] < CIPSO_V4_INV_LVL) {
  681. *host_lvl = doi_def->map.std->lvl.cipso[net_lvl];
  682. return 0;
  683. }
  684. return -EPERM;
  685. }
  686. return -EINVAL;
  687. }
  688. /**
  689. * cipso_v4_map_cat_rbm_valid - Checks to see if the category bitmap is valid
  690. * @doi_def: the DOI definition
  691. * @bitmap: category bitmap
  692. * @bitmap_len: bitmap length in bytes
  693. *
  694. * Description:
  695. * Checks the given category bitmap against the given DOI definition and
  696. * returns a negative value if any of the categories in the bitmap do not have
  697. * a valid mapping and a zero value if all of the categories are valid.
  698. *
  699. */
  700. static int cipso_v4_map_cat_rbm_valid(const struct cipso_v4_doi *doi_def,
  701. const unsigned char *bitmap,
  702. u32 bitmap_len)
  703. {
  704. int cat = -1;
  705. u32 bitmap_len_bits = bitmap_len * 8;
  706. u32 cipso_cat_size;
  707. u32 *cipso_array;
  708. switch (doi_def->type) {
  709. case CIPSO_V4_MAP_PASS:
  710. return 0;
  711. case CIPSO_V4_MAP_TRANS:
  712. cipso_cat_size = doi_def->map.std->cat.cipso_size;
  713. cipso_array = doi_def->map.std->cat.cipso;
  714. for (;;) {
  715. cat = cipso_v4_bitmap_walk(bitmap,
  716. bitmap_len_bits,
  717. cat + 1,
  718. 1);
  719. if (cat < 0)
  720. break;
  721. if (cat >= cipso_cat_size ||
  722. cipso_array[cat] >= CIPSO_V4_INV_CAT)
  723. return -EFAULT;
  724. }
  725. if (cat == -1)
  726. return 0;
  727. break;
  728. }
  729. return -EFAULT;
  730. }
  731. /**
  732. * cipso_v4_map_cat_rbm_hton - Perform a category mapping from host to network
  733. * @doi_def: the DOI definition
  734. * @secattr: the security attributes
  735. * @net_cat: the zero'd out category bitmap in network/CIPSO format
  736. * @net_cat_len: the length of the CIPSO bitmap in bytes
  737. *
  738. * Description:
  739. * Perform a label mapping to translate a local MLS category bitmap to the
  740. * correct CIPSO bitmap using the given DOI definition. Returns the minimum
  741. * size in bytes of the network bitmap on success, negative values otherwise.
  742. *
  743. */
  744. static int cipso_v4_map_cat_rbm_hton(const struct cipso_v4_doi *doi_def,
  745. const struct netlbl_lsm_secattr *secattr,
  746. unsigned char *net_cat,
  747. u32 net_cat_len)
  748. {
  749. int host_spot = -1;
  750. u32 net_spot = CIPSO_V4_INV_CAT;
  751. u32 net_spot_max = 0;
  752. u32 net_clen_bits = net_cat_len * 8;
  753. u32 host_cat_size = 0;
  754. u32 *host_cat_array = NULL;
  755. if (doi_def->type == CIPSO_V4_MAP_TRANS) {
  756. host_cat_size = doi_def->map.std->cat.local_size;
  757. host_cat_array = doi_def->map.std->cat.local;
  758. }
  759. for (;;) {
  760. host_spot = netlbl_secattr_catmap_walk(secattr->attr.mls.cat,
  761. host_spot + 1);
  762. if (host_spot < 0)
  763. break;
  764. switch (doi_def->type) {
  765. case CIPSO_V4_MAP_PASS:
  766. net_spot = host_spot;
  767. break;
  768. case CIPSO_V4_MAP_TRANS:
  769. if (host_spot >= host_cat_size)
  770. return -EPERM;
  771. net_spot = host_cat_array[host_spot];
  772. if (net_spot >= CIPSO_V4_INV_CAT)
  773. return -EPERM;
  774. break;
  775. }
  776. if (net_spot >= net_clen_bits)
  777. return -ENOSPC;
  778. cipso_v4_bitmap_setbit(net_cat, net_spot, 1);
  779. if (net_spot > net_spot_max)
  780. net_spot_max = net_spot;
  781. }
  782. if (++net_spot_max % 8)
  783. return net_spot_max / 8 + 1;
  784. return net_spot_max / 8;
  785. }
  786. /**
  787. * cipso_v4_map_cat_rbm_ntoh - Perform a category mapping from network to host
  788. * @doi_def: the DOI definition
  789. * @net_cat: the category bitmap in network/CIPSO format
  790. * @net_cat_len: the length of the CIPSO bitmap in bytes
  791. * @secattr: the security attributes
  792. *
  793. * Description:
  794. * Perform a label mapping to translate a CIPSO bitmap to the correct local
  795. * MLS category bitmap using the given DOI definition. Returns zero on
  796. * success, negative values on failure.
  797. *
  798. */
  799. static int cipso_v4_map_cat_rbm_ntoh(const struct cipso_v4_doi *doi_def,
  800. const unsigned char *net_cat,
  801. u32 net_cat_len,
  802. struct netlbl_lsm_secattr *secattr)
  803. {
  804. int ret_val;
  805. int net_spot = -1;
  806. u32 host_spot = CIPSO_V4_INV_CAT;
  807. u32 net_clen_bits = net_cat_len * 8;
  808. u32 net_cat_size = 0;
  809. u32 *net_cat_array = NULL;
  810. if (doi_def->type == CIPSO_V4_MAP_TRANS) {
  811. net_cat_size = doi_def->map.std->cat.cipso_size;
  812. net_cat_array = doi_def->map.std->cat.cipso;
  813. }
  814. for (;;) {
  815. net_spot = cipso_v4_bitmap_walk(net_cat,
  816. net_clen_bits,
  817. net_spot + 1,
  818. 1);
  819. if (net_spot < 0) {
  820. if (net_spot == -2)
  821. return -EFAULT;
  822. return 0;
  823. }
  824. switch (doi_def->type) {
  825. case CIPSO_V4_MAP_PASS:
  826. host_spot = net_spot;
  827. break;
  828. case CIPSO_V4_MAP_TRANS:
  829. if (net_spot >= net_cat_size)
  830. return -EPERM;
  831. host_spot = net_cat_array[net_spot];
  832. if (host_spot >= CIPSO_V4_INV_CAT)
  833. return -EPERM;
  834. break;
  835. }
  836. ret_val = netlbl_secattr_catmap_setbit(secattr->attr.mls.cat,
  837. host_spot,
  838. GFP_ATOMIC);
  839. if (ret_val != 0)
  840. return ret_val;
  841. }
  842. return -EINVAL;
  843. }
  844. /**
  845. * cipso_v4_map_cat_enum_valid - Checks to see if the categories are valid
  846. * @doi_def: the DOI definition
  847. * @enumcat: category list
  848. * @enumcat_len: length of the category list in bytes
  849. *
  850. * Description:
  851. * Checks the given categories against the given DOI definition and returns a
  852. * negative value if any of the categories do not have a valid mapping and a
  853. * zero value if all of the categories are valid.
  854. *
  855. */
  856. static int cipso_v4_map_cat_enum_valid(const struct cipso_v4_doi *doi_def,
  857. const unsigned char *enumcat,
  858. u32 enumcat_len)
  859. {
  860. u16 cat;
  861. int cat_prev = -1;
  862. u32 iter;
  863. if (doi_def->type != CIPSO_V4_MAP_PASS || enumcat_len & 0x01)
  864. return -EFAULT;
  865. for (iter = 0; iter < enumcat_len; iter += 2) {
  866. cat = get_unaligned_be16(&enumcat[iter]);
  867. if (cat <= cat_prev)
  868. return -EFAULT;
  869. cat_prev = cat;
  870. }
  871. return 0;
  872. }
  873. /**
  874. * cipso_v4_map_cat_enum_hton - Perform a category mapping from host to network
  875. * @doi_def: the DOI definition
  876. * @secattr: the security attributes
  877. * @net_cat: the zero'd out category list in network/CIPSO format
  878. * @net_cat_len: the length of the CIPSO category list in bytes
  879. *
  880. * Description:
  881. * Perform a label mapping to translate a local MLS category bitmap to the
  882. * correct CIPSO category list using the given DOI definition. Returns the
  883. * size in bytes of the network category bitmap on success, negative values
  884. * otherwise.
  885. *
  886. */
  887. static int cipso_v4_map_cat_enum_hton(const struct cipso_v4_doi *doi_def,
  888. const struct netlbl_lsm_secattr *secattr,
  889. unsigned char *net_cat,
  890. u32 net_cat_len)
  891. {
  892. int cat = -1;
  893. u32 cat_iter = 0;
  894. for (;;) {
  895. cat = netlbl_secattr_catmap_walk(secattr->attr.mls.cat,
  896. cat + 1);
  897. if (cat < 0)
  898. break;
  899. if ((cat_iter + 2) > net_cat_len)
  900. return -ENOSPC;
  901. *((__be16 *)&net_cat[cat_iter]) = htons(cat);
  902. cat_iter += 2;
  903. }
  904. return cat_iter;
  905. }
  906. /**
  907. * cipso_v4_map_cat_enum_ntoh - Perform a category mapping from network to host
  908. * @doi_def: the DOI definition
  909. * @net_cat: the category list in network/CIPSO format
  910. * @net_cat_len: the length of the CIPSO bitmap in bytes
  911. * @secattr: the security attributes
  912. *
  913. * Description:
  914. * Perform a label mapping to translate a CIPSO category list to the correct
  915. * local MLS category bitmap using the given DOI definition. Returns zero on
  916. * success, negative values on failure.
  917. *
  918. */
  919. static int cipso_v4_map_cat_enum_ntoh(const struct cipso_v4_doi *doi_def,
  920. const unsigned char *net_cat,
  921. u32 net_cat_len,
  922. struct netlbl_lsm_secattr *secattr)
  923. {
  924. int ret_val;
  925. u32 iter;
  926. for (iter = 0; iter < net_cat_len; iter += 2) {
  927. ret_val = netlbl_secattr_catmap_setbit(secattr->attr.mls.cat,
  928. get_unaligned_be16(&net_cat[iter]),
  929. GFP_ATOMIC);
  930. if (ret_val != 0)
  931. return ret_val;
  932. }
  933. return 0;
  934. }
  935. /**
  936. * cipso_v4_map_cat_rng_valid - Checks to see if the categories are valid
  937. * @doi_def: the DOI definition
  938. * @rngcat: category list
  939. * @rngcat_len: length of the category list in bytes
  940. *
  941. * Description:
  942. * Checks the given categories against the given DOI definition and returns a
  943. * negative value if any of the categories do not have a valid mapping and a
  944. * zero value if all of the categories are valid.
  945. *
  946. */
  947. static int cipso_v4_map_cat_rng_valid(const struct cipso_v4_doi *doi_def,
  948. const unsigned char *rngcat,
  949. u32 rngcat_len)
  950. {
  951. u16 cat_high;
  952. u16 cat_low;
  953. u32 cat_prev = CIPSO_V4_MAX_REM_CATS + 1;
  954. u32 iter;
  955. if (doi_def->type != CIPSO_V4_MAP_PASS || rngcat_len & 0x01)
  956. return -EFAULT;
  957. for (iter = 0; iter < rngcat_len; iter += 4) {
  958. cat_high = get_unaligned_be16(&rngcat[iter]);
  959. if ((iter + 4) <= rngcat_len)
  960. cat_low = get_unaligned_be16(&rngcat[iter + 2]);
  961. else
  962. cat_low = 0;
  963. if (cat_high > cat_prev)
  964. return -EFAULT;
  965. cat_prev = cat_low;
  966. }
  967. return 0;
  968. }
  969. /**
  970. * cipso_v4_map_cat_rng_hton - Perform a category mapping from host to network
  971. * @doi_def: the DOI definition
  972. * @secattr: the security attributes
  973. * @net_cat: the zero'd out category list in network/CIPSO format
  974. * @net_cat_len: the length of the CIPSO category list in bytes
  975. *
  976. * Description:
  977. * Perform a label mapping to translate a local MLS category bitmap to the
  978. * correct CIPSO category list using the given DOI definition. Returns the
  979. * size in bytes of the network category bitmap on success, negative values
  980. * otherwise.
  981. *
  982. */
  983. static int cipso_v4_map_cat_rng_hton(const struct cipso_v4_doi *doi_def,
  984. const struct netlbl_lsm_secattr *secattr,
  985. unsigned char *net_cat,
  986. u32 net_cat_len)
  987. {
  988. int iter = -1;
  989. u16 array[CIPSO_V4_TAG_RNG_CAT_MAX * 2];
  990. u32 array_cnt = 0;
  991. u32 cat_size = 0;
  992. /* make sure we don't overflow the 'array[]' variable */
  993. if (net_cat_len >
  994. (CIPSO_V4_OPT_LEN_MAX - CIPSO_V4_HDR_LEN - CIPSO_V4_TAG_RNG_BLEN))
  995. return -ENOSPC;
  996. for (;;) {
  997. iter = netlbl_secattr_catmap_walk(secattr->attr.mls.cat,
  998. iter + 1);
  999. if (iter < 0)
  1000. break;
  1001. cat_size += (iter == 0 ? 0 : sizeof(u16));
  1002. if (cat_size > net_cat_len)
  1003. return -ENOSPC;
  1004. array[array_cnt++] = iter;
  1005. iter = netlbl_secattr_catmap_walk_rng(secattr->attr.mls.cat,
  1006. iter);
  1007. if (iter < 0)
  1008. return -EFAULT;
  1009. cat_size += sizeof(u16);
  1010. if (cat_size > net_cat_len)
  1011. return -ENOSPC;
  1012. array[array_cnt++] = iter;
  1013. }
  1014. for (iter = 0; array_cnt > 0;) {
  1015. *((__be16 *)&net_cat[iter]) = htons(array[--array_cnt]);
  1016. iter += 2;
  1017. array_cnt--;
  1018. if (array[array_cnt] != 0) {
  1019. *((__be16 *)&net_cat[iter]) = htons(array[array_cnt]);
  1020. iter += 2;
  1021. }
  1022. }
  1023. return cat_size;
  1024. }
  1025. /**
  1026. * cipso_v4_map_cat_rng_ntoh - Perform a category mapping from network to host
  1027. * @doi_def: the DOI definition
  1028. * @net_cat: the category list in network/CIPSO format
  1029. * @net_cat_len: the length of the CIPSO bitmap in bytes
  1030. * @secattr: the security attributes
  1031. *
  1032. * Description:
  1033. * Perform a label mapping to translate a CIPSO category list to the correct
  1034. * local MLS category bitmap using the given DOI definition. Returns zero on
  1035. * success, negative values on failure.
  1036. *
  1037. */
  1038. static int cipso_v4_map_cat_rng_ntoh(const struct cipso_v4_doi *doi_def,
  1039. const unsigned char *net_cat,
  1040. u32 net_cat_len,
  1041. struct netlbl_lsm_secattr *secattr)
  1042. {
  1043. int ret_val;
  1044. u32 net_iter;
  1045. u16 cat_low;
  1046. u16 cat_high;
  1047. for (net_iter = 0; net_iter < net_cat_len; net_iter += 4) {
  1048. cat_high = get_unaligned_be16(&net_cat[net_iter]);
  1049. if ((net_iter + 4) <= net_cat_len)
  1050. cat_low = get_unaligned_be16(&net_cat[net_iter + 2]);
  1051. else
  1052. cat_low = 0;
  1053. ret_val = netlbl_secattr_catmap_setrng(secattr->attr.mls.cat,
  1054. cat_low,
  1055. cat_high,
  1056. GFP_ATOMIC);
  1057. if (ret_val != 0)
  1058. return ret_val;
  1059. }
  1060. return 0;
  1061. }
  1062. /*
  1063. * Protocol Handling Functions
  1064. */
  1065. /**
  1066. * cipso_v4_gentag_hdr - Generate a CIPSO option header
  1067. * @doi_def: the DOI definition
  1068. * @len: the total tag length in bytes, not including this header
  1069. * @buf: the CIPSO option buffer
  1070. *
  1071. * Description:
  1072. * Write a CIPSO header into the beginning of @buffer.
  1073. *
  1074. */
  1075. static void cipso_v4_gentag_hdr(const struct cipso_v4_doi *doi_def,
  1076. unsigned char *buf,
  1077. u32 len)
  1078. {
  1079. buf[0] = IPOPT_CIPSO;
  1080. buf[1] = CIPSO_V4_HDR_LEN + len;
  1081. *(__be32 *)&buf[2] = htonl(doi_def->doi);
  1082. }
  1083. /**
  1084. * cipso_v4_gentag_rbm - Generate a CIPSO restricted bitmap tag (type #1)
  1085. * @doi_def: the DOI definition
  1086. * @secattr: the security attributes
  1087. * @buffer: the option buffer
  1088. * @buffer_len: length of buffer in bytes
  1089. *
  1090. * Description:
  1091. * Generate a CIPSO option using the restricted bitmap tag, tag type #1. The
  1092. * actual buffer length may be larger than the indicated size due to
  1093. * translation between host and network category bitmaps. Returns the size of
  1094. * the tag on success, negative values on failure.
  1095. *
  1096. */
  1097. static int cipso_v4_gentag_rbm(const struct cipso_v4_doi *doi_def,
  1098. const struct netlbl_lsm_secattr *secattr,
  1099. unsigned char *buffer,
  1100. u32 buffer_len)
  1101. {
  1102. int ret_val;
  1103. u32 tag_len;
  1104. u32 level;
  1105. if ((secattr->flags & NETLBL_SECATTR_MLS_LVL) == 0)
  1106. return -EPERM;
  1107. ret_val = cipso_v4_map_lvl_hton(doi_def,
  1108. secattr->attr.mls.lvl,
  1109. &level);
  1110. if (ret_val != 0)
  1111. return ret_val;
  1112. if (secattr->flags & NETLBL_SECATTR_MLS_CAT) {
  1113. ret_val = cipso_v4_map_cat_rbm_hton(doi_def,
  1114. secattr,
  1115. &buffer[4],
  1116. buffer_len - 4);
  1117. if (ret_val < 0)
  1118. return ret_val;
  1119. /* This will send packets using the "optimized" format when
  1120. * possibile as specified in section 3.4.2.6 of the
  1121. * CIPSO draft. */
  1122. if (cipso_v4_rbm_optfmt && ret_val > 0 && ret_val <= 10)
  1123. tag_len = 14;
  1124. else
  1125. tag_len = 4 + ret_val;
  1126. } else
  1127. tag_len = 4;
  1128. buffer[0] = CIPSO_V4_TAG_RBITMAP;
  1129. buffer[1] = tag_len;
  1130. buffer[3] = level;
  1131. return tag_len;
  1132. }
  1133. /**
  1134. * cipso_v4_parsetag_rbm - Parse a CIPSO restricted bitmap tag
  1135. * @doi_def: the DOI definition
  1136. * @tag: the CIPSO tag
  1137. * @secattr: the security attributes
  1138. *
  1139. * Description:
  1140. * Parse a CIPSO restricted bitmap tag (tag type #1) and return the security
  1141. * attributes in @secattr. Return zero on success, negatives values on
  1142. * failure.
  1143. *
  1144. */
  1145. static int cipso_v4_parsetag_rbm(const struct cipso_v4_doi *doi_def,
  1146. const unsigned char *tag,
  1147. struct netlbl_lsm_secattr *secattr)
  1148. {
  1149. int ret_val;
  1150. u8 tag_len = tag[1];
  1151. u32 level;
  1152. ret_val = cipso_v4_map_lvl_ntoh(doi_def, tag[3], &level);
  1153. if (ret_val != 0)
  1154. return ret_val;
  1155. secattr->attr.mls.lvl = level;
  1156. secattr->flags |= NETLBL_SECATTR_MLS_LVL;
  1157. if (tag_len > 4) {
  1158. secattr->attr.mls.cat =
  1159. netlbl_secattr_catmap_alloc(GFP_ATOMIC);
  1160. if (secattr->attr.mls.cat == NULL)
  1161. return -ENOMEM;
  1162. ret_val = cipso_v4_map_cat_rbm_ntoh(doi_def,
  1163. &tag[4],
  1164. tag_len - 4,
  1165. secattr);
  1166. if (ret_val != 0) {
  1167. netlbl_secattr_catmap_free(secattr->attr.mls.cat);
  1168. return ret_val;
  1169. }
  1170. secattr->flags |= NETLBL_SECATTR_MLS_CAT;
  1171. }
  1172. return 0;
  1173. }
  1174. /**
  1175. * cipso_v4_gentag_enum - Generate a CIPSO enumerated tag (type #2)
  1176. * @doi_def: the DOI definition
  1177. * @secattr: the security attributes
  1178. * @buffer: the option buffer
  1179. * @buffer_len: length of buffer in bytes
  1180. *
  1181. * Description:
  1182. * Generate a CIPSO option using the enumerated tag, tag type #2. Returns the
  1183. * size of the tag on success, negative values on failure.
  1184. *
  1185. */
  1186. static int cipso_v4_gentag_enum(const struct cipso_v4_doi *doi_def,
  1187. const struct netlbl_lsm_secattr *secattr,
  1188. unsigned char *buffer,
  1189. u32 buffer_len)
  1190. {
  1191. int ret_val;
  1192. u32 tag_len;
  1193. u32 level;
  1194. if (!(secattr->flags & NETLBL_SECATTR_MLS_LVL))
  1195. return -EPERM;
  1196. ret_val = cipso_v4_map_lvl_hton(doi_def,
  1197. secattr->attr.mls.lvl,
  1198. &level);
  1199. if (ret_val != 0)
  1200. return ret_val;
  1201. if (secattr->flags & NETLBL_SECATTR_MLS_CAT) {
  1202. ret_val = cipso_v4_map_cat_enum_hton(doi_def,
  1203. secattr,
  1204. &buffer[4],
  1205. buffer_len - 4);
  1206. if (ret_val < 0)
  1207. return ret_val;
  1208. tag_len = 4 + ret_val;
  1209. } else
  1210. tag_len = 4;
  1211. buffer[0] = CIPSO_V4_TAG_ENUM;
  1212. buffer[1] = tag_len;
  1213. buffer[3] = level;
  1214. return tag_len;
  1215. }
  1216. /**
  1217. * cipso_v4_parsetag_enum - Parse a CIPSO enumerated tag
  1218. * @doi_def: the DOI definition
  1219. * @tag: the CIPSO tag
  1220. * @secattr: the security attributes
  1221. *
  1222. * Description:
  1223. * Parse a CIPSO enumerated tag (tag type #2) and return the security
  1224. * attributes in @secattr. Return zero on success, negatives values on
  1225. * failure.
  1226. *
  1227. */
  1228. static int cipso_v4_parsetag_enum(const struct cipso_v4_doi *doi_def,
  1229. const unsigned char *tag,
  1230. struct netlbl_lsm_secattr *secattr)
  1231. {
  1232. int ret_val;
  1233. u8 tag_len = tag[1];
  1234. u32 level;
  1235. ret_val = cipso_v4_map_lvl_ntoh(doi_def, tag[3], &level);
  1236. if (ret_val != 0)
  1237. return ret_val;
  1238. secattr->attr.mls.lvl = level;
  1239. secattr->flags |= NETLBL_SECATTR_MLS_LVL;
  1240. if (tag_len > 4) {
  1241. secattr->attr.mls.cat =
  1242. netlbl_secattr_catmap_alloc(GFP_ATOMIC);
  1243. if (secattr->attr.mls.cat == NULL)
  1244. return -ENOMEM;
  1245. ret_val = cipso_v4_map_cat_enum_ntoh(doi_def,
  1246. &tag[4],
  1247. tag_len - 4,
  1248. secattr);
  1249. if (ret_val != 0) {
  1250. netlbl_secattr_catmap_free(secattr->attr.mls.cat);
  1251. return ret_val;
  1252. }
  1253. secattr->flags |= NETLBL_SECATTR_MLS_CAT;
  1254. }
  1255. return 0;
  1256. }
  1257. /**
  1258. * cipso_v4_gentag_rng - Generate a CIPSO ranged tag (type #5)
  1259. * @doi_def: the DOI definition
  1260. * @secattr: the security attributes
  1261. * @buffer: the option buffer
  1262. * @buffer_len: length of buffer in bytes
  1263. *
  1264. * Description:
  1265. * Generate a CIPSO option using the ranged tag, tag type #5. Returns the
  1266. * size of the tag on success, negative values on failure.
  1267. *
  1268. */
  1269. static int cipso_v4_gentag_rng(const struct cipso_v4_doi *doi_def,
  1270. const struct netlbl_lsm_secattr *secattr,
  1271. unsigned char *buffer,
  1272. u32 buffer_len)
  1273. {
  1274. int ret_val;
  1275. u32 tag_len;
  1276. u32 level;
  1277. if (!(secattr->flags & NETLBL_SECATTR_MLS_LVL))
  1278. return -EPERM;
  1279. ret_val = cipso_v4_map_lvl_hton(doi_def,
  1280. secattr->attr.mls.lvl,
  1281. &level);
  1282. if (ret_val != 0)
  1283. return ret_val;
  1284. if (secattr->flags & NETLBL_SECATTR_MLS_CAT) {
  1285. ret_val = cipso_v4_map_cat_rng_hton(doi_def,
  1286. secattr,
  1287. &buffer[4],
  1288. buffer_len - 4);
  1289. if (ret_val < 0)
  1290. return ret_val;
  1291. tag_len = 4 + ret_val;
  1292. } else
  1293. tag_len = 4;
  1294. buffer[0] = CIPSO_V4_TAG_RANGE;
  1295. buffer[1] = tag_len;
  1296. buffer[3] = level;
  1297. return tag_len;
  1298. }
  1299. /**
  1300. * cipso_v4_parsetag_rng - Parse a CIPSO ranged tag
  1301. * @doi_def: the DOI definition
  1302. * @tag: the CIPSO tag
  1303. * @secattr: the security attributes
  1304. *
  1305. * Description:
  1306. * Parse a CIPSO ranged tag (tag type #5) and return the security attributes
  1307. * in @secattr. Return zero on success, negatives values on failure.
  1308. *
  1309. */
  1310. static int cipso_v4_parsetag_rng(const struct cipso_v4_doi *doi_def,
  1311. const unsigned char *tag,
  1312. struct netlbl_lsm_secattr *secattr)
  1313. {
  1314. int ret_val;
  1315. u8 tag_len = tag[1];
  1316. u32 level;
  1317. ret_val = cipso_v4_map_lvl_ntoh(doi_def, tag[3], &level);
  1318. if (ret_val != 0)
  1319. return ret_val;
  1320. secattr->attr.mls.lvl = level;
  1321. secattr->flags |= NETLBL_SECATTR_MLS_LVL;
  1322. if (tag_len > 4) {
  1323. secattr->attr.mls.cat =
  1324. netlbl_secattr_catmap_alloc(GFP_ATOMIC);
  1325. if (secattr->attr.mls.cat == NULL)
  1326. return -ENOMEM;
  1327. ret_val = cipso_v4_map_cat_rng_ntoh(doi_def,
  1328. &tag[4],
  1329. tag_len - 4,
  1330. secattr);
  1331. if (ret_val != 0) {
  1332. netlbl_secattr_catmap_free(secattr->attr.mls.cat);
  1333. return ret_val;
  1334. }
  1335. secattr->flags |= NETLBL_SECATTR_MLS_CAT;
  1336. }
  1337. return 0;
  1338. }
  1339. /**
  1340. * cipso_v4_gentag_loc - Generate a CIPSO local tag (non-standard)
  1341. * @doi_def: the DOI definition
  1342. * @secattr: the security attributes
  1343. * @buffer: the option buffer
  1344. * @buffer_len: length of buffer in bytes
  1345. *
  1346. * Description:
  1347. * Generate a CIPSO option using the local tag. Returns the size of the tag
  1348. * on success, negative values on failure.
  1349. *
  1350. */
  1351. static int cipso_v4_gentag_loc(const struct cipso_v4_doi *doi_def,
  1352. const struct netlbl_lsm_secattr *secattr,
  1353. unsigned char *buffer,
  1354. u32 buffer_len)
  1355. {
  1356. if (!(secattr->flags & NETLBL_SECATTR_SECID))
  1357. return -EPERM;
  1358. buffer[0] = CIPSO_V4_TAG_LOCAL;
  1359. buffer[1] = CIPSO_V4_TAG_LOC_BLEN;
  1360. *(u32 *)&buffer[2] = secattr->attr.secid;
  1361. return CIPSO_V4_TAG_LOC_BLEN;
  1362. }
  1363. /**
  1364. * cipso_v4_parsetag_loc - Parse a CIPSO local tag
  1365. * @doi_def: the DOI definition
  1366. * @tag: the CIPSO tag
  1367. * @secattr: the security attributes
  1368. *
  1369. * Description:
  1370. * Parse a CIPSO local tag and return the security attributes in @secattr.
  1371. * Return zero on success, negatives values on failure.
  1372. *
  1373. */
  1374. static int cipso_v4_parsetag_loc(const struct cipso_v4_doi *doi_def,
  1375. const unsigned char *tag,
  1376. struct netlbl_lsm_secattr *secattr)
  1377. {
  1378. secattr->attr.secid = *(u32 *)&tag[2];
  1379. secattr->flags |= NETLBL_SECATTR_SECID;
  1380. return 0;
  1381. }
  1382. /**
  1383. * cipso_v4_validate - Validate a CIPSO option
  1384. * @option: the start of the option, on error it is set to point to the error
  1385. *
  1386. * Description:
  1387. * This routine is called to validate a CIPSO option, it checks all of the
  1388. * fields to ensure that they are at least valid, see the draft snippet below
  1389. * for details. If the option is valid then a zero value is returned and
  1390. * the value of @option is unchanged. If the option is invalid then a
  1391. * non-zero value is returned and @option is adjusted to point to the
  1392. * offending portion of the option. From the IETF draft ...
  1393. *
  1394. * "If any field within the CIPSO options, such as the DOI identifier, is not
  1395. * recognized the IP datagram is discarded and an ICMP 'parameter problem'
  1396. * (type 12) is generated and returned. The ICMP code field is set to 'bad
  1397. * parameter' (code 0) and the pointer is set to the start of the CIPSO field
  1398. * that is unrecognized."
  1399. *
  1400. */
  1401. int cipso_v4_validate(const struct sk_buff *skb, unsigned char **option)
  1402. {
  1403. unsigned char *opt = *option;
  1404. unsigned char *tag;
  1405. unsigned char opt_iter;
  1406. unsigned char err_offset = 0;
  1407. u8 opt_len;
  1408. u8 tag_len;
  1409. struct cipso_v4_doi *doi_def = NULL;
  1410. u32 tag_iter;
  1411. /* caller already checks for length values that are too large */
  1412. opt_len = opt[1];
  1413. if (opt_len < 8) {
  1414. err_offset = 1;
  1415. goto validate_return;
  1416. }
  1417. rcu_read_lock();
  1418. doi_def = cipso_v4_doi_search(get_unaligned_be32(&opt[2]));
  1419. if (doi_def == NULL) {
  1420. err_offset = 2;
  1421. goto validate_return_locked;
  1422. }
  1423. opt_iter = CIPSO_V4_HDR_LEN;
  1424. tag = opt + opt_iter;
  1425. while (opt_iter < opt_len) {
  1426. for (tag_iter = 0; doi_def->tags[tag_iter] != tag[0];)
  1427. if (doi_def->tags[tag_iter] == CIPSO_V4_TAG_INVALID ||
  1428. ++tag_iter == CIPSO_V4_TAG_MAXCNT) {
  1429. err_offset = opt_iter;
  1430. goto validate_return_locked;
  1431. }
  1432. tag_len = tag[1];
  1433. if (tag_len > (opt_len - opt_iter)) {
  1434. err_offset = opt_iter + 1;
  1435. goto validate_return_locked;
  1436. }
  1437. switch (tag[0]) {
  1438. case CIPSO_V4_TAG_RBITMAP:
  1439. if (tag_len < CIPSO_V4_TAG_RBM_BLEN) {
  1440. err_offset = opt_iter + 1;
  1441. goto validate_return_locked;
  1442. }
  1443. /* We are already going to do all the verification
  1444. * necessary at the socket layer so from our point of
  1445. * view it is safe to turn these checks off (and less
  1446. * work), however, the CIPSO draft says we should do
  1447. * all the CIPSO validations here but it doesn't
  1448. * really specify _exactly_ what we need to validate
  1449. * ... so, just make it a sysctl tunable. */
  1450. if (cipso_v4_rbm_strictvalid) {
  1451. if (cipso_v4_map_lvl_valid(doi_def,
  1452. tag[3]) < 0) {
  1453. err_offset = opt_iter + 3;
  1454. goto validate_return_locked;
  1455. }
  1456. if (tag_len > CIPSO_V4_TAG_RBM_BLEN &&
  1457. cipso_v4_map_cat_rbm_valid(doi_def,
  1458. &tag[4],
  1459. tag_len - 4) < 0) {
  1460. err_offset = opt_iter + 4;
  1461. goto validate_return_locked;
  1462. }
  1463. }
  1464. break;
  1465. case CIPSO_V4_TAG_ENUM:
  1466. if (tag_len < CIPSO_V4_TAG_ENUM_BLEN) {
  1467. err_offset = opt_iter + 1;
  1468. goto validate_return_locked;
  1469. }
  1470. if (cipso_v4_map_lvl_valid(doi_def,
  1471. tag[3]) < 0) {
  1472. err_offset = opt_iter + 3;
  1473. goto validate_return_locked;
  1474. }
  1475. if (tag_len > CIPSO_V4_TAG_ENUM_BLEN &&
  1476. cipso_v4_map_cat_enum_valid(doi_def,
  1477. &tag[4],
  1478. tag_len - 4) < 0) {
  1479. err_offset = opt_iter + 4;
  1480. goto validate_return_locked;
  1481. }
  1482. break;
  1483. case CIPSO_V4_TAG_RANGE:
  1484. if (tag_len < CIPSO_V4_TAG_RNG_BLEN) {
  1485. err_offset = opt_iter + 1;
  1486. goto validate_return_locked;
  1487. }
  1488. if (cipso_v4_map_lvl_valid(doi_def,
  1489. tag[3]) < 0) {
  1490. err_offset = opt_iter + 3;
  1491. goto validate_return_locked;
  1492. }
  1493. if (tag_len > CIPSO_V4_TAG_RNG_BLEN &&
  1494. cipso_v4_map_cat_rng_valid(doi_def,
  1495. &tag[4],
  1496. tag_len - 4) < 0) {
  1497. err_offset = opt_iter + 4;
  1498. goto validate_return_locked;
  1499. }
  1500. break;
  1501. case CIPSO_V4_TAG_LOCAL:
  1502. /* This is a non-standard tag that we only allow for
  1503. * local connections, so if the incoming interface is
  1504. * not the loopback device drop the packet. */
  1505. if (!(skb->dev->flags & IFF_LOOPBACK)) {
  1506. err_offset = opt_iter;
  1507. goto validate_return_locked;
  1508. }
  1509. if (tag_len != CIPSO_V4_TAG_LOC_BLEN) {
  1510. err_offset = opt_iter + 1;
  1511. goto validate_return_locked;
  1512. }
  1513. break;
  1514. default:
  1515. err_offset = opt_iter;
  1516. goto validate_return_locked;
  1517. }
  1518. tag += tag_len;
  1519. opt_iter += tag_len;
  1520. }
  1521. validate_return_locked:
  1522. rcu_read_unlock();
  1523. validate_return:
  1524. *option = opt + err_offset;
  1525. return err_offset;
  1526. }
  1527. /**
  1528. * cipso_v4_error - Send the correct reponse for a bad packet
  1529. * @skb: the packet
  1530. * @error: the error code
  1531. * @gateway: CIPSO gateway flag
  1532. *
  1533. * Description:
  1534. * Based on the error code given in @error, send an ICMP error message back to
  1535. * the originating host. From the IETF draft ...
  1536. *
  1537. * "If the contents of the CIPSO [option] are valid but the security label is
  1538. * outside of the configured host or port label range, the datagram is
  1539. * discarded and an ICMP 'destination unreachable' (type 3) is generated and
  1540. * returned. The code field of the ICMP is set to 'communication with
  1541. * destination network administratively prohibited' (code 9) or to
  1542. * 'communication with destination host administratively prohibited'
  1543. * (code 10). The value of the code is dependent on whether the originator
  1544. * of the ICMP message is acting as a CIPSO host or a CIPSO gateway. The
  1545. * recipient of the ICMP message MUST be able to handle either value. The
  1546. * same procedure is performed if a CIPSO [option] can not be added to an
  1547. * IP packet because it is too large to fit in the IP options area."
  1548. *
  1549. * "If the error is triggered by receipt of an ICMP message, the message is
  1550. * discarded and no response is permitted (consistent with general ICMP
  1551. * processing rules)."
  1552. *
  1553. */
  1554. void cipso_v4_error(struct sk_buff *skb, int error, u32 gateway)
  1555. {
  1556. if (ip_hdr(skb)->protocol == IPPROTO_ICMP || error != -EACCES)
  1557. return;
  1558. if (gateway)
  1559. icmp_send(skb, ICMP_DEST_UNREACH, ICMP_NET_ANO, 0);
  1560. else
  1561. icmp_send(skb, ICMP_DEST_UNREACH, ICMP_HOST_ANO, 0);
  1562. }
  1563. /**
  1564. * cipso_v4_genopt - Generate a CIPSO option
  1565. * @buf: the option buffer
  1566. * @buf_len: the size of opt_buf
  1567. * @doi_def: the CIPSO DOI to use
  1568. * @secattr: the security attributes
  1569. *
  1570. * Description:
  1571. * Generate a CIPSO option using the DOI definition and security attributes
  1572. * passed to the function. Returns the length of the option on success and
  1573. * negative values on failure.
  1574. *
  1575. */
  1576. static int cipso_v4_genopt(unsigned char *buf, u32 buf_len,
  1577. const struct cipso_v4_doi *doi_def,
  1578. const struct netlbl_lsm_secattr *secattr)
  1579. {
  1580. int ret_val;
  1581. u32 iter;
  1582. if (buf_len <= CIPSO_V4_HDR_LEN)
  1583. return -ENOSPC;
  1584. /* XXX - This code assumes only one tag per CIPSO option which isn't
  1585. * really a good assumption to make but since we only support the MAC
  1586. * tags right now it is a safe assumption. */
  1587. iter = 0;
  1588. do {
  1589. memset(buf, 0, buf_len);
  1590. switch (doi_def->tags[iter]) {
  1591. case CIPSO_V4_TAG_RBITMAP:
  1592. ret_val = cipso_v4_gentag_rbm(doi_def,
  1593. secattr,
  1594. &buf[CIPSO_V4_HDR_LEN],
  1595. buf_len - CIPSO_V4_HDR_LEN);
  1596. break;
  1597. case CIPSO_V4_TAG_ENUM:
  1598. ret_val = cipso_v4_gentag_enum(doi_def,
  1599. secattr,
  1600. &buf[CIPSO_V4_HDR_LEN],
  1601. buf_len - CIPSO_V4_HDR_LEN);
  1602. break;
  1603. case CIPSO_V4_TAG_RANGE:
  1604. ret_val = cipso_v4_gentag_rng(doi_def,
  1605. secattr,
  1606. &buf[CIPSO_V4_HDR_LEN],
  1607. buf_len - CIPSO_V4_HDR_LEN);
  1608. break;
  1609. case CIPSO_V4_TAG_LOCAL:
  1610. ret_val = cipso_v4_gentag_loc(doi_def,
  1611. secattr,
  1612. &buf[CIPSO_V4_HDR_LEN],
  1613. buf_len - CIPSO_V4_HDR_LEN);
  1614. break;
  1615. default:
  1616. return -EPERM;
  1617. }
  1618. iter++;
  1619. } while (ret_val < 0 &&
  1620. iter < CIPSO_V4_TAG_MAXCNT &&
  1621. doi_def->tags[iter] != CIPSO_V4_TAG_INVALID);
  1622. if (ret_val < 0)
  1623. return ret_val;
  1624. cipso_v4_gentag_hdr(doi_def, buf, ret_val);
  1625. return CIPSO_V4_HDR_LEN + ret_val;
  1626. }
  1627. /**
  1628. * cipso_v4_sock_setattr - Add a CIPSO option to a socket
  1629. * @sk: the socket
  1630. * @doi_def: the CIPSO DOI to use
  1631. * @secattr: the specific security attributes of the socket
  1632. *
  1633. * Description:
  1634. * Set the CIPSO option on the given socket using the DOI definition and
  1635. * security attributes passed to the function. This function requires
  1636. * exclusive access to @sk, which means it either needs to be in the
  1637. * process of being created or locked. Returns zero on success and negative
  1638. * values on failure.
  1639. *
  1640. */
  1641. int cipso_v4_sock_setattr(struct sock *sk,
  1642. const struct cipso_v4_doi *doi_def,
  1643. const struct netlbl_lsm_secattr *secattr)
  1644. {
  1645. int ret_val = -EPERM;
  1646. unsigned char *buf = NULL;
  1647. u32 buf_len;
  1648. u32 opt_len;
  1649. struct ip_options *opt = NULL;
  1650. struct inet_sock *sk_inet;
  1651. struct inet_connection_sock *sk_conn;
  1652. /* In the case of sock_create_lite(), the sock->sk field is not
  1653. * defined yet but it is not a problem as the only users of these
  1654. * "lite" PF_INET sockets are functions which do an accept() call
  1655. * afterwards so we will label the socket as part of the accept(). */
  1656. if (sk == NULL)
  1657. return 0;
  1658. /* We allocate the maximum CIPSO option size here so we are probably
  1659. * being a little wasteful, but it makes our life _much_ easier later
  1660. * on and after all we are only talking about 40 bytes. */
  1661. buf_len = CIPSO_V4_OPT_LEN_MAX;
  1662. buf = kmalloc(buf_len, GFP_ATOMIC);
  1663. if (buf == NULL) {
  1664. ret_val = -ENOMEM;
  1665. goto socket_setattr_failure;
  1666. }
  1667. ret_val = cipso_v4_genopt(buf, buf_len, doi_def, secattr);
  1668. if (ret_val < 0)
  1669. goto socket_setattr_failure;
  1670. buf_len = ret_val;
  1671. /* We can't use ip_options_get() directly because it makes a call to
  1672. * ip_options_get_alloc() which allocates memory with GFP_KERNEL and
  1673. * we won't always have CAP_NET_RAW even though we _always_ want to
  1674. * set the IPOPT_CIPSO option. */
  1675. opt_len = (buf_len + 3) & ~3;
  1676. opt = kzalloc(sizeof(*opt) + opt_len, GFP_ATOMIC);
  1677. if (opt == NULL) {
  1678. ret_val = -ENOMEM;
  1679. goto socket_setattr_failure;
  1680. }
  1681. memcpy(opt->__data, buf, buf_len);
  1682. opt->optlen = opt_len;
  1683. opt->cipso = sizeof(struct iphdr);
  1684. kfree(buf);
  1685. buf = NULL;
  1686. sk_inet = inet_sk(sk);
  1687. if (sk_inet->is_icsk) {
  1688. sk_conn = inet_csk(sk);
  1689. if (sk_inet->opt)
  1690. sk_conn->icsk_ext_hdr_len -= sk_inet->opt->optlen;
  1691. sk_conn->icsk_ext_hdr_len += opt->optlen;
  1692. sk_conn->icsk_sync_mss(sk, sk_conn->icsk_pmtu_cookie);
  1693. }
  1694. opt = xchg(&sk_inet->opt, opt);
  1695. kfree(opt);
  1696. return 0;
  1697. socket_setattr_failure:
  1698. kfree(buf);
  1699. kfree(opt);
  1700. return ret_val;
  1701. }
  1702. /**
  1703. * cipso_v4_sock_delattr - Delete the CIPSO option from a socket
  1704. * @sk: the socket
  1705. *
  1706. * Description:
  1707. * Removes the CIPSO option from a socket, if present.
  1708. *
  1709. */
  1710. void cipso_v4_sock_delattr(struct sock *sk)
  1711. {
  1712. u8 hdr_delta;
  1713. struct ip_options *opt;
  1714. struct inet_sock *sk_inet;
  1715. sk_inet = inet_sk(sk);
  1716. opt = sk_inet->opt;
  1717. if (opt == NULL || opt->cipso == 0)
  1718. return;
  1719. if (opt->srr || opt->rr || opt->ts || opt->router_alert) {
  1720. u8 cipso_len;
  1721. u8 cipso_off;
  1722. unsigned char *cipso_ptr;
  1723. int iter;
  1724. int optlen_new;
  1725. cipso_off = opt->cipso - sizeof(struct iphdr);
  1726. cipso_ptr = &opt->__data[cipso_off];
  1727. cipso_len = cipso_ptr[1];
  1728. if (opt->srr > opt->cipso)
  1729. opt->srr -= cipso_len;
  1730. if (opt->rr > opt->cipso)
  1731. opt->rr -= cipso_len;
  1732. if (opt->ts > opt->cipso)
  1733. opt->ts -= cipso_len;
  1734. if (opt->router_alert > opt->cipso)
  1735. opt->router_alert -= cipso_len;
  1736. opt->cipso = 0;
  1737. memmove(cipso_ptr, cipso_ptr + cipso_len,
  1738. opt->optlen - cipso_off - cipso_len);
  1739. /* determining the new total option length is tricky because of
  1740. * the padding necessary, the only thing i can think to do at
  1741. * this point is walk the options one-by-one, skipping the
  1742. * padding at the end to determine the actual option size and
  1743. * from there we can determine the new total option length */
  1744. iter = 0;
  1745. optlen_new = 0;
  1746. while (iter < opt->optlen)
  1747. if (opt->__data[iter] != IPOPT_NOP) {
  1748. iter += opt->__data[iter + 1];
  1749. optlen_new = iter;
  1750. } else
  1751. iter++;
  1752. hdr_delta = opt->optlen;
  1753. opt->optlen = (optlen_new + 3) & ~3;
  1754. hdr_delta -= opt->optlen;
  1755. } else {
  1756. /* only the cipso option was present on the socket so we can
  1757. * remove the entire option struct */
  1758. sk_inet->opt = NULL;
  1759. hdr_delta = opt->optlen;
  1760. kfree(opt);
  1761. }
  1762. if (sk_inet->is_icsk && hdr_delta > 0) {
  1763. struct inet_connection_sock *sk_conn = inet_csk(sk);
  1764. sk_conn->icsk_ext_hdr_len -= hdr_delta;
  1765. sk_conn->icsk_sync_mss(sk, sk_conn->icsk_pmtu_cookie);
  1766. }
  1767. }
  1768. /**
  1769. * cipso_v4_getattr - Helper function for the cipso_v4_*_getattr functions
  1770. * @cipso: the CIPSO v4 option
  1771. * @secattr: the security attributes
  1772. *
  1773. * Description:
  1774. * Inspect @cipso and return the security attributes in @secattr. Returns zero
  1775. * on success and negative values on failure.
  1776. *
  1777. */
  1778. static int cipso_v4_getattr(const unsigned char *cipso,
  1779. struct netlbl_lsm_secattr *secattr)
  1780. {
  1781. int ret_val = -ENOMSG;
  1782. u32 doi;
  1783. struct cipso_v4_doi *doi_def;
  1784. if (cipso_v4_cache_check(cipso, cipso[1], secattr) == 0)
  1785. return 0;
  1786. doi = get_unaligned_be32(&cipso[2]);
  1787. rcu_read_lock();
  1788. doi_def = cipso_v4_doi_search(doi);
  1789. if (doi_def == NULL)
  1790. goto getattr_return;
  1791. /* XXX - This code assumes only one tag per CIPSO option which isn't
  1792. * really a good assumption to make but since we only support the MAC
  1793. * tags right now it is a safe assumption. */
  1794. switch (cipso[6]) {
  1795. case CIPSO_V4_TAG_RBITMAP:
  1796. ret_val = cipso_v4_parsetag_rbm(doi_def, &cipso[6], secattr);
  1797. break;
  1798. case CIPSO_V4_TAG_ENUM:
  1799. ret_val = cipso_v4_parsetag_enum(doi_def, &cipso[6], secattr);
  1800. break;
  1801. case CIPSO_V4_TAG_RANGE:
  1802. ret_val = cipso_v4_parsetag_rng(doi_def, &cipso[6], secattr);
  1803. break;
  1804. case CIPSO_V4_TAG_LOCAL:
  1805. ret_val = cipso_v4_parsetag_loc(doi_def, &cipso[6], secattr);
  1806. break;
  1807. }
  1808. if (ret_val == 0)
  1809. secattr->type = NETLBL_NLTYPE_CIPSOV4;
  1810. getattr_return:
  1811. rcu_read_unlock();
  1812. return ret_val;
  1813. }
  1814. /**
  1815. * cipso_v4_sock_getattr - Get the security attributes from a sock
  1816. * @sk: the sock
  1817. * @secattr: the security attributes
  1818. *
  1819. * Description:
  1820. * Query @sk to see if there is a CIPSO option attached to the sock and if
  1821. * there is return the CIPSO security attributes in @secattr. This function
  1822. * requires that @sk be locked, or privately held, but it does not do any
  1823. * locking itself. Returns zero on success and negative values on failure.
  1824. *
  1825. */
  1826. int cipso_v4_sock_getattr(struct sock *sk, struct netlbl_lsm_secattr *secattr)
  1827. {
  1828. struct ip_options *opt;
  1829. opt = inet_sk(sk)->opt;
  1830. if (opt == NULL || opt->cipso == 0)
  1831. return -ENOMSG;
  1832. return cipso_v4_getattr(opt->__data + opt->cipso - sizeof(struct iphdr),
  1833. secattr);
  1834. }
  1835. /**
  1836. * cipso_v4_skbuff_setattr - Set the CIPSO option on a packet
  1837. * @skb: the packet
  1838. * @secattr: the security attributes
  1839. *
  1840. * Description:
  1841. * Set the CIPSO option on the given packet based on the security attributes.
  1842. * Returns a pointer to the IP header on success and NULL on failure.
  1843. *
  1844. */
  1845. int cipso_v4_skbuff_setattr(struct sk_buff *skb,
  1846. const struct cipso_v4_doi *doi_def,
  1847. const struct netlbl_lsm_secattr *secattr)
  1848. {
  1849. int ret_val;
  1850. struct iphdr *iph;
  1851. struct ip_options *opt = &IPCB(skb)->opt;
  1852. unsigned char buf[CIPSO_V4_OPT_LEN_MAX];
  1853. u32 buf_len = CIPSO_V4_OPT_LEN_MAX;
  1854. u32 opt_len;
  1855. int len_delta;
  1856. ret_val = cipso_v4_genopt(buf, buf_len, doi_def, secattr);
  1857. if (ret_val < 0)
  1858. return ret_val;
  1859. buf_len = ret_val;
  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);