cipso_ipv4.c 40 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495
  1. /*
  2. * CIPSO - Commercial IP Security Option
  3. *
  4. * This is an implementation of the CIPSO 2.2 protocol as specified in
  5. * draft-ietf-cipso-ipsecurity-01.txt with additional tag types as found in
  6. * FIPS-188, copies of both documents can be found in the Documentation
  7. * directory. While CIPSO never became a full IETF RFC standard many vendors
  8. * have chosen to adopt the protocol and over the years it has become a
  9. * de-facto standard for labeled networking.
  10. *
  11. * Author: Paul Moore <paul.moore@hp.com>
  12. *
  13. */
  14. /*
  15. * (c) Copyright Hewlett-Packard Development Company, L.P., 2006
  16. *
  17. * This program is free software; you can redistribute it and/or modify
  18. * it under the terms of the GNU General Public License as published by
  19. * the Free Software Foundation; either version 2 of the License, or
  20. * (at your option) any later version.
  21. *
  22. * This program is distributed in the hope that it will be useful,
  23. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  24. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See
  25. * the GNU General Public License for more details.
  26. *
  27. * You should have received a copy of the GNU General Public License
  28. * along with this program; if not, write to the Free Software
  29. * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
  30. *
  31. */
  32. #include <linux/init.h>
  33. #include <linux/types.h>
  34. #include <linux/rcupdate.h>
  35. #include <linux/list.h>
  36. #include <linux/spinlock.h>
  37. #include <linux/string.h>
  38. #include <linux/jhash.h>
  39. #include <net/ip.h>
  40. #include <net/icmp.h>
  41. #include <net/tcp.h>
  42. #include <net/netlabel.h>
  43. #include <net/cipso_ipv4.h>
  44. #include <asm/atomic.h>
  45. #include <asm/bug.h>
  46. struct cipso_v4_domhsh_entry {
  47. char *domain;
  48. u32 valid;
  49. struct list_head list;
  50. struct rcu_head rcu;
  51. };
  52. /* List of available DOI definitions */
  53. /* XXX - Updates should be minimal so having a single lock for the
  54. * cipso_v4_doi_list and the cipso_v4_doi_list->dom_list should be
  55. * okay. */
  56. /* XXX - This currently assumes a minimal number of different DOIs in use,
  57. * if in practice there are a lot of different DOIs this list should
  58. * probably be turned into a hash table or something similar so we
  59. * can do quick lookups. */
  60. static DEFINE_SPINLOCK(cipso_v4_doi_list_lock);
  61. static struct list_head cipso_v4_doi_list = LIST_HEAD_INIT(cipso_v4_doi_list);
  62. /* Label mapping cache */
  63. int cipso_v4_cache_enabled = 1;
  64. int cipso_v4_cache_bucketsize = 10;
  65. #define CIPSO_V4_CACHE_BUCKETBITS 7
  66. #define CIPSO_V4_CACHE_BUCKETS (1 << CIPSO_V4_CACHE_BUCKETBITS)
  67. #define CIPSO_V4_CACHE_REORDERLIMIT 10
  68. struct cipso_v4_map_cache_bkt {
  69. spinlock_t lock;
  70. u32 size;
  71. struct list_head list;
  72. };
  73. struct cipso_v4_map_cache_entry {
  74. u32 hash;
  75. unsigned char *key;
  76. size_t key_len;
  77. struct netlbl_lsm_cache *lsm_data;
  78. u32 activity;
  79. struct list_head list;
  80. };
  81. static struct cipso_v4_map_cache_bkt *cipso_v4_cache = NULL;
  82. /* Restricted bitmap (tag #1) flags */
  83. int cipso_v4_rbm_optfmt = 0;
  84. int cipso_v4_rbm_strictvalid = 1;
  85. /*
  86. * Helper Functions
  87. */
  88. /**
  89. * cipso_v4_bitmap_walk - Walk a bitmap looking for a bit
  90. * @bitmap: the bitmap
  91. * @bitmap_len: length in bits
  92. * @offset: starting offset
  93. * @state: if non-zero, look for a set (1) bit else look for a cleared (0) bit
  94. *
  95. * Description:
  96. * Starting at @offset, walk the bitmap from left to right until either the
  97. * desired bit is found or we reach the end. Return the bit offset, -1 if
  98. * not found, or -2 if error.
  99. */
  100. static int cipso_v4_bitmap_walk(const unsigned char *bitmap,
  101. u32 bitmap_len,
  102. u32 offset,
  103. u8 state)
  104. {
  105. u32 bit_spot;
  106. u32 byte_offset;
  107. unsigned char bitmask;
  108. unsigned char byte;
  109. /* gcc always rounds to zero when doing integer division */
  110. byte_offset = offset / 8;
  111. byte = bitmap[byte_offset];
  112. bit_spot = offset;
  113. bitmask = 0x80 >> (offset % 8);
  114. while (bit_spot < bitmap_len) {
  115. if ((state && (byte & bitmask) == bitmask) ||
  116. (state == 0 && (byte & bitmask) == 0))
  117. return bit_spot;
  118. bit_spot++;
  119. bitmask >>= 1;
  120. if (bitmask == 0) {
  121. byte = bitmap[++byte_offset];
  122. bitmask = 0x80;
  123. }
  124. }
  125. return -1;
  126. }
  127. /**
  128. * cipso_v4_bitmap_setbit - Sets a single bit in a bitmap
  129. * @bitmap: the bitmap
  130. * @bit: the bit
  131. * @state: if non-zero, set the bit (1) else clear the bit (0)
  132. *
  133. * Description:
  134. * Set a single bit in the bitmask. Returns zero on success, negative values
  135. * on error.
  136. */
  137. static void cipso_v4_bitmap_setbit(unsigned char *bitmap,
  138. u32 bit,
  139. u8 state)
  140. {
  141. u32 byte_spot;
  142. u8 bitmask;
  143. /* gcc always rounds to zero when doing integer division */
  144. byte_spot = bit / 8;
  145. bitmask = 0x80 >> (bit % 8);
  146. if (state)
  147. bitmap[byte_spot] |= bitmask;
  148. else
  149. bitmap[byte_spot] &= ~bitmask;
  150. }
  151. /**
  152. * cipso_v4_doi_domhsh_free - Frees a domain list entry
  153. * @entry: the entry's RCU field
  154. *
  155. * Description:
  156. * This function is designed to be used as a callback to the call_rcu()
  157. * function so that the memory allocated to a domain list entry can be released
  158. * safely.
  159. *
  160. */
  161. static void cipso_v4_doi_domhsh_free(struct rcu_head *entry)
  162. {
  163. struct cipso_v4_domhsh_entry *ptr;
  164. ptr = container_of(entry, struct cipso_v4_domhsh_entry, rcu);
  165. kfree(ptr->domain);
  166. kfree(ptr);
  167. }
  168. /**
  169. * cipso_v4_cache_entry_free - Frees a cache entry
  170. * @entry: the entry to free
  171. *
  172. * Description:
  173. * This function frees the memory associated with a cache entry including the
  174. * LSM cache data if there are no longer any users, i.e. reference count == 0.
  175. *
  176. */
  177. static void cipso_v4_cache_entry_free(struct cipso_v4_map_cache_entry *entry)
  178. {
  179. if (entry->lsm_data)
  180. netlbl_secattr_cache_free(entry->lsm_data);
  181. kfree(entry->key);
  182. kfree(entry);
  183. }
  184. /**
  185. * cipso_v4_map_cache_hash - Hashing function for the CIPSO cache
  186. * @key: the hash key
  187. * @key_len: the length of the key in bytes
  188. *
  189. * Description:
  190. * The CIPSO tag hashing function. Returns a 32-bit hash value.
  191. *
  192. */
  193. static u32 cipso_v4_map_cache_hash(const unsigned char *key, u32 key_len)
  194. {
  195. return jhash(key, key_len, 0);
  196. }
  197. /*
  198. * Label Mapping Cache Functions
  199. */
  200. /**
  201. * cipso_v4_cache_init - Initialize the CIPSO cache
  202. *
  203. * Description:
  204. * Initializes the CIPSO label mapping cache, this function should be called
  205. * before any of the other functions defined in this file. Returns zero on
  206. * success, negative values on error.
  207. *
  208. */
  209. static int cipso_v4_cache_init(void)
  210. {
  211. u32 iter;
  212. cipso_v4_cache = kcalloc(CIPSO_V4_CACHE_BUCKETS,
  213. sizeof(struct cipso_v4_map_cache_bkt),
  214. GFP_KERNEL);
  215. if (cipso_v4_cache == NULL)
  216. return -ENOMEM;
  217. for (iter = 0; iter < CIPSO_V4_CACHE_BUCKETS; iter++) {
  218. spin_lock_init(&cipso_v4_cache[iter].lock);
  219. cipso_v4_cache[iter].size = 0;
  220. INIT_LIST_HEAD(&cipso_v4_cache[iter].list);
  221. }
  222. return 0;
  223. }
  224. /**
  225. * cipso_v4_cache_invalidate - Invalidates the current CIPSO cache
  226. *
  227. * Description:
  228. * Invalidates and frees any entries in the CIPSO cache. Returns zero on
  229. * success and negative values on failure.
  230. *
  231. */
  232. void cipso_v4_cache_invalidate(void)
  233. {
  234. struct cipso_v4_map_cache_entry *entry, *tmp_entry;
  235. u32 iter;
  236. for (iter = 0; iter < CIPSO_V4_CACHE_BUCKETS; iter++) {
  237. spin_lock_bh(&cipso_v4_cache[iter].lock);
  238. list_for_each_entry_safe(entry,
  239. tmp_entry,
  240. &cipso_v4_cache[iter].list, list) {
  241. list_del(&entry->list);
  242. cipso_v4_cache_entry_free(entry);
  243. }
  244. cipso_v4_cache[iter].size = 0;
  245. spin_unlock_bh(&cipso_v4_cache[iter].lock);
  246. }
  247. return;
  248. }
  249. /**
  250. * cipso_v4_cache_check - Check the CIPSO cache for a label mapping
  251. * @key: the buffer to check
  252. * @key_len: buffer length in bytes
  253. * @secattr: the security attribute struct to use
  254. *
  255. * Description:
  256. * This function checks the cache to see if a label mapping already exists for
  257. * the given key. If there is a match then the cache is adjusted and the
  258. * @secattr struct is populated with the correct LSM security attributes. The
  259. * cache is adjusted in the following manner if the entry is not already the
  260. * first in the cache bucket:
  261. *
  262. * 1. The cache entry's activity counter is incremented
  263. * 2. The previous (higher ranking) entry's activity counter is decremented
  264. * 3. If the difference between the two activity counters is geater than
  265. * CIPSO_V4_CACHE_REORDERLIMIT the two entries are swapped
  266. *
  267. * Returns zero on success, -ENOENT for a cache miss, and other negative values
  268. * on error.
  269. *
  270. */
  271. static int cipso_v4_cache_check(const unsigned char *key,
  272. u32 key_len,
  273. struct netlbl_lsm_secattr *secattr)
  274. {
  275. u32 bkt;
  276. struct cipso_v4_map_cache_entry *entry;
  277. struct cipso_v4_map_cache_entry *prev_entry = NULL;
  278. u32 hash;
  279. if (!cipso_v4_cache_enabled)
  280. return -ENOENT;
  281. hash = cipso_v4_map_cache_hash(key, key_len);
  282. bkt = hash & (CIPSO_V4_CACHE_BUCKETBITS - 1);
  283. spin_lock_bh(&cipso_v4_cache[bkt].lock);
  284. list_for_each_entry(entry, &cipso_v4_cache[bkt].list, list) {
  285. if (entry->hash == hash &&
  286. entry->key_len == key_len &&
  287. memcmp(entry->key, key, key_len) == 0) {
  288. entry->activity += 1;
  289. atomic_inc(&entry->lsm_data->refcount);
  290. secattr->cache = entry->lsm_data;
  291. secattr->flags |= NETLBL_SECATTR_CACHE;
  292. if (prev_entry == NULL) {
  293. spin_unlock_bh(&cipso_v4_cache[bkt].lock);
  294. return 0;
  295. }
  296. if (prev_entry->activity > 0)
  297. prev_entry->activity -= 1;
  298. if (entry->activity > prev_entry->activity &&
  299. entry->activity - prev_entry->activity >
  300. CIPSO_V4_CACHE_REORDERLIMIT) {
  301. __list_del(entry->list.prev, entry->list.next);
  302. __list_add(&entry->list,
  303. prev_entry->list.prev,
  304. &prev_entry->list);
  305. }
  306. spin_unlock_bh(&cipso_v4_cache[bkt].lock);
  307. return 0;
  308. }
  309. prev_entry = entry;
  310. }
  311. spin_unlock_bh(&cipso_v4_cache[bkt].lock);
  312. return -ENOENT;
  313. }
  314. /**
  315. * cipso_v4_cache_add - Add an entry to the CIPSO cache
  316. * @skb: the packet
  317. * @secattr: the packet's security attributes
  318. *
  319. * Description:
  320. * Add a new entry into the CIPSO label mapping cache. Add the new entry to
  321. * head of the cache bucket's list, if the cache bucket is out of room remove
  322. * the last entry in the list first. It is important to note that there is
  323. * currently no checking for duplicate keys. Returns zero on success,
  324. * negative values on failure.
  325. *
  326. */
  327. int cipso_v4_cache_add(const struct sk_buff *skb,
  328. const struct netlbl_lsm_secattr *secattr)
  329. {
  330. int ret_val = -EPERM;
  331. u32 bkt;
  332. struct cipso_v4_map_cache_entry *entry = NULL;
  333. struct cipso_v4_map_cache_entry *old_entry = NULL;
  334. unsigned char *cipso_ptr;
  335. u32 cipso_ptr_len;
  336. if (!cipso_v4_cache_enabled || cipso_v4_cache_bucketsize <= 0)
  337. return 0;
  338. cipso_ptr = CIPSO_V4_OPTPTR(skb);
  339. cipso_ptr_len = cipso_ptr[1];
  340. entry = kzalloc(sizeof(*entry), GFP_ATOMIC);
  341. if (entry == NULL)
  342. return -ENOMEM;
  343. entry->key = kmemdup(cipso_ptr, cipso_ptr_len, GFP_ATOMIC);
  344. if (entry->key == NULL) {
  345. ret_val = -ENOMEM;
  346. goto cache_add_failure;
  347. }
  348. entry->key_len = cipso_ptr_len;
  349. entry->hash = cipso_v4_map_cache_hash(cipso_ptr, cipso_ptr_len);
  350. atomic_inc(&secattr->cache->refcount);
  351. entry->lsm_data = secattr->cache;
  352. bkt = entry->hash & (CIPSO_V4_CACHE_BUCKETBITS - 1);
  353. spin_lock_bh(&cipso_v4_cache[bkt].lock);
  354. if (cipso_v4_cache[bkt].size < cipso_v4_cache_bucketsize) {
  355. list_add(&entry->list, &cipso_v4_cache[bkt].list);
  356. cipso_v4_cache[bkt].size += 1;
  357. } else {
  358. old_entry = list_entry(cipso_v4_cache[bkt].list.prev,
  359. struct cipso_v4_map_cache_entry, list);
  360. list_del(&old_entry->list);
  361. list_add(&entry->list, &cipso_v4_cache[bkt].list);
  362. cipso_v4_cache_entry_free(old_entry);
  363. }
  364. spin_unlock_bh(&cipso_v4_cache[bkt].lock);
  365. return 0;
  366. cache_add_failure:
  367. if (entry)
  368. cipso_v4_cache_entry_free(entry);
  369. return ret_val;
  370. }
  371. /*
  372. * DOI List Functions
  373. */
  374. /**
  375. * cipso_v4_doi_search - Searches for a DOI definition
  376. * @doi: the DOI to search for
  377. *
  378. * Description:
  379. * Search the DOI definition list for a DOI definition with a DOI value that
  380. * matches @doi. The caller is responsibile for calling rcu_read_[un]lock().
  381. * Returns a pointer to the DOI definition on success and NULL on failure.
  382. */
  383. static struct cipso_v4_doi *cipso_v4_doi_search(u32 doi)
  384. {
  385. struct cipso_v4_doi *iter;
  386. list_for_each_entry_rcu(iter, &cipso_v4_doi_list, list)
  387. if (iter->doi == doi && iter->valid)
  388. return iter;
  389. return NULL;
  390. }
  391. /**
  392. * cipso_v4_doi_add - Add a new DOI to the CIPSO protocol engine
  393. * @doi_def: the DOI structure
  394. *
  395. * Description:
  396. * The caller defines a new DOI for use by the CIPSO engine and calls this
  397. * function to add it to the list of acceptable domains. The caller must
  398. * ensure that the mapping table specified in @doi_def->map meets all of the
  399. * requirements of the mapping type (see cipso_ipv4.h for details). Returns
  400. * zero on success and non-zero on failure.
  401. *
  402. */
  403. int cipso_v4_doi_add(struct cipso_v4_doi *doi_def)
  404. {
  405. u32 iter;
  406. if (doi_def == NULL || doi_def->doi == CIPSO_V4_DOI_UNKNOWN)
  407. return -EINVAL;
  408. for (iter = 0; iter < CIPSO_V4_TAG_MAXCNT; iter++) {
  409. switch (doi_def->tags[iter]) {
  410. case CIPSO_V4_TAG_RBITMAP:
  411. break;
  412. case CIPSO_V4_TAG_INVALID:
  413. if (iter == 0)
  414. return -EINVAL;
  415. break;
  416. default:
  417. return -EINVAL;
  418. }
  419. }
  420. doi_def->valid = 1;
  421. INIT_RCU_HEAD(&doi_def->rcu);
  422. INIT_LIST_HEAD(&doi_def->dom_list);
  423. rcu_read_lock();
  424. if (cipso_v4_doi_search(doi_def->doi) != NULL)
  425. goto doi_add_failure_rlock;
  426. spin_lock(&cipso_v4_doi_list_lock);
  427. if (cipso_v4_doi_search(doi_def->doi) != NULL)
  428. goto doi_add_failure_slock;
  429. list_add_tail_rcu(&doi_def->list, &cipso_v4_doi_list);
  430. spin_unlock(&cipso_v4_doi_list_lock);
  431. rcu_read_unlock();
  432. return 0;
  433. doi_add_failure_slock:
  434. spin_unlock(&cipso_v4_doi_list_lock);
  435. doi_add_failure_rlock:
  436. rcu_read_unlock();
  437. return -EEXIST;
  438. }
  439. /**
  440. * cipso_v4_doi_remove - Remove an existing DOI from the CIPSO protocol engine
  441. * @doi: the DOI value
  442. * @audit_secid: the LSM secid to use in the audit message
  443. * @callback: the DOI cleanup/free callback
  444. *
  445. * Description:
  446. * Removes a DOI definition from the CIPSO engine, @callback is called to
  447. * free any memory. The NetLabel routines will be called to release their own
  448. * LSM domain mappings as well as our own domain list. Returns zero on
  449. * success and negative values on failure.
  450. *
  451. */
  452. int cipso_v4_doi_remove(u32 doi,
  453. struct netlbl_audit *audit_info,
  454. void (*callback) (struct rcu_head * head))
  455. {
  456. struct cipso_v4_doi *doi_def;
  457. struct cipso_v4_domhsh_entry *dom_iter;
  458. rcu_read_lock();
  459. if (cipso_v4_doi_search(doi) != NULL) {
  460. spin_lock(&cipso_v4_doi_list_lock);
  461. doi_def = cipso_v4_doi_search(doi);
  462. if (doi_def == NULL) {
  463. spin_unlock(&cipso_v4_doi_list_lock);
  464. rcu_read_unlock();
  465. return -ENOENT;
  466. }
  467. doi_def->valid = 0;
  468. list_del_rcu(&doi_def->list);
  469. spin_unlock(&cipso_v4_doi_list_lock);
  470. list_for_each_entry_rcu(dom_iter, &doi_def->dom_list, list)
  471. if (dom_iter->valid)
  472. netlbl_domhsh_remove(dom_iter->domain,
  473. audit_info);
  474. cipso_v4_cache_invalidate();
  475. rcu_read_unlock();
  476. call_rcu(&doi_def->rcu, callback);
  477. return 0;
  478. }
  479. rcu_read_unlock();
  480. return -ENOENT;
  481. }
  482. /**
  483. * cipso_v4_doi_getdef - Returns a pointer to a valid DOI definition
  484. * @doi: the DOI value
  485. *
  486. * Description:
  487. * Searches for a valid DOI definition and if one is found it is returned to
  488. * the caller. Otherwise NULL is returned. The caller must ensure that
  489. * rcu_read_lock() is held while accessing the returned definition.
  490. *
  491. */
  492. struct cipso_v4_doi *cipso_v4_doi_getdef(u32 doi)
  493. {
  494. return cipso_v4_doi_search(doi);
  495. }
  496. /**
  497. * cipso_v4_doi_walk - Iterate through the DOI definitions
  498. * @skip_cnt: skip past this number of DOI definitions, updated
  499. * @callback: callback for each DOI definition
  500. * @cb_arg: argument for the callback function
  501. *
  502. * Description:
  503. * Iterate over the DOI definition list, skipping the first @skip_cnt entries.
  504. * For each entry call @callback, if @callback returns a negative value stop
  505. * 'walking' through the list and return. Updates the value in @skip_cnt upon
  506. * return. Returns zero on success, negative values on failure.
  507. *
  508. */
  509. int cipso_v4_doi_walk(u32 *skip_cnt,
  510. int (*callback) (struct cipso_v4_doi *doi_def, void *arg),
  511. void *cb_arg)
  512. {
  513. int ret_val = -ENOENT;
  514. u32 doi_cnt = 0;
  515. struct cipso_v4_doi *iter_doi;
  516. rcu_read_lock();
  517. list_for_each_entry_rcu(iter_doi, &cipso_v4_doi_list, list)
  518. if (iter_doi->valid) {
  519. if (doi_cnt++ < *skip_cnt)
  520. continue;
  521. ret_val = callback(iter_doi, cb_arg);
  522. if (ret_val < 0) {
  523. doi_cnt--;
  524. goto doi_walk_return;
  525. }
  526. }
  527. doi_walk_return:
  528. rcu_read_unlock();
  529. *skip_cnt = doi_cnt;
  530. return ret_val;
  531. }
  532. /**
  533. * cipso_v4_doi_domhsh_add - Adds a domain entry to a DOI definition
  534. * @doi_def: the DOI definition
  535. * @domain: the domain to add
  536. *
  537. * Description:
  538. * Adds the @domain to the the DOI specified by @doi_def, this function
  539. * should only be called by external functions (i.e. NetLabel). This function
  540. * does allocate memory. Returns zero on success, negative values on failure.
  541. *
  542. */
  543. int cipso_v4_doi_domhsh_add(struct cipso_v4_doi *doi_def, const char *domain)
  544. {
  545. struct cipso_v4_domhsh_entry *iter;
  546. struct cipso_v4_domhsh_entry *new_dom;
  547. new_dom = kzalloc(sizeof(*new_dom), GFP_KERNEL);
  548. if (new_dom == NULL)
  549. return -ENOMEM;
  550. if (domain) {
  551. new_dom->domain = kstrdup(domain, GFP_KERNEL);
  552. if (new_dom->domain == NULL) {
  553. kfree(new_dom);
  554. return -ENOMEM;
  555. }
  556. }
  557. new_dom->valid = 1;
  558. INIT_RCU_HEAD(&new_dom->rcu);
  559. rcu_read_lock();
  560. spin_lock(&cipso_v4_doi_list_lock);
  561. list_for_each_entry_rcu(iter, &doi_def->dom_list, list)
  562. if (iter->valid &&
  563. ((domain != NULL && iter->domain != NULL &&
  564. strcmp(iter->domain, domain) == 0) ||
  565. (domain == NULL && iter->domain == NULL))) {
  566. spin_unlock(&cipso_v4_doi_list_lock);
  567. rcu_read_unlock();
  568. kfree(new_dom->domain);
  569. kfree(new_dom);
  570. return -EEXIST;
  571. }
  572. list_add_tail_rcu(&new_dom->list, &doi_def->dom_list);
  573. spin_unlock(&cipso_v4_doi_list_lock);
  574. rcu_read_unlock();
  575. return 0;
  576. }
  577. /**
  578. * cipso_v4_doi_domhsh_remove - Removes a domain entry from a DOI definition
  579. * @doi_def: the DOI definition
  580. * @domain: the domain to remove
  581. *
  582. * Description:
  583. * Removes the @domain from the DOI specified by @doi_def, this function
  584. * should only be called by external functions (i.e. NetLabel). Returns zero
  585. * on success and negative values on error.
  586. *
  587. */
  588. int cipso_v4_doi_domhsh_remove(struct cipso_v4_doi *doi_def,
  589. const char *domain)
  590. {
  591. struct cipso_v4_domhsh_entry *iter;
  592. rcu_read_lock();
  593. spin_lock(&cipso_v4_doi_list_lock);
  594. list_for_each_entry_rcu(iter, &doi_def->dom_list, list)
  595. if (iter->valid &&
  596. ((domain != NULL && iter->domain != NULL &&
  597. strcmp(iter->domain, domain) == 0) ||
  598. (domain == NULL && iter->domain == NULL))) {
  599. iter->valid = 0;
  600. list_del_rcu(&iter->list);
  601. spin_unlock(&cipso_v4_doi_list_lock);
  602. rcu_read_unlock();
  603. call_rcu(&iter->rcu, cipso_v4_doi_domhsh_free);
  604. return 0;
  605. }
  606. spin_unlock(&cipso_v4_doi_list_lock);
  607. rcu_read_unlock();
  608. return -ENOENT;
  609. }
  610. /*
  611. * Label Mapping Functions
  612. */
  613. /**
  614. * cipso_v4_map_lvl_valid - Checks to see if the given level is understood
  615. * @doi_def: the DOI definition
  616. * @level: the level to check
  617. *
  618. * Description:
  619. * Checks the given level against the given DOI definition and returns a
  620. * negative value if the level does not have a valid mapping and a zero value
  621. * if the level is defined by the DOI.
  622. *
  623. */
  624. static int cipso_v4_map_lvl_valid(const struct cipso_v4_doi *doi_def, u8 level)
  625. {
  626. switch (doi_def->type) {
  627. case CIPSO_V4_MAP_PASS:
  628. return 0;
  629. case CIPSO_V4_MAP_STD:
  630. if (doi_def->map.std->lvl.cipso[level] < CIPSO_V4_INV_LVL)
  631. return 0;
  632. break;
  633. }
  634. return -EFAULT;
  635. }
  636. /**
  637. * cipso_v4_map_lvl_hton - Perform a level mapping from the host to the network
  638. * @doi_def: the DOI definition
  639. * @host_lvl: the host MLS level
  640. * @net_lvl: the network/CIPSO MLS level
  641. *
  642. * Description:
  643. * Perform a label mapping to translate a local MLS level to the correct
  644. * CIPSO level using the given DOI definition. Returns zero on success,
  645. * negative values otherwise.
  646. *
  647. */
  648. static int cipso_v4_map_lvl_hton(const struct cipso_v4_doi *doi_def,
  649. u32 host_lvl,
  650. u32 *net_lvl)
  651. {
  652. switch (doi_def->type) {
  653. case CIPSO_V4_MAP_PASS:
  654. *net_lvl = host_lvl;
  655. return 0;
  656. case CIPSO_V4_MAP_STD:
  657. if (host_lvl < doi_def->map.std->lvl.local_size) {
  658. *net_lvl = doi_def->map.std->lvl.local[host_lvl];
  659. return 0;
  660. }
  661. break;
  662. }
  663. return -EINVAL;
  664. }
  665. /**
  666. * cipso_v4_map_lvl_ntoh - Perform a level mapping from the network to the host
  667. * @doi_def: the DOI definition
  668. * @net_lvl: the network/CIPSO MLS level
  669. * @host_lvl: the host MLS level
  670. *
  671. * Description:
  672. * Perform a label mapping to translate a CIPSO level to the correct local MLS
  673. * level using the given DOI definition. Returns zero on success, negative
  674. * values otherwise.
  675. *
  676. */
  677. static int cipso_v4_map_lvl_ntoh(const struct cipso_v4_doi *doi_def,
  678. u32 net_lvl,
  679. u32 *host_lvl)
  680. {
  681. struct cipso_v4_std_map_tbl *map_tbl;
  682. switch (doi_def->type) {
  683. case CIPSO_V4_MAP_PASS:
  684. *host_lvl = net_lvl;
  685. return 0;
  686. case CIPSO_V4_MAP_STD:
  687. map_tbl = doi_def->map.std;
  688. if (net_lvl < map_tbl->lvl.cipso_size &&
  689. map_tbl->lvl.cipso[net_lvl] < CIPSO_V4_INV_LVL) {
  690. *host_lvl = doi_def->map.std->lvl.cipso[net_lvl];
  691. return 0;
  692. }
  693. break;
  694. }
  695. return -EINVAL;
  696. }
  697. /**
  698. * cipso_v4_map_cat_rbm_valid - Checks to see if the category bitmap is valid
  699. * @doi_def: the DOI definition
  700. * @bitmap: category bitmap
  701. * @bitmap_len: bitmap length in bytes
  702. *
  703. * Description:
  704. * Checks the given category bitmap against the given DOI definition and
  705. * returns a negative value if any of the categories in the bitmap do not have
  706. * a valid mapping and a zero value if all of the categories are valid.
  707. *
  708. */
  709. static int cipso_v4_map_cat_rbm_valid(const struct cipso_v4_doi *doi_def,
  710. const unsigned char *bitmap,
  711. u32 bitmap_len)
  712. {
  713. int cat = -1;
  714. u32 bitmap_len_bits = bitmap_len * 8;
  715. u32 cipso_cat_size;
  716. u32 *cipso_array;
  717. switch (doi_def->type) {
  718. case CIPSO_V4_MAP_PASS:
  719. return 0;
  720. case CIPSO_V4_MAP_STD:
  721. cipso_cat_size = doi_def->map.std->cat.cipso_size;
  722. cipso_array = doi_def->map.std->cat.cipso;
  723. for (;;) {
  724. cat = cipso_v4_bitmap_walk(bitmap,
  725. bitmap_len_bits,
  726. cat + 1,
  727. 1);
  728. if (cat < 0)
  729. break;
  730. if (cat >= cipso_cat_size ||
  731. cipso_array[cat] >= CIPSO_V4_INV_CAT)
  732. return -EFAULT;
  733. }
  734. if (cat == -1)
  735. return 0;
  736. break;
  737. }
  738. return -EFAULT;
  739. }
  740. /**
  741. * cipso_v4_map_cat_rbm_hton - Perform a category mapping from host to network
  742. * @doi_def: the DOI definition
  743. * @host_cat: the category bitmap in host format
  744. * @host_cat_len: the length of the host's category bitmap in bytes
  745. * @net_cat: the zero'd out category bitmap in network/CIPSO format
  746. * @net_cat_len: the length of the CIPSO bitmap in bytes
  747. *
  748. * Description:
  749. * Perform a label mapping to translate a local MLS category bitmap to the
  750. * correct CIPSO bitmap using the given DOI definition. Returns the minimum
  751. * size in bytes of the network bitmap on success, negative values otherwise.
  752. *
  753. */
  754. static int cipso_v4_map_cat_rbm_hton(const struct cipso_v4_doi *doi_def,
  755. const unsigned char *host_cat,
  756. u32 host_cat_len,
  757. unsigned char *net_cat,
  758. u32 net_cat_len)
  759. {
  760. int host_spot = -1;
  761. u32 net_spot;
  762. u32 net_spot_max = 0;
  763. u32 host_clen_bits = host_cat_len * 8;
  764. u32 net_clen_bits = net_cat_len * 8;
  765. u32 host_cat_size;
  766. u32 *host_cat_array;
  767. switch (doi_def->type) {
  768. case CIPSO_V4_MAP_PASS:
  769. net_spot_max = host_cat_len;
  770. while (net_spot_max > 0 && host_cat[net_spot_max - 1] == 0)
  771. net_spot_max--;
  772. if (net_spot_max > net_cat_len)
  773. return -EINVAL;
  774. memcpy(net_cat, host_cat, net_spot_max);
  775. return net_spot_max;
  776. case CIPSO_V4_MAP_STD:
  777. host_cat_size = doi_def->map.std->cat.local_size;
  778. host_cat_array = doi_def->map.std->cat.local;
  779. for (;;) {
  780. host_spot = cipso_v4_bitmap_walk(host_cat,
  781. host_clen_bits,
  782. host_spot + 1,
  783. 1);
  784. if (host_spot < 0)
  785. break;
  786. if (host_spot >= host_cat_size)
  787. return -EPERM;
  788. net_spot = host_cat_array[host_spot];
  789. if (net_spot >= CIPSO_V4_INV_CAT)
  790. return -EPERM;
  791. if (net_spot >= net_clen_bits)
  792. return -ENOSPC;
  793. cipso_v4_bitmap_setbit(net_cat, net_spot, 1);
  794. if (net_spot > net_spot_max)
  795. net_spot_max = net_spot;
  796. }
  797. if (host_spot == -2)
  798. return -EFAULT;
  799. if (++net_spot_max % 8)
  800. return net_spot_max / 8 + 1;
  801. return net_spot_max / 8;
  802. }
  803. return -EINVAL;
  804. }
  805. /**
  806. * cipso_v4_map_cat_rbm_ntoh - Perform a category mapping from network to host
  807. * @doi_def: the DOI definition
  808. * @net_cat: the category bitmap in network/CIPSO format
  809. * @net_cat_len: the length of the CIPSO bitmap in bytes
  810. * @host_cat: the zero'd out category bitmap in host format
  811. * @host_cat_len: the length of the host's category bitmap in bytes
  812. *
  813. * Description:
  814. * Perform a label mapping to translate a CIPSO bitmap to the correct local
  815. * MLS category bitmap using the given DOI definition. Returns the minimum
  816. * size in bytes of the host bitmap on success, negative values otherwise.
  817. *
  818. */
  819. static int cipso_v4_map_cat_rbm_ntoh(const struct cipso_v4_doi *doi_def,
  820. const unsigned char *net_cat,
  821. u32 net_cat_len,
  822. unsigned char *host_cat,
  823. u32 host_cat_len)
  824. {
  825. u32 host_spot;
  826. u32 host_spot_max = 0;
  827. int net_spot = -1;
  828. u32 net_clen_bits = net_cat_len * 8;
  829. u32 host_clen_bits = host_cat_len * 8;
  830. u32 net_cat_size;
  831. u32 *net_cat_array;
  832. switch (doi_def->type) {
  833. case CIPSO_V4_MAP_PASS:
  834. if (net_cat_len > host_cat_len)
  835. return -EINVAL;
  836. memcpy(host_cat, net_cat, net_cat_len);
  837. return net_cat_len;
  838. case CIPSO_V4_MAP_STD:
  839. net_cat_size = doi_def->map.std->cat.cipso_size;
  840. net_cat_array = doi_def->map.std->cat.cipso;
  841. for (;;) {
  842. net_spot = cipso_v4_bitmap_walk(net_cat,
  843. net_clen_bits,
  844. net_spot + 1,
  845. 1);
  846. if (net_spot < 0)
  847. break;
  848. if (net_spot >= net_cat_size ||
  849. net_cat_array[net_spot] >= CIPSO_V4_INV_CAT)
  850. return -EPERM;
  851. host_spot = net_cat_array[net_spot];
  852. if (host_spot >= CIPSO_V4_INV_CAT)
  853. return -EPERM;
  854. if (host_spot >= host_clen_bits)
  855. return -ENOSPC;
  856. cipso_v4_bitmap_setbit(host_cat, host_spot, 1);
  857. if (host_spot > host_spot_max)
  858. host_spot_max = host_spot;
  859. }
  860. if (net_spot == -2)
  861. return -EFAULT;
  862. if (++host_spot_max % 8)
  863. return host_spot_max / 8 + 1;
  864. return host_spot_max / 8;
  865. }
  866. return -EINVAL;
  867. }
  868. /*
  869. * Protocol Handling Functions
  870. */
  871. #define CIPSO_V4_OPT_LEN_MAX 40
  872. #define CIPSO_V4_HDR_LEN 6
  873. /**
  874. * cipso_v4_gentag_hdr - Generate a CIPSO option header
  875. * @doi_def: the DOI definition
  876. * @len: the total tag length in bytes, not including this header
  877. * @buf: the CIPSO option buffer
  878. *
  879. * Description:
  880. * Write a CIPSO header into the beginning of @buffer.
  881. *
  882. */
  883. static void cipso_v4_gentag_hdr(const struct cipso_v4_doi *doi_def,
  884. unsigned char *buf,
  885. u32 len)
  886. {
  887. buf[0] = IPOPT_CIPSO;
  888. buf[1] = CIPSO_V4_HDR_LEN + len;
  889. *(__be32 *)&buf[2] = htonl(doi_def->doi);
  890. }
  891. /**
  892. * cipso_v4_gentag_rbm - Generate a CIPSO restricted bitmap tag (type #1)
  893. * @doi_def: the DOI definition
  894. * @secattr: the security attributes
  895. * @buffer: the option buffer
  896. * @buffer_len: length of buffer in bytes
  897. *
  898. * Description:
  899. * Generate a CIPSO option using the restricted bitmap tag, tag type #1. The
  900. * actual buffer length may be larger than the indicated size due to
  901. * translation between host and network category bitmaps. Returns the size of
  902. * the tag on success, negative values on failure.
  903. *
  904. */
  905. static int cipso_v4_gentag_rbm(const struct cipso_v4_doi *doi_def,
  906. const struct netlbl_lsm_secattr *secattr,
  907. unsigned char *buffer,
  908. u32 buffer_len)
  909. {
  910. int ret_val;
  911. u32 tag_len;
  912. u32 level;
  913. if ((secattr->flags & NETLBL_SECATTR_MLS_LVL) == 0)
  914. return -EPERM;
  915. ret_val = cipso_v4_map_lvl_hton(doi_def, secattr->mls_lvl, &level);
  916. if (ret_val != 0)
  917. return ret_val;
  918. if (secattr->flags & NETLBL_SECATTR_MLS_CAT) {
  919. ret_val = cipso_v4_map_cat_rbm_hton(doi_def,
  920. secattr->mls_cat,
  921. secattr->mls_cat_len,
  922. &buffer[4],
  923. buffer_len - 4);
  924. if (ret_val < 0)
  925. return ret_val;
  926. /* This will send packets using the "optimized" format when
  927. * possibile as specified in section 3.4.2.6 of the
  928. * CIPSO draft. */
  929. if (cipso_v4_rbm_optfmt && ret_val > 0 && ret_val <= 10)
  930. tag_len = 14;
  931. else
  932. tag_len = 4 + ret_val;
  933. } else
  934. tag_len = 4;
  935. buffer[0] = 0x01;
  936. buffer[1] = tag_len;
  937. buffer[3] = level;
  938. return tag_len;
  939. }
  940. /**
  941. * cipso_v4_parsetag_rbm - Parse a CIPSO restricted bitmap tag
  942. * @doi_def: the DOI definition
  943. * @tag: the CIPSO tag
  944. * @secattr: the security attributes
  945. *
  946. * Description:
  947. * Parse a CIPSO restricted bitmap tag (tag type #1) and return the security
  948. * attributes in @secattr. Return zero on success, negatives values on
  949. * failure.
  950. *
  951. */
  952. static int cipso_v4_parsetag_rbm(const struct cipso_v4_doi *doi_def,
  953. const unsigned char *tag,
  954. struct netlbl_lsm_secattr *secattr)
  955. {
  956. int ret_val;
  957. u8 tag_len = tag[1];
  958. u32 level;
  959. ret_val = cipso_v4_map_lvl_ntoh(doi_def, tag[3], &level);
  960. if (ret_val != 0)
  961. return ret_val;
  962. secattr->mls_lvl = level;
  963. secattr->flags |= NETLBL_SECATTR_MLS_LVL;
  964. if (tag_len > 4) {
  965. switch (doi_def->type) {
  966. case CIPSO_V4_MAP_PASS:
  967. secattr->mls_cat_len = tag_len - 4;
  968. break;
  969. case CIPSO_V4_MAP_STD:
  970. secattr->mls_cat_len =
  971. doi_def->map.std->cat.local_size;
  972. break;
  973. }
  974. secattr->mls_cat = kzalloc(secattr->mls_cat_len, GFP_ATOMIC);
  975. if (secattr->mls_cat == NULL)
  976. return -ENOMEM;
  977. ret_val = cipso_v4_map_cat_rbm_ntoh(doi_def,
  978. &tag[4],
  979. tag_len - 4,
  980. secattr->mls_cat,
  981. secattr->mls_cat_len);
  982. if (ret_val < 0) {
  983. kfree(secattr->mls_cat);
  984. return ret_val;
  985. } else if (ret_val > 0) {
  986. secattr->mls_cat_len = ret_val;
  987. secattr->flags |= NETLBL_SECATTR_MLS_CAT;
  988. }
  989. }
  990. return 0;
  991. }
  992. /**
  993. * cipso_v4_validate - Validate a CIPSO option
  994. * @option: the start of the option, on error it is set to point to the error
  995. *
  996. * Description:
  997. * This routine is called to validate a CIPSO option, it checks all of the
  998. * fields to ensure that they are at least valid, see the draft snippet below
  999. * for details. If the option is valid then a zero value is returned and
  1000. * the value of @option is unchanged. If the option is invalid then a
  1001. * non-zero value is returned and @option is adjusted to point to the
  1002. * offending portion of the option. From the IETF draft ...
  1003. *
  1004. * "If any field within the CIPSO options, such as the DOI identifier, is not
  1005. * recognized the IP datagram is discarded and an ICMP 'parameter problem'
  1006. * (type 12) is generated and returned. The ICMP code field is set to 'bad
  1007. * parameter' (code 0) and the pointer is set to the start of the CIPSO field
  1008. * that is unrecognized."
  1009. *
  1010. */
  1011. int cipso_v4_validate(unsigned char **option)
  1012. {
  1013. unsigned char *opt = *option;
  1014. unsigned char *tag;
  1015. unsigned char opt_iter;
  1016. unsigned char err_offset = 0;
  1017. u8 opt_len;
  1018. u8 tag_len;
  1019. struct cipso_v4_doi *doi_def = NULL;
  1020. u32 tag_iter;
  1021. /* caller already checks for length values that are too large */
  1022. opt_len = opt[1];
  1023. if (opt_len < 8) {
  1024. err_offset = 1;
  1025. goto validate_return;
  1026. }
  1027. rcu_read_lock();
  1028. doi_def = cipso_v4_doi_getdef(ntohl(*((__be32 *)&opt[2])));
  1029. if (doi_def == NULL) {
  1030. err_offset = 2;
  1031. goto validate_return_locked;
  1032. }
  1033. opt_iter = 6;
  1034. tag = opt + opt_iter;
  1035. while (opt_iter < opt_len) {
  1036. for (tag_iter = 0; doi_def->tags[tag_iter] != tag[0];)
  1037. if (doi_def->tags[tag_iter] == CIPSO_V4_TAG_INVALID ||
  1038. ++tag_iter == CIPSO_V4_TAG_MAXCNT) {
  1039. err_offset = opt_iter;
  1040. goto validate_return_locked;
  1041. }
  1042. tag_len = tag[1];
  1043. if (tag_len > (opt_len - opt_iter)) {
  1044. err_offset = opt_iter + 1;
  1045. goto validate_return_locked;
  1046. }
  1047. switch (tag[0]) {
  1048. case CIPSO_V4_TAG_RBITMAP:
  1049. if (tag_len < 4) {
  1050. err_offset = opt_iter + 1;
  1051. goto validate_return_locked;
  1052. }
  1053. /* We are already going to do all the verification
  1054. * necessary at the socket layer so from our point of
  1055. * view it is safe to turn these checks off (and less
  1056. * work), however, the CIPSO draft says we should do
  1057. * all the CIPSO validations here but it doesn't
  1058. * really specify _exactly_ what we need to validate
  1059. * ... so, just make it a sysctl tunable. */
  1060. if (cipso_v4_rbm_strictvalid) {
  1061. if (cipso_v4_map_lvl_valid(doi_def,
  1062. tag[3]) < 0) {
  1063. err_offset = opt_iter + 3;
  1064. goto validate_return_locked;
  1065. }
  1066. if (tag_len > 4 &&
  1067. cipso_v4_map_cat_rbm_valid(doi_def,
  1068. &tag[4],
  1069. tag_len - 4) < 0) {
  1070. err_offset = opt_iter + 4;
  1071. goto validate_return_locked;
  1072. }
  1073. }
  1074. break;
  1075. default:
  1076. err_offset = opt_iter;
  1077. goto validate_return_locked;
  1078. }
  1079. tag += tag_len;
  1080. opt_iter += tag_len;
  1081. }
  1082. validate_return_locked:
  1083. rcu_read_unlock();
  1084. validate_return:
  1085. *option = opt + err_offset;
  1086. return err_offset;
  1087. }
  1088. /**
  1089. * cipso_v4_error - Send the correct reponse for a bad packet
  1090. * @skb: the packet
  1091. * @error: the error code
  1092. * @gateway: CIPSO gateway flag
  1093. *
  1094. * Description:
  1095. * Based on the error code given in @error, send an ICMP error message back to
  1096. * the originating host. From the IETF draft ...
  1097. *
  1098. * "If the contents of the CIPSO [option] are valid but the security label is
  1099. * outside of the configured host or port label range, the datagram is
  1100. * discarded and an ICMP 'destination unreachable' (type 3) is generated and
  1101. * returned. The code field of the ICMP is set to 'communication with
  1102. * destination network administratively prohibited' (code 9) or to
  1103. * 'communication with destination host administratively prohibited'
  1104. * (code 10). The value of the code is dependent on whether the originator
  1105. * of the ICMP message is acting as a CIPSO host or a CIPSO gateway. The
  1106. * recipient of the ICMP message MUST be able to handle either value. The
  1107. * same procedure is performed if a CIPSO [option] can not be added to an
  1108. * IP packet because it is too large to fit in the IP options area."
  1109. *
  1110. * "If the error is triggered by receipt of an ICMP message, the message is
  1111. * discarded and no response is permitted (consistent with general ICMP
  1112. * processing rules)."
  1113. *
  1114. */
  1115. void cipso_v4_error(struct sk_buff *skb, int error, u32 gateway)
  1116. {
  1117. if (skb->nh.iph->protocol == IPPROTO_ICMP || error != -EACCES)
  1118. return;
  1119. if (gateway)
  1120. icmp_send(skb, ICMP_DEST_UNREACH, ICMP_NET_ANO, 0);
  1121. else
  1122. icmp_send(skb, ICMP_DEST_UNREACH, ICMP_HOST_ANO, 0);
  1123. }
  1124. /**
  1125. * cipso_v4_socket_setattr - Add a CIPSO option to a socket
  1126. * @sock: the socket
  1127. * @doi_def: the CIPSO DOI to use
  1128. * @secattr: the specific security attributes of the socket
  1129. *
  1130. * Description:
  1131. * Set the CIPSO option on the given socket using the DOI definition and
  1132. * security attributes passed to the function. This function requires
  1133. * exclusive access to @sock->sk, which means it either needs to be in the
  1134. * process of being created or locked via lock_sock(sock->sk). Returns zero on
  1135. * success and negative values on failure.
  1136. *
  1137. */
  1138. int cipso_v4_socket_setattr(const struct socket *sock,
  1139. const struct cipso_v4_doi *doi_def,
  1140. const struct netlbl_lsm_secattr *secattr)
  1141. {
  1142. int ret_val = -EPERM;
  1143. u32 iter;
  1144. unsigned char *buf;
  1145. u32 buf_len = 0;
  1146. u32 opt_len;
  1147. struct ip_options *opt = NULL;
  1148. struct sock *sk;
  1149. struct inet_sock *sk_inet;
  1150. struct inet_connection_sock *sk_conn;
  1151. /* In the case of sock_create_lite(), the sock->sk field is not
  1152. * defined yet but it is not a problem as the only users of these
  1153. * "lite" PF_INET sockets are functions which do an accept() call
  1154. * afterwards so we will label the socket as part of the accept(). */
  1155. sk = sock->sk;
  1156. if (sk == NULL)
  1157. return 0;
  1158. /* We allocate the maximum CIPSO option size here so we are probably
  1159. * being a little wasteful, but it makes our life _much_ easier later
  1160. * on and after all we are only talking about 40 bytes. */
  1161. buf_len = CIPSO_V4_OPT_LEN_MAX;
  1162. buf = kmalloc(buf_len, GFP_ATOMIC);
  1163. if (buf == NULL) {
  1164. ret_val = -ENOMEM;
  1165. goto socket_setattr_failure;
  1166. }
  1167. /* XXX - This code assumes only one tag per CIPSO option which isn't
  1168. * really a good assumption to make but since we only support the MAC
  1169. * tags right now it is a safe assumption. */
  1170. iter = 0;
  1171. do {
  1172. memset(buf, 0, buf_len);
  1173. switch (doi_def->tags[iter]) {
  1174. case CIPSO_V4_TAG_RBITMAP:
  1175. ret_val = cipso_v4_gentag_rbm(doi_def,
  1176. secattr,
  1177. &buf[CIPSO_V4_HDR_LEN],
  1178. buf_len - CIPSO_V4_HDR_LEN);
  1179. break;
  1180. default:
  1181. ret_val = -EPERM;
  1182. goto socket_setattr_failure;
  1183. }
  1184. iter++;
  1185. } while (ret_val < 0 &&
  1186. iter < CIPSO_V4_TAG_MAXCNT &&
  1187. doi_def->tags[iter] != CIPSO_V4_TAG_INVALID);
  1188. if (ret_val < 0)
  1189. goto socket_setattr_failure;
  1190. cipso_v4_gentag_hdr(doi_def, buf, ret_val);
  1191. buf_len = CIPSO_V4_HDR_LEN + ret_val;
  1192. /* We can't use ip_options_get() directly because it makes a call to
  1193. * ip_options_get_alloc() which allocates memory with GFP_KERNEL and
  1194. * we won't always have CAP_NET_RAW even though we _always_ want to
  1195. * set the IPOPT_CIPSO option. */
  1196. opt_len = (buf_len + 3) & ~3;
  1197. opt = kzalloc(sizeof(*opt) + opt_len, GFP_ATOMIC);
  1198. if (opt == NULL) {
  1199. ret_val = -ENOMEM;
  1200. goto socket_setattr_failure;
  1201. }
  1202. memcpy(opt->__data, buf, buf_len);
  1203. opt->optlen = opt_len;
  1204. opt->is_data = 1;
  1205. opt->cipso = sizeof(struct iphdr);
  1206. kfree(buf);
  1207. buf = NULL;
  1208. sk_inet = inet_sk(sk);
  1209. if (sk_inet->is_icsk) {
  1210. sk_conn = inet_csk(sk);
  1211. if (sk_inet->opt)
  1212. sk_conn->icsk_ext_hdr_len -= sk_inet->opt->optlen;
  1213. sk_conn->icsk_ext_hdr_len += opt->optlen;
  1214. sk_conn->icsk_sync_mss(sk, sk_conn->icsk_pmtu_cookie);
  1215. }
  1216. opt = xchg(&sk_inet->opt, opt);
  1217. kfree(opt);
  1218. return 0;
  1219. socket_setattr_failure:
  1220. kfree(buf);
  1221. kfree(opt);
  1222. return ret_val;
  1223. }
  1224. /**
  1225. * cipso_v4_sock_getattr - Get the security attributes from a sock
  1226. * @sk: the sock
  1227. * @secattr: the security attributes
  1228. *
  1229. * Description:
  1230. * Query @sk to see if there is a CIPSO option attached to the sock and if
  1231. * there is return the CIPSO security attributes in @secattr. This function
  1232. * requires that @sk be locked, or privately held, but it does not do any
  1233. * locking itself. Returns zero on success and negative values on failure.
  1234. *
  1235. */
  1236. int cipso_v4_sock_getattr(struct sock *sk, struct netlbl_lsm_secattr *secattr)
  1237. {
  1238. int ret_val = -ENOMSG;
  1239. struct inet_sock *sk_inet;
  1240. unsigned char *cipso_ptr;
  1241. u32 doi;
  1242. struct cipso_v4_doi *doi_def;
  1243. sk_inet = inet_sk(sk);
  1244. if (sk_inet->opt == NULL || sk_inet->opt->cipso == 0)
  1245. return -ENOMSG;
  1246. cipso_ptr = sk_inet->opt->__data + sk_inet->opt->cipso -
  1247. sizeof(struct iphdr);
  1248. ret_val = cipso_v4_cache_check(cipso_ptr, cipso_ptr[1], secattr);
  1249. if (ret_val == 0)
  1250. return ret_val;
  1251. doi = ntohl(*(__be32 *)&cipso_ptr[2]);
  1252. rcu_read_lock();
  1253. doi_def = cipso_v4_doi_getdef(doi);
  1254. if (doi_def == NULL) {
  1255. rcu_read_unlock();
  1256. return -ENOMSG;
  1257. }
  1258. /* XXX - This code assumes only one tag per CIPSO option which isn't
  1259. * really a good assumption to make but since we only support the MAC
  1260. * tags right now it is a safe assumption. */
  1261. switch (cipso_ptr[6]) {
  1262. case CIPSO_V4_TAG_RBITMAP:
  1263. ret_val = cipso_v4_parsetag_rbm(doi_def,
  1264. &cipso_ptr[6],
  1265. secattr);
  1266. break;
  1267. }
  1268. rcu_read_unlock();
  1269. return ret_val;
  1270. }
  1271. /**
  1272. * cipso_v4_socket_getattr - Get the security attributes from a socket
  1273. * @sock: the socket
  1274. * @secattr: the security attributes
  1275. *
  1276. * Description:
  1277. * Query @sock to see if there is a CIPSO option attached to the socket and if
  1278. * there is return the CIPSO security attributes in @secattr. Returns zero on
  1279. * success and negative values on failure.
  1280. *
  1281. */
  1282. int cipso_v4_socket_getattr(const struct socket *sock,
  1283. struct netlbl_lsm_secattr *secattr)
  1284. {
  1285. int ret_val;
  1286. lock_sock(sock->sk);
  1287. ret_val = cipso_v4_sock_getattr(sock->sk, secattr);
  1288. release_sock(sock->sk);
  1289. return ret_val;
  1290. }
  1291. /**
  1292. * cipso_v4_skbuff_getattr - Get the security attributes from the CIPSO option
  1293. * @skb: the packet
  1294. * @secattr: the security attributes
  1295. *
  1296. * Description:
  1297. * Parse the given packet's CIPSO option and return the security attributes.
  1298. * Returns zero on success and negative values on failure.
  1299. *
  1300. */
  1301. int cipso_v4_skbuff_getattr(const struct sk_buff *skb,
  1302. struct netlbl_lsm_secattr *secattr)
  1303. {
  1304. int ret_val = -ENOMSG;
  1305. unsigned char *cipso_ptr;
  1306. u32 doi;
  1307. struct cipso_v4_doi *doi_def;
  1308. cipso_ptr = CIPSO_V4_OPTPTR(skb);
  1309. if (cipso_v4_cache_check(cipso_ptr, cipso_ptr[1], secattr) == 0)
  1310. return 0;
  1311. doi = ntohl(*(__be32 *)&cipso_ptr[2]);
  1312. rcu_read_lock();
  1313. doi_def = cipso_v4_doi_getdef(doi);
  1314. if (doi_def == NULL)
  1315. goto skbuff_getattr_return;
  1316. /* XXX - This code assumes only one tag per CIPSO option which isn't
  1317. * really a good assumption to make but since we only support the MAC
  1318. * tags right now it is a safe assumption. */
  1319. switch (cipso_ptr[6]) {
  1320. case CIPSO_V4_TAG_RBITMAP:
  1321. ret_val = cipso_v4_parsetag_rbm(doi_def,
  1322. &cipso_ptr[6],
  1323. secattr);
  1324. break;
  1325. }
  1326. skbuff_getattr_return:
  1327. rcu_read_unlock();
  1328. return ret_val;
  1329. }
  1330. /*
  1331. * Setup Functions
  1332. */
  1333. /**
  1334. * cipso_v4_init - Initialize the CIPSO module
  1335. *
  1336. * Description:
  1337. * Initialize the CIPSO module and prepare it for use. Returns zero on success
  1338. * and negative values on failure.
  1339. *
  1340. */
  1341. static int __init cipso_v4_init(void)
  1342. {
  1343. int ret_val;
  1344. ret_val = cipso_v4_cache_init();
  1345. if (ret_val != 0)
  1346. panic("Failed to initialize the CIPSO/IPv4 cache (%d)\n",
  1347. ret_val);
  1348. return 0;
  1349. }
  1350. subsys_initcall(cipso_v4_init);