cipso_ipv4.c 60 KB

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