cipso_ipv4.c 52 KB

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