cipso_ipv4.c 40 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480
  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. if (prev_entry == NULL) {
  292. spin_unlock_bh(&cipso_v4_cache[bkt].lock);
  293. return 0;
  294. }
  295. if (prev_entry->activity > 0)
  296. prev_entry->activity -= 1;
  297. if (entry->activity > prev_entry->activity &&
  298. entry->activity - prev_entry->activity >
  299. CIPSO_V4_CACHE_REORDERLIMIT) {
  300. __list_del(entry->list.prev, entry->list.next);
  301. __list_add(&entry->list,
  302. prev_entry->list.prev,
  303. &prev_entry->list);
  304. }
  305. spin_unlock_bh(&cipso_v4_cache[bkt].lock);
  306. return 0;
  307. }
  308. prev_entry = entry;
  309. }
  310. spin_unlock_bh(&cipso_v4_cache[bkt].lock);
  311. return -ENOENT;
  312. }
  313. /**
  314. * cipso_v4_cache_add - Add an entry to the CIPSO cache
  315. * @skb: the packet
  316. * @secattr: the packet's security attributes
  317. *
  318. * Description:
  319. * Add a new entry into the CIPSO label mapping cache. Add the new entry to
  320. * head of the cache bucket's list, if the cache bucket is out of room remove
  321. * the last entry in the list first. It is important to note that there is
  322. * currently no checking for duplicate keys. Returns zero on success,
  323. * negative values on failure.
  324. *
  325. */
  326. int cipso_v4_cache_add(const struct sk_buff *skb,
  327. const struct netlbl_lsm_secattr *secattr)
  328. {
  329. int ret_val = -EPERM;
  330. u32 bkt;
  331. struct cipso_v4_map_cache_entry *entry = NULL;
  332. struct cipso_v4_map_cache_entry *old_entry = NULL;
  333. unsigned char *cipso_ptr;
  334. u32 cipso_ptr_len;
  335. if (!cipso_v4_cache_enabled || cipso_v4_cache_bucketsize <= 0)
  336. return 0;
  337. cipso_ptr = CIPSO_V4_OPTPTR(skb);
  338. cipso_ptr_len = cipso_ptr[1];
  339. entry = kzalloc(sizeof(*entry), GFP_ATOMIC);
  340. if (entry == NULL)
  341. return -ENOMEM;
  342. entry->key = kmemdup(cipso_ptr, cipso_ptr_len, GFP_ATOMIC);
  343. if (entry->key == NULL) {
  344. ret_val = -ENOMEM;
  345. goto cache_add_failure;
  346. }
  347. entry->key_len = cipso_ptr_len;
  348. entry->hash = cipso_v4_map_cache_hash(cipso_ptr, cipso_ptr_len);
  349. atomic_inc(&secattr->cache->refcount);
  350. entry->lsm_data = secattr->cache;
  351. bkt = entry->hash & (CIPSO_V4_CACHE_BUCKETBITS - 1);
  352. spin_lock_bh(&cipso_v4_cache[bkt].lock);
  353. if (cipso_v4_cache[bkt].size < cipso_v4_cache_bucketsize) {
  354. list_add(&entry->list, &cipso_v4_cache[bkt].list);
  355. cipso_v4_cache[bkt].size += 1;
  356. } else {
  357. old_entry = list_entry(cipso_v4_cache[bkt].list.prev,
  358. struct cipso_v4_map_cache_entry, list);
  359. list_del(&old_entry->list);
  360. list_add(&entry->list, &cipso_v4_cache[bkt].list);
  361. cipso_v4_cache_entry_free(old_entry);
  362. }
  363. spin_unlock_bh(&cipso_v4_cache[bkt].lock);
  364. return 0;
  365. cache_add_failure:
  366. if (entry)
  367. cipso_v4_cache_entry_free(entry);
  368. return ret_val;
  369. }
  370. /*
  371. * DOI List Functions
  372. */
  373. /**
  374. * cipso_v4_doi_search - Searches for a DOI definition
  375. * @doi: the DOI to search for
  376. *
  377. * Description:
  378. * Search the DOI definition list for a DOI definition with a DOI value that
  379. * matches @doi. The caller is responsibile for calling rcu_read_[un]lock().
  380. * Returns a pointer to the DOI definition on success and NULL on failure.
  381. */
  382. static struct cipso_v4_doi *cipso_v4_doi_search(u32 doi)
  383. {
  384. struct cipso_v4_doi *iter;
  385. list_for_each_entry_rcu(iter, &cipso_v4_doi_list, list)
  386. if (iter->doi == doi && iter->valid)
  387. return iter;
  388. return NULL;
  389. }
  390. /**
  391. * cipso_v4_doi_add - Add a new DOI to the CIPSO protocol engine
  392. * @doi_def: the DOI structure
  393. *
  394. * Description:
  395. * The caller defines a new DOI for use by the CIPSO engine and calls this
  396. * function to add it to the list of acceptable domains. The caller must
  397. * ensure that the mapping table specified in @doi_def->map meets all of the
  398. * requirements of the mapping type (see cipso_ipv4.h for details). Returns
  399. * zero on success and non-zero on failure.
  400. *
  401. */
  402. int cipso_v4_doi_add(struct cipso_v4_doi *doi_def)
  403. {
  404. if (doi_def == NULL || doi_def->doi == CIPSO_V4_DOI_UNKNOWN)
  405. return -EINVAL;
  406. doi_def->valid = 1;
  407. INIT_RCU_HEAD(&doi_def->rcu);
  408. INIT_LIST_HEAD(&doi_def->dom_list);
  409. rcu_read_lock();
  410. if (cipso_v4_doi_search(doi_def->doi) != NULL)
  411. goto doi_add_failure_rlock;
  412. spin_lock(&cipso_v4_doi_list_lock);
  413. if (cipso_v4_doi_search(doi_def->doi) != NULL)
  414. goto doi_add_failure_slock;
  415. list_add_tail_rcu(&doi_def->list, &cipso_v4_doi_list);
  416. spin_unlock(&cipso_v4_doi_list_lock);
  417. rcu_read_unlock();
  418. return 0;
  419. doi_add_failure_slock:
  420. spin_unlock(&cipso_v4_doi_list_lock);
  421. doi_add_failure_rlock:
  422. rcu_read_unlock();
  423. return -EEXIST;
  424. }
  425. /**
  426. * cipso_v4_doi_remove - Remove an existing DOI from the CIPSO protocol engine
  427. * @doi: the DOI value
  428. * @audit_secid: the LSM secid to use in the audit message
  429. * @callback: the DOI cleanup/free callback
  430. *
  431. * Description:
  432. * Removes a DOI definition from the CIPSO engine, @callback is called to
  433. * free any memory. The NetLabel routines will be called to release their own
  434. * LSM domain mappings as well as our own domain list. Returns zero on
  435. * success and negative values on failure.
  436. *
  437. */
  438. int cipso_v4_doi_remove(u32 doi,
  439. struct netlbl_audit *audit_info,
  440. void (*callback) (struct rcu_head * head))
  441. {
  442. struct cipso_v4_doi *doi_def;
  443. struct cipso_v4_domhsh_entry *dom_iter;
  444. rcu_read_lock();
  445. if (cipso_v4_doi_search(doi) != NULL) {
  446. spin_lock(&cipso_v4_doi_list_lock);
  447. doi_def = cipso_v4_doi_search(doi);
  448. if (doi_def == NULL) {
  449. spin_unlock(&cipso_v4_doi_list_lock);
  450. rcu_read_unlock();
  451. return -ENOENT;
  452. }
  453. doi_def->valid = 0;
  454. list_del_rcu(&doi_def->list);
  455. spin_unlock(&cipso_v4_doi_list_lock);
  456. list_for_each_entry_rcu(dom_iter, &doi_def->dom_list, list)
  457. if (dom_iter->valid)
  458. netlbl_domhsh_remove(dom_iter->domain,
  459. audit_info);
  460. cipso_v4_cache_invalidate();
  461. rcu_read_unlock();
  462. call_rcu(&doi_def->rcu, callback);
  463. return 0;
  464. }
  465. rcu_read_unlock();
  466. return -ENOENT;
  467. }
  468. /**
  469. * cipso_v4_doi_getdef - Returns a pointer to a valid DOI definition
  470. * @doi: the DOI value
  471. *
  472. * Description:
  473. * Searches for a valid DOI definition and if one is found it is returned to
  474. * the caller. Otherwise NULL is returned. The caller must ensure that
  475. * rcu_read_lock() is held while accessing the returned definition.
  476. *
  477. */
  478. struct cipso_v4_doi *cipso_v4_doi_getdef(u32 doi)
  479. {
  480. return cipso_v4_doi_search(doi);
  481. }
  482. /**
  483. * cipso_v4_doi_walk - Iterate through the DOI definitions
  484. * @skip_cnt: skip past this number of DOI definitions, updated
  485. * @callback: callback for each DOI definition
  486. * @cb_arg: argument for the callback function
  487. *
  488. * Description:
  489. * Iterate over the DOI definition list, skipping the first @skip_cnt entries.
  490. * For each entry call @callback, if @callback returns a negative value stop
  491. * 'walking' through the list and return. Updates the value in @skip_cnt upon
  492. * return. Returns zero on success, negative values on failure.
  493. *
  494. */
  495. int cipso_v4_doi_walk(u32 *skip_cnt,
  496. int (*callback) (struct cipso_v4_doi *doi_def, void *arg),
  497. void *cb_arg)
  498. {
  499. int ret_val = -ENOENT;
  500. u32 doi_cnt = 0;
  501. struct cipso_v4_doi *iter_doi;
  502. rcu_read_lock();
  503. list_for_each_entry_rcu(iter_doi, &cipso_v4_doi_list, list)
  504. if (iter_doi->valid) {
  505. if (doi_cnt++ < *skip_cnt)
  506. continue;
  507. ret_val = callback(iter_doi, cb_arg);
  508. if (ret_val < 0) {
  509. doi_cnt--;
  510. goto doi_walk_return;
  511. }
  512. }
  513. doi_walk_return:
  514. rcu_read_unlock();
  515. *skip_cnt = doi_cnt;
  516. return ret_val;
  517. }
  518. /**
  519. * cipso_v4_doi_domhsh_add - Adds a domain entry to a DOI definition
  520. * @doi_def: the DOI definition
  521. * @domain: the domain to add
  522. *
  523. * Description:
  524. * Adds the @domain to the the DOI specified by @doi_def, this function
  525. * should only be called by external functions (i.e. NetLabel). This function
  526. * does allocate memory. Returns zero on success, negative values on failure.
  527. *
  528. */
  529. int cipso_v4_doi_domhsh_add(struct cipso_v4_doi *doi_def, const char *domain)
  530. {
  531. struct cipso_v4_domhsh_entry *iter;
  532. struct cipso_v4_domhsh_entry *new_dom;
  533. new_dom = kzalloc(sizeof(*new_dom), GFP_KERNEL);
  534. if (new_dom == NULL)
  535. return -ENOMEM;
  536. if (domain) {
  537. new_dom->domain = kstrdup(domain, GFP_KERNEL);
  538. if (new_dom->domain == NULL) {
  539. kfree(new_dom);
  540. return -ENOMEM;
  541. }
  542. }
  543. new_dom->valid = 1;
  544. INIT_RCU_HEAD(&new_dom->rcu);
  545. rcu_read_lock();
  546. spin_lock(&cipso_v4_doi_list_lock);
  547. list_for_each_entry_rcu(iter, &doi_def->dom_list, list)
  548. if (iter->valid &&
  549. ((domain != NULL && iter->domain != NULL &&
  550. strcmp(iter->domain, domain) == 0) ||
  551. (domain == NULL && iter->domain == NULL))) {
  552. spin_unlock(&cipso_v4_doi_list_lock);
  553. rcu_read_unlock();
  554. kfree(new_dom->domain);
  555. kfree(new_dom);
  556. return -EEXIST;
  557. }
  558. list_add_tail_rcu(&new_dom->list, &doi_def->dom_list);
  559. spin_unlock(&cipso_v4_doi_list_lock);
  560. rcu_read_unlock();
  561. return 0;
  562. }
  563. /**
  564. * cipso_v4_doi_domhsh_remove - Removes a domain entry from a DOI definition
  565. * @doi_def: the DOI definition
  566. * @domain: the domain to remove
  567. *
  568. * Description:
  569. * Removes the @domain from the DOI specified by @doi_def, this function
  570. * should only be called by external functions (i.e. NetLabel). Returns zero
  571. * on success and negative values on error.
  572. *
  573. */
  574. int cipso_v4_doi_domhsh_remove(struct cipso_v4_doi *doi_def,
  575. const char *domain)
  576. {
  577. struct cipso_v4_domhsh_entry *iter;
  578. rcu_read_lock();
  579. spin_lock(&cipso_v4_doi_list_lock);
  580. list_for_each_entry_rcu(iter, &doi_def->dom_list, list)
  581. if (iter->valid &&
  582. ((domain != NULL && iter->domain != NULL &&
  583. strcmp(iter->domain, domain) == 0) ||
  584. (domain == NULL && iter->domain == NULL))) {
  585. iter->valid = 0;
  586. list_del_rcu(&iter->list);
  587. spin_unlock(&cipso_v4_doi_list_lock);
  588. rcu_read_unlock();
  589. call_rcu(&iter->rcu, cipso_v4_doi_domhsh_free);
  590. return 0;
  591. }
  592. spin_unlock(&cipso_v4_doi_list_lock);
  593. rcu_read_unlock();
  594. return -ENOENT;
  595. }
  596. /*
  597. * Label Mapping Functions
  598. */
  599. /**
  600. * cipso_v4_map_lvl_valid - Checks to see if the given level is understood
  601. * @doi_def: the DOI definition
  602. * @level: the level to check
  603. *
  604. * Description:
  605. * Checks the given level against the given DOI definition and returns a
  606. * negative value if the level does not have a valid mapping and a zero value
  607. * if the level is defined by the DOI.
  608. *
  609. */
  610. static int cipso_v4_map_lvl_valid(const struct cipso_v4_doi *doi_def, u8 level)
  611. {
  612. switch (doi_def->type) {
  613. case CIPSO_V4_MAP_PASS:
  614. return 0;
  615. case CIPSO_V4_MAP_STD:
  616. if (doi_def->map.std->lvl.cipso[level] < CIPSO_V4_INV_LVL)
  617. return 0;
  618. break;
  619. }
  620. return -EFAULT;
  621. }
  622. /**
  623. * cipso_v4_map_lvl_hton - Perform a level mapping from the host to the network
  624. * @doi_def: the DOI definition
  625. * @host_lvl: the host MLS level
  626. * @net_lvl: the network/CIPSO MLS level
  627. *
  628. * Description:
  629. * Perform a label mapping to translate a local MLS level to the correct
  630. * CIPSO level using the given DOI definition. Returns zero on success,
  631. * negative values otherwise.
  632. *
  633. */
  634. static int cipso_v4_map_lvl_hton(const struct cipso_v4_doi *doi_def,
  635. u32 host_lvl,
  636. u32 *net_lvl)
  637. {
  638. switch (doi_def->type) {
  639. case CIPSO_V4_MAP_PASS:
  640. *net_lvl = host_lvl;
  641. return 0;
  642. case CIPSO_V4_MAP_STD:
  643. if (host_lvl < doi_def->map.std->lvl.local_size) {
  644. *net_lvl = doi_def->map.std->lvl.local[host_lvl];
  645. return 0;
  646. }
  647. break;
  648. }
  649. return -EINVAL;
  650. }
  651. /**
  652. * cipso_v4_map_lvl_ntoh - Perform a level mapping from the network to the host
  653. * @doi_def: the DOI definition
  654. * @net_lvl: the network/CIPSO MLS level
  655. * @host_lvl: the host MLS level
  656. *
  657. * Description:
  658. * Perform a label mapping to translate a CIPSO level to the correct local MLS
  659. * level using the given DOI definition. Returns zero on success, negative
  660. * values otherwise.
  661. *
  662. */
  663. static int cipso_v4_map_lvl_ntoh(const struct cipso_v4_doi *doi_def,
  664. u32 net_lvl,
  665. u32 *host_lvl)
  666. {
  667. struct cipso_v4_std_map_tbl *map_tbl;
  668. switch (doi_def->type) {
  669. case CIPSO_V4_MAP_PASS:
  670. *host_lvl = net_lvl;
  671. return 0;
  672. case CIPSO_V4_MAP_STD:
  673. map_tbl = doi_def->map.std;
  674. if (net_lvl < map_tbl->lvl.cipso_size &&
  675. map_tbl->lvl.cipso[net_lvl] < CIPSO_V4_INV_LVL) {
  676. *host_lvl = doi_def->map.std->lvl.cipso[net_lvl];
  677. return 0;
  678. }
  679. break;
  680. }
  681. return -EINVAL;
  682. }
  683. /**
  684. * cipso_v4_map_cat_rbm_valid - Checks to see if the category bitmap is valid
  685. * @doi_def: the DOI definition
  686. * @bitmap: category bitmap
  687. * @bitmap_len: bitmap length in bytes
  688. *
  689. * Description:
  690. * Checks the given category bitmap against the given DOI definition and
  691. * returns a negative value if any of the categories in the bitmap do not have
  692. * a valid mapping and a zero value if all of the categories are valid.
  693. *
  694. */
  695. static int cipso_v4_map_cat_rbm_valid(const struct cipso_v4_doi *doi_def,
  696. const unsigned char *bitmap,
  697. u32 bitmap_len)
  698. {
  699. int cat = -1;
  700. u32 bitmap_len_bits = bitmap_len * 8;
  701. u32 cipso_cat_size;
  702. u32 *cipso_array;
  703. switch (doi_def->type) {
  704. case CIPSO_V4_MAP_PASS:
  705. return 0;
  706. case CIPSO_V4_MAP_STD:
  707. cipso_cat_size = doi_def->map.std->cat.cipso_size;
  708. cipso_array = doi_def->map.std->cat.cipso;
  709. for (;;) {
  710. cat = cipso_v4_bitmap_walk(bitmap,
  711. bitmap_len_bits,
  712. cat + 1,
  713. 1);
  714. if (cat < 0)
  715. break;
  716. if (cat >= cipso_cat_size ||
  717. cipso_array[cat] >= CIPSO_V4_INV_CAT)
  718. return -EFAULT;
  719. }
  720. if (cat == -1)
  721. return 0;
  722. break;
  723. }
  724. return -EFAULT;
  725. }
  726. /**
  727. * cipso_v4_map_cat_rbm_hton - Perform a category mapping from host to network
  728. * @doi_def: the DOI definition
  729. * @host_cat: the category bitmap in host format
  730. * @host_cat_len: the length of the host's category bitmap in bytes
  731. * @net_cat: the zero'd out category bitmap in network/CIPSO format
  732. * @net_cat_len: the length of the CIPSO bitmap in bytes
  733. *
  734. * Description:
  735. * Perform a label mapping to translate a local MLS category bitmap to the
  736. * correct CIPSO bitmap using the given DOI definition. Returns the minimum
  737. * size in bytes of the network bitmap on success, negative values otherwise.
  738. *
  739. */
  740. static int cipso_v4_map_cat_rbm_hton(const struct cipso_v4_doi *doi_def,
  741. const unsigned char *host_cat,
  742. u32 host_cat_len,
  743. unsigned char *net_cat,
  744. u32 net_cat_len)
  745. {
  746. int host_spot = -1;
  747. u32 net_spot;
  748. u32 net_spot_max = 0;
  749. u32 host_clen_bits = host_cat_len * 8;
  750. u32 net_clen_bits = net_cat_len * 8;
  751. u32 host_cat_size;
  752. u32 *host_cat_array;
  753. switch (doi_def->type) {
  754. case CIPSO_V4_MAP_PASS:
  755. net_spot_max = host_cat_len;
  756. while (net_spot_max > 0 && host_cat[net_spot_max - 1] == 0)
  757. net_spot_max--;
  758. if (net_spot_max > net_cat_len)
  759. return -EINVAL;
  760. memcpy(net_cat, host_cat, net_spot_max);
  761. return net_spot_max;
  762. case CIPSO_V4_MAP_STD:
  763. host_cat_size = doi_def->map.std->cat.local_size;
  764. host_cat_array = doi_def->map.std->cat.local;
  765. for (;;) {
  766. host_spot = cipso_v4_bitmap_walk(host_cat,
  767. host_clen_bits,
  768. host_spot + 1,
  769. 1);
  770. if (host_spot < 0)
  771. break;
  772. if (host_spot >= host_cat_size)
  773. return -EPERM;
  774. net_spot = host_cat_array[host_spot];
  775. if (net_spot >= net_clen_bits)
  776. return -ENOSPC;
  777. cipso_v4_bitmap_setbit(net_cat, net_spot, 1);
  778. if (net_spot > net_spot_max)
  779. net_spot_max = net_spot;
  780. }
  781. if (host_spot == -2)
  782. return -EFAULT;
  783. if (++net_spot_max % 8)
  784. return net_spot_max / 8 + 1;
  785. return net_spot_max / 8;
  786. }
  787. return -EINVAL;
  788. }
  789. /**
  790. * cipso_v4_map_cat_rbm_ntoh - Perform a category mapping from network to host
  791. * @doi_def: the DOI definition
  792. * @net_cat: the category bitmap in network/CIPSO format
  793. * @net_cat_len: the length of the CIPSO bitmap in bytes
  794. * @host_cat: the zero'd out category bitmap in host format
  795. * @host_cat_len: the length of the host's category bitmap in bytes
  796. *
  797. * Description:
  798. * Perform a label mapping to translate a CIPSO bitmap to the correct local
  799. * MLS category bitmap using the given DOI definition. Returns the minimum
  800. * size in bytes of the host bitmap on success, negative values otherwise.
  801. *
  802. */
  803. static int cipso_v4_map_cat_rbm_ntoh(const struct cipso_v4_doi *doi_def,
  804. const unsigned char *net_cat,
  805. u32 net_cat_len,
  806. unsigned char *host_cat,
  807. u32 host_cat_len)
  808. {
  809. u32 host_spot;
  810. u32 host_spot_max = 0;
  811. int net_spot = -1;
  812. u32 net_clen_bits = net_cat_len * 8;
  813. u32 host_clen_bits = host_cat_len * 8;
  814. u32 net_cat_size;
  815. u32 *net_cat_array;
  816. switch (doi_def->type) {
  817. case CIPSO_V4_MAP_PASS:
  818. if (net_cat_len > host_cat_len)
  819. return -EINVAL;
  820. memcpy(host_cat, net_cat, net_cat_len);
  821. return net_cat_len;
  822. case CIPSO_V4_MAP_STD:
  823. net_cat_size = doi_def->map.std->cat.cipso_size;
  824. net_cat_array = doi_def->map.std->cat.cipso;
  825. for (;;) {
  826. net_spot = cipso_v4_bitmap_walk(net_cat,
  827. net_clen_bits,
  828. net_spot + 1,
  829. 1);
  830. if (net_spot < 0)
  831. break;
  832. if (net_spot >= net_cat_size ||
  833. net_cat_array[net_spot] >= CIPSO_V4_INV_CAT)
  834. return -EPERM;
  835. host_spot = net_cat_array[net_spot];
  836. if (host_spot >= host_clen_bits)
  837. return -ENOSPC;
  838. cipso_v4_bitmap_setbit(host_cat, host_spot, 1);
  839. if (host_spot > host_spot_max)
  840. host_spot_max = host_spot;
  841. }
  842. if (net_spot == -2)
  843. return -EFAULT;
  844. if (++host_spot_max % 8)
  845. return host_spot_max / 8 + 1;
  846. return host_spot_max / 8;
  847. }
  848. return -EINVAL;
  849. }
  850. /*
  851. * Protocol Handling Functions
  852. */
  853. #define CIPSO_V4_HDR_LEN 6
  854. /**
  855. * cipso_v4_gentag_hdr - Generate a CIPSO option header
  856. * @doi_def: the DOI definition
  857. * @len: the total tag length in bytes
  858. * @buf: the CIPSO option buffer
  859. *
  860. * Description:
  861. * Write a CIPSO header into the beginning of @buffer. Return zero on success,
  862. * negative values on failure.
  863. *
  864. */
  865. static int cipso_v4_gentag_hdr(const struct cipso_v4_doi *doi_def,
  866. u32 len,
  867. unsigned char *buf)
  868. {
  869. if (CIPSO_V4_HDR_LEN + len > 40)
  870. return -ENOSPC;
  871. buf[0] = IPOPT_CIPSO;
  872. buf[1] = CIPSO_V4_HDR_LEN + len;
  873. *(__be32 *)&buf[2] = htonl(doi_def->doi);
  874. return 0;
  875. }
  876. #define CIPSO_V4_TAG1_CAT_LEN 30
  877. /**
  878. * cipso_v4_gentag_rbm - Generate a CIPSO restricted bitmap tag (type #1)
  879. * @doi_def: the DOI definition
  880. * @secattr: the security attributes
  881. * @buffer: the option buffer
  882. * @buffer_len: length of buffer in bytes
  883. *
  884. * Description:
  885. * Generate a CIPSO option using the restricted bitmap tag, tag type #1. The
  886. * actual buffer length may be larger than the indicated size due to
  887. * translation between host and network category bitmaps. Returns zero on
  888. * success, negative values on failure.
  889. *
  890. */
  891. static int cipso_v4_gentag_rbm(const struct cipso_v4_doi *doi_def,
  892. const struct netlbl_lsm_secattr *secattr,
  893. unsigned char **buffer,
  894. u32 *buffer_len)
  895. {
  896. int ret_val = -EPERM;
  897. unsigned char *buf = NULL;
  898. u32 buf_len;
  899. u32 level;
  900. if (secattr->mls_cat) {
  901. buf = kzalloc(CIPSO_V4_HDR_LEN + 4 + CIPSO_V4_TAG1_CAT_LEN,
  902. GFP_ATOMIC);
  903. if (buf == NULL)
  904. return -ENOMEM;
  905. ret_val = cipso_v4_map_cat_rbm_hton(doi_def,
  906. secattr->mls_cat,
  907. secattr->mls_cat_len,
  908. &buf[CIPSO_V4_HDR_LEN + 4],
  909. CIPSO_V4_TAG1_CAT_LEN);
  910. if (ret_val < 0)
  911. goto gentag_failure;
  912. /* This will send packets using the "optimized" format when
  913. * possibile as specified in section 3.4.2.6 of the
  914. * CIPSO draft. */
  915. if (cipso_v4_rbm_optfmt && (ret_val > 0 && ret_val < 10))
  916. ret_val = 10;
  917. buf_len = 4 + ret_val;
  918. } else {
  919. buf = kzalloc(CIPSO_V4_HDR_LEN + 4, GFP_ATOMIC);
  920. if (buf == NULL)
  921. return -ENOMEM;
  922. buf_len = 4;
  923. }
  924. ret_val = cipso_v4_map_lvl_hton(doi_def, secattr->mls_lvl, &level);
  925. if (ret_val != 0)
  926. goto gentag_failure;
  927. ret_val = cipso_v4_gentag_hdr(doi_def, buf_len, buf);
  928. if (ret_val != 0)
  929. goto gentag_failure;
  930. buf[CIPSO_V4_HDR_LEN] = 0x01;
  931. buf[CIPSO_V4_HDR_LEN + 1] = buf_len;
  932. buf[CIPSO_V4_HDR_LEN + 3] = level;
  933. *buffer = buf;
  934. *buffer_len = CIPSO_V4_HDR_LEN + buf_len;
  935. return 0;
  936. gentag_failure:
  937. kfree(buf);
  938. return ret_val;
  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->mls_lvl_vld = 1;
  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. }
  986. secattr->mls_cat_len = ret_val;
  987. }
  988. return 0;
  989. }
  990. /**
  991. * cipso_v4_validate - Validate a CIPSO option
  992. * @option: the start of the option, on error it is set to point to the error
  993. *
  994. * Description:
  995. * This routine is called to validate a CIPSO option, it checks all of the
  996. * fields to ensure that they are at least valid, see the draft snippet below
  997. * for details. If the option is valid then a zero value is returned and
  998. * the value of @option is unchanged. If the option is invalid then a
  999. * non-zero value is returned and @option is adjusted to point to the
  1000. * offending portion of the option. From the IETF draft ...
  1001. *
  1002. * "If any field within the CIPSO options, such as the DOI identifier, is not
  1003. * recognized the IP datagram is discarded and an ICMP 'parameter problem'
  1004. * (type 12) is generated and returned. The ICMP code field is set to 'bad
  1005. * parameter' (code 0) and the pointer is set to the start of the CIPSO field
  1006. * that is unrecognized."
  1007. *
  1008. */
  1009. int cipso_v4_validate(unsigned char **option)
  1010. {
  1011. unsigned char *opt = *option;
  1012. unsigned char *tag;
  1013. unsigned char opt_iter;
  1014. unsigned char err_offset = 0;
  1015. u8 opt_len;
  1016. u8 tag_len;
  1017. struct cipso_v4_doi *doi_def = NULL;
  1018. u32 tag_iter;
  1019. /* caller already checks for length values that are too large */
  1020. opt_len = opt[1];
  1021. if (opt_len < 8) {
  1022. err_offset = 1;
  1023. goto validate_return;
  1024. }
  1025. rcu_read_lock();
  1026. doi_def = cipso_v4_doi_getdef(ntohl(*((__be32 *)&opt[2])));
  1027. if (doi_def == NULL) {
  1028. err_offset = 2;
  1029. goto validate_return_locked;
  1030. }
  1031. opt_iter = 6;
  1032. tag = opt + opt_iter;
  1033. while (opt_iter < opt_len) {
  1034. for (tag_iter = 0; doi_def->tags[tag_iter] != tag[0];)
  1035. if (doi_def->tags[tag_iter] == CIPSO_V4_TAG_INVALID ||
  1036. ++tag_iter == CIPSO_V4_TAG_MAXCNT) {
  1037. err_offset = opt_iter;
  1038. goto validate_return_locked;
  1039. }
  1040. tag_len = tag[1];
  1041. if (tag_len > (opt_len - opt_iter)) {
  1042. err_offset = opt_iter + 1;
  1043. goto validate_return_locked;
  1044. }
  1045. switch (tag[0]) {
  1046. case CIPSO_V4_TAG_RBITMAP:
  1047. if (tag_len < 4) {
  1048. err_offset = opt_iter + 1;
  1049. goto validate_return_locked;
  1050. }
  1051. /* We are already going to do all the verification
  1052. * necessary at the socket layer so from our point of
  1053. * view it is safe to turn these checks off (and less
  1054. * work), however, the CIPSO draft says we should do
  1055. * all the CIPSO validations here but it doesn't
  1056. * really specify _exactly_ what we need to validate
  1057. * ... so, just make it a sysctl tunable. */
  1058. if (cipso_v4_rbm_strictvalid) {
  1059. if (cipso_v4_map_lvl_valid(doi_def,
  1060. tag[3]) < 0) {
  1061. err_offset = opt_iter + 3;
  1062. goto validate_return_locked;
  1063. }
  1064. if (tag_len > 4 &&
  1065. cipso_v4_map_cat_rbm_valid(doi_def,
  1066. &tag[4],
  1067. tag_len - 4) < 0) {
  1068. err_offset = opt_iter + 4;
  1069. goto validate_return_locked;
  1070. }
  1071. }
  1072. break;
  1073. default:
  1074. err_offset = opt_iter;
  1075. goto validate_return_locked;
  1076. }
  1077. tag += tag_len;
  1078. opt_iter += tag_len;
  1079. }
  1080. validate_return_locked:
  1081. rcu_read_unlock();
  1082. validate_return:
  1083. *option = opt + err_offset;
  1084. return err_offset;
  1085. }
  1086. /**
  1087. * cipso_v4_error - Send the correct reponse for a bad packet
  1088. * @skb: the packet
  1089. * @error: the error code
  1090. * @gateway: CIPSO gateway flag
  1091. *
  1092. * Description:
  1093. * Based on the error code given in @error, send an ICMP error message back to
  1094. * the originating host. From the IETF draft ...
  1095. *
  1096. * "If the contents of the CIPSO [option] are valid but the security label is
  1097. * outside of the configured host or port label range, the datagram is
  1098. * discarded and an ICMP 'destination unreachable' (type 3) is generated and
  1099. * returned. The code field of the ICMP is set to 'communication with
  1100. * destination network administratively prohibited' (code 9) or to
  1101. * 'communication with destination host administratively prohibited'
  1102. * (code 10). The value of the code is dependent on whether the originator
  1103. * of the ICMP message is acting as a CIPSO host or a CIPSO gateway. The
  1104. * recipient of the ICMP message MUST be able to handle either value. The
  1105. * same procedure is performed if a CIPSO [option] can not be added to an
  1106. * IP packet because it is too large to fit in the IP options area."
  1107. *
  1108. * "If the error is triggered by receipt of an ICMP message, the message is
  1109. * discarded and no response is permitted (consistent with general ICMP
  1110. * processing rules)."
  1111. *
  1112. */
  1113. void cipso_v4_error(struct sk_buff *skb, int error, u32 gateway)
  1114. {
  1115. if (skb->nh.iph->protocol == IPPROTO_ICMP || error != -EACCES)
  1116. return;
  1117. if (gateway)
  1118. icmp_send(skb, ICMP_DEST_UNREACH, ICMP_NET_ANO, 0);
  1119. else
  1120. icmp_send(skb, ICMP_DEST_UNREACH, ICMP_HOST_ANO, 0);
  1121. }
  1122. /**
  1123. * cipso_v4_socket_setattr - Add a CIPSO option to a socket
  1124. * @sock: the socket
  1125. * @doi_def: the CIPSO DOI to use
  1126. * @secattr: the specific security attributes of the socket
  1127. *
  1128. * Description:
  1129. * Set the CIPSO option on the given socket using the DOI definition and
  1130. * security attributes passed to the function. This function requires
  1131. * exclusive access to @sock->sk, which means it either needs to be in the
  1132. * process of being created or locked via lock_sock(sock->sk). Returns zero on
  1133. * success and negative values on failure.
  1134. *
  1135. */
  1136. int cipso_v4_socket_setattr(const struct socket *sock,
  1137. const struct cipso_v4_doi *doi_def,
  1138. const struct netlbl_lsm_secattr *secattr)
  1139. {
  1140. int ret_val = -EPERM;
  1141. u32 iter;
  1142. unsigned char *buf = NULL;
  1143. u32 buf_len = 0;
  1144. u32 opt_len;
  1145. struct ip_options *opt = NULL;
  1146. struct sock *sk;
  1147. struct inet_sock *sk_inet;
  1148. struct inet_connection_sock *sk_conn;
  1149. /* In the case of sock_create_lite(), the sock->sk field is not
  1150. * defined yet but it is not a problem as the only users of these
  1151. * "lite" PF_INET sockets are functions which do an accept() call
  1152. * afterwards so we will label the socket as part of the accept(). */
  1153. sk = sock->sk;
  1154. if (sk == NULL)
  1155. return 0;
  1156. /* XXX - This code assumes only one tag per CIPSO option which isn't
  1157. * really a good assumption to make but since we only support the MAC
  1158. * tags right now it is a safe assumption. */
  1159. iter = 0;
  1160. do {
  1161. switch (doi_def->tags[iter]) {
  1162. case CIPSO_V4_TAG_RBITMAP:
  1163. ret_val = cipso_v4_gentag_rbm(doi_def,
  1164. secattr,
  1165. &buf,
  1166. &buf_len);
  1167. break;
  1168. default:
  1169. ret_val = -EPERM;
  1170. goto socket_setattr_failure;
  1171. }
  1172. iter++;
  1173. } while (ret_val != 0 &&
  1174. iter < CIPSO_V4_TAG_MAXCNT &&
  1175. doi_def->tags[iter] != CIPSO_V4_TAG_INVALID);
  1176. if (ret_val != 0)
  1177. goto socket_setattr_failure;
  1178. /* We can't use ip_options_get() directly because it makes a call to
  1179. * ip_options_get_alloc() which allocates memory with GFP_KERNEL and
  1180. * we won't always have CAP_NET_RAW even though we _always_ want to
  1181. * set the IPOPT_CIPSO option. */
  1182. opt_len = (buf_len + 3) & ~3;
  1183. opt = kzalloc(sizeof(*opt) + opt_len, GFP_ATOMIC);
  1184. if (opt == NULL) {
  1185. ret_val = -ENOMEM;
  1186. goto socket_setattr_failure;
  1187. }
  1188. memcpy(opt->__data, buf, buf_len);
  1189. opt->optlen = opt_len;
  1190. opt->is_data = 1;
  1191. opt->cipso = sizeof(struct iphdr);
  1192. kfree(buf);
  1193. buf = NULL;
  1194. sk_inet = inet_sk(sk);
  1195. if (sk_inet->is_icsk) {
  1196. sk_conn = inet_csk(sk);
  1197. if (sk_inet->opt)
  1198. sk_conn->icsk_ext_hdr_len -= sk_inet->opt->optlen;
  1199. sk_conn->icsk_ext_hdr_len += opt->optlen;
  1200. sk_conn->icsk_sync_mss(sk, sk_conn->icsk_pmtu_cookie);
  1201. }
  1202. opt = xchg(&sk_inet->opt, opt);
  1203. kfree(opt);
  1204. return 0;
  1205. socket_setattr_failure:
  1206. kfree(buf);
  1207. kfree(opt);
  1208. return ret_val;
  1209. }
  1210. /**
  1211. * cipso_v4_sock_getattr - Get the security attributes from a sock
  1212. * @sk: the sock
  1213. * @secattr: the security attributes
  1214. *
  1215. * Description:
  1216. * Query @sk to see if there is a CIPSO option attached to the sock and if
  1217. * there is return the CIPSO security attributes in @secattr. This function
  1218. * requires that @sk be locked, or privately held, but it does not do any
  1219. * locking itself. Returns zero on success and negative values on failure.
  1220. *
  1221. */
  1222. int cipso_v4_sock_getattr(struct sock *sk, struct netlbl_lsm_secattr *secattr)
  1223. {
  1224. int ret_val = -ENOMSG;
  1225. struct inet_sock *sk_inet;
  1226. unsigned char *cipso_ptr;
  1227. u32 doi;
  1228. struct cipso_v4_doi *doi_def;
  1229. sk_inet = inet_sk(sk);
  1230. if (sk_inet->opt == NULL || sk_inet->opt->cipso == 0)
  1231. return -ENOMSG;
  1232. cipso_ptr = sk_inet->opt->__data + sk_inet->opt->cipso -
  1233. sizeof(struct iphdr);
  1234. ret_val = cipso_v4_cache_check(cipso_ptr, cipso_ptr[1], secattr);
  1235. if (ret_val == 0)
  1236. return ret_val;
  1237. doi = ntohl(*(__be32 *)&cipso_ptr[2]);
  1238. rcu_read_lock();
  1239. doi_def = cipso_v4_doi_getdef(doi);
  1240. if (doi_def == NULL) {
  1241. rcu_read_unlock();
  1242. return -ENOMSG;
  1243. }
  1244. switch (cipso_ptr[6]) {
  1245. case CIPSO_V4_TAG_RBITMAP:
  1246. ret_val = cipso_v4_parsetag_rbm(doi_def,
  1247. &cipso_ptr[6],
  1248. secattr);
  1249. break;
  1250. }
  1251. rcu_read_unlock();
  1252. return ret_val;
  1253. }
  1254. /**
  1255. * cipso_v4_socket_getattr - Get the security attributes from a socket
  1256. * @sock: the socket
  1257. * @secattr: the security attributes
  1258. *
  1259. * Description:
  1260. * Query @sock to see if there is a CIPSO option attached to the socket and if
  1261. * there is return the CIPSO security attributes in @secattr. Returns zero on
  1262. * success and negative values on failure.
  1263. *
  1264. */
  1265. int cipso_v4_socket_getattr(const struct socket *sock,
  1266. struct netlbl_lsm_secattr *secattr)
  1267. {
  1268. int ret_val;
  1269. lock_sock(sock->sk);
  1270. ret_val = cipso_v4_sock_getattr(sock->sk, secattr);
  1271. release_sock(sock->sk);
  1272. return ret_val;
  1273. }
  1274. /**
  1275. * cipso_v4_skbuff_getattr - Get the security attributes from the CIPSO option
  1276. * @skb: the packet
  1277. * @secattr: the security attributes
  1278. *
  1279. * Description:
  1280. * Parse the given packet's CIPSO option and return the security attributes.
  1281. * Returns zero on success and negative values on failure.
  1282. *
  1283. */
  1284. int cipso_v4_skbuff_getattr(const struct sk_buff *skb,
  1285. struct netlbl_lsm_secattr *secattr)
  1286. {
  1287. int ret_val = -ENOMSG;
  1288. unsigned char *cipso_ptr;
  1289. u32 doi;
  1290. struct cipso_v4_doi *doi_def;
  1291. if (!CIPSO_V4_OPTEXIST(skb))
  1292. return -ENOMSG;
  1293. cipso_ptr = CIPSO_V4_OPTPTR(skb);
  1294. if (cipso_v4_cache_check(cipso_ptr, cipso_ptr[1], secattr) == 0)
  1295. return 0;
  1296. doi = ntohl(*(__be32 *)&cipso_ptr[2]);
  1297. rcu_read_lock();
  1298. doi_def = cipso_v4_doi_getdef(doi);
  1299. if (doi_def == NULL)
  1300. goto skbuff_getattr_return;
  1301. switch (cipso_ptr[6]) {
  1302. case CIPSO_V4_TAG_RBITMAP:
  1303. ret_val = cipso_v4_parsetag_rbm(doi_def,
  1304. &cipso_ptr[6],
  1305. secattr);
  1306. break;
  1307. }
  1308. skbuff_getattr_return:
  1309. rcu_read_unlock();
  1310. return ret_val;
  1311. }
  1312. /*
  1313. * Setup Functions
  1314. */
  1315. /**
  1316. * cipso_v4_init - Initialize the CIPSO module
  1317. *
  1318. * Description:
  1319. * Initialize the CIPSO module and prepare it for use. Returns zero on success
  1320. * and negative values on failure.
  1321. *
  1322. */
  1323. static int __init cipso_v4_init(void)
  1324. {
  1325. int ret_val;
  1326. ret_val = cipso_v4_cache_init();
  1327. if (ret_val != 0)
  1328. panic("Failed to initialize the CIPSO/IPv4 cache (%d)\n",
  1329. ret_val);
  1330. return 0;
  1331. }
  1332. subsys_initcall(cipso_v4_init);