cipso_ipv4.c 63 KB

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