cipso_ipv4.c 61 KB

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