cipso_ipv4.c 40 KB

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