cipso_ipv4.c 63 KB

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