cipso_ipv4.c 52 KB

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