cipso_ipv4.c 63 KB

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