cipso_ipv4.c 62 KB

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