cipso_ipv4.c 63 KB

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