cipso_ipv4.c 39 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476
  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 = kmalloc(cipso_ptr_len, GFP_ATOMIC);
  343. if (entry->key == NULL) {
  344. ret_val = -ENOMEM;
  345. goto cache_add_failure;
  346. }
  347. memcpy(entry->key, cipso_ptr, cipso_ptr_len);
  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 = doi_def->map.std->cat.cipso_size;
  703. u32 *cipso_array = doi_def->map.std->cat.cipso;
  704. switch (doi_def->type) {
  705. case CIPSO_V4_MAP_PASS:
  706. return 0;
  707. case CIPSO_V4_MAP_STD:
  708. for (;;) {
  709. cat = cipso_v4_bitmap_walk(bitmap,
  710. bitmap_len_bits,
  711. cat + 1,
  712. 1);
  713. if (cat < 0)
  714. break;
  715. if (cat >= cipso_cat_size ||
  716. cipso_array[cat] >= CIPSO_V4_INV_CAT)
  717. return -EFAULT;
  718. }
  719. if (cat == -1)
  720. return 0;
  721. break;
  722. }
  723. return -EFAULT;
  724. }
  725. /**
  726. * cipso_v4_map_cat_rbm_hton - Perform a category mapping from host to network
  727. * @doi_def: the DOI definition
  728. * @host_cat: the category bitmap in host format
  729. * @host_cat_len: the length of the host's category bitmap in bytes
  730. * @net_cat: the zero'd out category bitmap in network/CIPSO format
  731. * @net_cat_len: the length of the CIPSO bitmap in bytes
  732. *
  733. * Description:
  734. * Perform a label mapping to translate a local MLS category bitmap to the
  735. * correct CIPSO bitmap using the given DOI definition. Returns the minimum
  736. * size in bytes of the network bitmap on success, negative values otherwise.
  737. *
  738. */
  739. static int cipso_v4_map_cat_rbm_hton(const struct cipso_v4_doi *doi_def,
  740. const unsigned char *host_cat,
  741. u32 host_cat_len,
  742. unsigned char *net_cat,
  743. u32 net_cat_len)
  744. {
  745. int host_spot = -1;
  746. u32 net_spot;
  747. u32 net_spot_max = 0;
  748. u32 host_clen_bits = host_cat_len * 8;
  749. u32 net_clen_bits = net_cat_len * 8;
  750. u32 host_cat_size = doi_def->map.std->cat.local_size;
  751. u32 *host_cat_array = doi_def->map.std->cat.local;
  752. switch (doi_def->type) {
  753. case CIPSO_V4_MAP_PASS:
  754. net_spot_max = host_cat_len - 1;
  755. while (net_spot_max > 0 && host_cat[net_spot_max] == 0)
  756. net_spot_max--;
  757. if (net_spot_max > net_cat_len)
  758. return -EINVAL;
  759. memcpy(net_cat, host_cat, net_spot_max);
  760. return net_spot_max;
  761. case CIPSO_V4_MAP_STD:
  762. for (;;) {
  763. host_spot = cipso_v4_bitmap_walk(host_cat,
  764. host_clen_bits,
  765. host_spot + 1,
  766. 1);
  767. if (host_spot < 0)
  768. break;
  769. if (host_spot >= host_cat_size)
  770. return -EPERM;
  771. net_spot = host_cat_array[host_spot];
  772. if (net_spot >= net_clen_bits)
  773. return -ENOSPC;
  774. cipso_v4_bitmap_setbit(net_cat, net_spot, 1);
  775. if (net_spot > net_spot_max)
  776. net_spot_max = net_spot;
  777. }
  778. if (host_spot == -2)
  779. return -EFAULT;
  780. if (++net_spot_max % 8)
  781. return net_spot_max / 8 + 1;
  782. return net_spot_max / 8;
  783. }
  784. return -EINVAL;
  785. }
  786. /**
  787. * cipso_v4_map_cat_rbm_ntoh - Perform a category mapping from network to host
  788. * @doi_def: the DOI definition
  789. * @net_cat: the category bitmap in network/CIPSO format
  790. * @net_cat_len: the length of the CIPSO bitmap in bytes
  791. * @host_cat: the zero'd out category bitmap in host format
  792. * @host_cat_len: the length of the host's category bitmap in bytes
  793. *
  794. * Description:
  795. * Perform a label mapping to translate a CIPSO bitmap to the correct local
  796. * MLS category bitmap using the given DOI definition. Returns the minimum
  797. * size in bytes of the host bitmap on success, negative values otherwise.
  798. *
  799. */
  800. static int cipso_v4_map_cat_rbm_ntoh(const struct cipso_v4_doi *doi_def,
  801. const unsigned char *net_cat,
  802. u32 net_cat_len,
  803. unsigned char *host_cat,
  804. u32 host_cat_len)
  805. {
  806. u32 host_spot;
  807. u32 host_spot_max = 0;
  808. int net_spot = -1;
  809. u32 net_clen_bits = net_cat_len * 8;
  810. u32 host_clen_bits = host_cat_len * 8;
  811. u32 net_cat_size = doi_def->map.std->cat.cipso_size;
  812. u32 *net_cat_array = doi_def->map.std->cat.cipso;
  813. switch (doi_def->type) {
  814. case CIPSO_V4_MAP_PASS:
  815. if (net_cat_len > host_cat_len)
  816. return -EINVAL;
  817. memcpy(host_cat, net_cat, net_cat_len);
  818. return net_cat_len;
  819. case CIPSO_V4_MAP_STD:
  820. for (;;) {
  821. net_spot = cipso_v4_bitmap_walk(net_cat,
  822. net_clen_bits,
  823. net_spot + 1,
  824. 1);
  825. if (net_spot < 0)
  826. break;
  827. if (net_spot >= net_cat_size ||
  828. net_cat_array[net_spot] >= CIPSO_V4_INV_CAT)
  829. return -EPERM;
  830. host_spot = net_cat_array[net_spot];
  831. if (host_spot >= host_clen_bits)
  832. return -ENOSPC;
  833. cipso_v4_bitmap_setbit(host_cat, host_spot, 1);
  834. if (host_spot > host_spot_max)
  835. host_spot_max = host_spot;
  836. }
  837. if (net_spot == -2)
  838. return -EFAULT;
  839. if (++host_spot_max % 8)
  840. return host_spot_max / 8 + 1;
  841. return host_spot_max / 8;
  842. }
  843. return -EINVAL;
  844. }
  845. /*
  846. * Protocol Handling Functions
  847. */
  848. #define CIPSO_V4_HDR_LEN 6
  849. /**
  850. * cipso_v4_gentag_hdr - Generate a CIPSO option header
  851. * @doi_def: the DOI definition
  852. * @len: the total tag length in bytes
  853. * @buf: the CIPSO option buffer
  854. *
  855. * Description:
  856. * Write a CIPSO header into the beginning of @buffer. Return zero on success,
  857. * negative values on failure.
  858. *
  859. */
  860. static int cipso_v4_gentag_hdr(const struct cipso_v4_doi *doi_def,
  861. u32 len,
  862. unsigned char *buf)
  863. {
  864. if (CIPSO_V4_HDR_LEN + len > 40)
  865. return -ENOSPC;
  866. buf[0] = IPOPT_CIPSO;
  867. buf[1] = CIPSO_V4_HDR_LEN + len;
  868. *(u32 *)&buf[2] = htonl(doi_def->doi);
  869. return 0;
  870. }
  871. #define CIPSO_V4_TAG1_CAT_LEN 30
  872. /**
  873. * cipso_v4_gentag_rbm - Generate a CIPSO restricted bitmap tag (type #1)
  874. * @doi_def: the DOI definition
  875. * @secattr: the security attributes
  876. * @buffer: the option buffer
  877. * @buffer_len: length of buffer in bytes
  878. *
  879. * Description:
  880. * Generate a CIPSO option using the restricted bitmap tag, tag type #1. The
  881. * actual buffer length may be larger than the indicated size due to
  882. * translation between host and network category bitmaps. Returns zero on
  883. * success, negative values on failure.
  884. *
  885. */
  886. static int cipso_v4_gentag_rbm(const struct cipso_v4_doi *doi_def,
  887. const struct netlbl_lsm_secattr *secattr,
  888. unsigned char **buffer,
  889. u32 *buffer_len)
  890. {
  891. int ret_val = -EPERM;
  892. unsigned char *buf = NULL;
  893. u32 buf_len;
  894. u32 level;
  895. if (secattr->mls_cat) {
  896. buf = kzalloc(CIPSO_V4_HDR_LEN + 4 + CIPSO_V4_TAG1_CAT_LEN,
  897. GFP_ATOMIC);
  898. if (buf == NULL)
  899. return -ENOMEM;
  900. ret_val = cipso_v4_map_cat_rbm_hton(doi_def,
  901. secattr->mls_cat,
  902. secattr->mls_cat_len,
  903. &buf[CIPSO_V4_HDR_LEN + 4],
  904. CIPSO_V4_TAG1_CAT_LEN);
  905. if (ret_val < 0)
  906. goto gentag_failure;
  907. /* This will send packets using the "optimized" format when
  908. * possibile as specified in section 3.4.2.6 of the
  909. * CIPSO draft. */
  910. if (cipso_v4_rbm_optfmt && (ret_val > 0 && ret_val < 10))
  911. ret_val = 10;
  912. buf_len = 4 + ret_val;
  913. } else {
  914. buf = kzalloc(CIPSO_V4_HDR_LEN + 4, GFP_ATOMIC);
  915. if (buf == NULL)
  916. return -ENOMEM;
  917. buf_len = 4;
  918. }
  919. ret_val = cipso_v4_map_lvl_hton(doi_def, secattr->mls_lvl, &level);
  920. if (ret_val != 0)
  921. goto gentag_failure;
  922. ret_val = cipso_v4_gentag_hdr(doi_def, buf_len, buf);
  923. if (ret_val != 0)
  924. goto gentag_failure;
  925. buf[CIPSO_V4_HDR_LEN] = 0x01;
  926. buf[CIPSO_V4_HDR_LEN + 1] = buf_len;
  927. buf[CIPSO_V4_HDR_LEN + 3] = level;
  928. *buffer = buf;
  929. *buffer_len = CIPSO_V4_HDR_LEN + buf_len;
  930. return 0;
  931. gentag_failure:
  932. kfree(buf);
  933. return ret_val;
  934. }
  935. /**
  936. * cipso_v4_parsetag_rbm - Parse a CIPSO restricted bitmap tag
  937. * @doi_def: the DOI definition
  938. * @tag: the CIPSO tag
  939. * @secattr: the security attributes
  940. *
  941. * Description:
  942. * Parse a CIPSO restricted bitmap tag (tag type #1) and return the security
  943. * attributes in @secattr. Return zero on success, negatives values on
  944. * failure.
  945. *
  946. */
  947. static int cipso_v4_parsetag_rbm(const struct cipso_v4_doi *doi_def,
  948. const unsigned char *tag,
  949. struct netlbl_lsm_secattr *secattr)
  950. {
  951. int ret_val;
  952. u8 tag_len = tag[1];
  953. u32 level;
  954. ret_val = cipso_v4_map_lvl_ntoh(doi_def, tag[3], &level);
  955. if (ret_val != 0)
  956. return ret_val;
  957. secattr->mls_lvl = level;
  958. secattr->mls_lvl_vld = 1;
  959. if (tag_len > 4) {
  960. switch (doi_def->type) {
  961. case CIPSO_V4_MAP_PASS:
  962. secattr->mls_cat_len = tag_len - 4;
  963. break;
  964. case CIPSO_V4_MAP_STD:
  965. secattr->mls_cat_len =
  966. doi_def->map.std->cat.local_size;
  967. break;
  968. }
  969. secattr->mls_cat = kzalloc(secattr->mls_cat_len, GFP_ATOMIC);
  970. if (secattr->mls_cat == NULL)
  971. return -ENOMEM;
  972. ret_val = cipso_v4_map_cat_rbm_ntoh(doi_def,
  973. &tag[4],
  974. tag_len - 4,
  975. secattr->mls_cat,
  976. secattr->mls_cat_len);
  977. if (ret_val < 0) {
  978. kfree(secattr->mls_cat);
  979. return ret_val;
  980. }
  981. secattr->mls_cat_len = ret_val;
  982. }
  983. return 0;
  984. }
  985. /**
  986. * cipso_v4_validate - Validate a CIPSO option
  987. * @option: the start of the option, on error it is set to point to the error
  988. *
  989. * Description:
  990. * This routine is called to validate a CIPSO option, it checks all of the
  991. * fields to ensure that they are at least valid, see the draft snippet below
  992. * for details. If the option is valid then a zero value is returned and
  993. * the value of @option is unchanged. If the option is invalid then a
  994. * non-zero value is returned and @option is adjusted to point to the
  995. * offending portion of the option. From the IETF draft ...
  996. *
  997. * "If any field within the CIPSO options, such as the DOI identifier, is not
  998. * recognized the IP datagram is discarded and an ICMP 'parameter problem'
  999. * (type 12) is generated and returned. The ICMP code field is set to 'bad
  1000. * parameter' (code 0) and the pointer is set to the start of the CIPSO field
  1001. * that is unrecognized."
  1002. *
  1003. */
  1004. int cipso_v4_validate(unsigned char **option)
  1005. {
  1006. unsigned char *opt = *option;
  1007. unsigned char *tag;
  1008. unsigned char opt_iter;
  1009. unsigned char err_offset = 0;
  1010. u8 opt_len;
  1011. u8 tag_len;
  1012. struct cipso_v4_doi *doi_def = NULL;
  1013. u32 tag_iter;
  1014. /* caller already checks for length values that are too large */
  1015. opt_len = opt[1];
  1016. if (opt_len < 8) {
  1017. err_offset = 1;
  1018. goto validate_return;
  1019. }
  1020. rcu_read_lock();
  1021. doi_def = cipso_v4_doi_getdef(ntohl(*((u32 *)&opt[2])));
  1022. if (doi_def == NULL) {
  1023. err_offset = 2;
  1024. goto validate_return_locked;
  1025. }
  1026. opt_iter = 6;
  1027. tag = opt + opt_iter;
  1028. while (opt_iter < opt_len) {
  1029. for (tag_iter = 0; doi_def->tags[tag_iter] != tag[0];)
  1030. if (doi_def->tags[tag_iter] == CIPSO_V4_TAG_INVALID ||
  1031. ++tag_iter == CIPSO_V4_TAG_MAXCNT) {
  1032. err_offset = opt_iter;
  1033. goto validate_return_locked;
  1034. }
  1035. tag_len = tag[1];
  1036. if (tag_len > (opt_len - opt_iter)) {
  1037. err_offset = opt_iter + 1;
  1038. goto validate_return_locked;
  1039. }
  1040. switch (tag[0]) {
  1041. case CIPSO_V4_TAG_RBITMAP:
  1042. if (tag_len < 4) {
  1043. err_offset = opt_iter + 1;
  1044. goto validate_return_locked;
  1045. }
  1046. /* We are already going to do all the verification
  1047. * necessary at the socket layer so from our point of
  1048. * view it is safe to turn these checks off (and less
  1049. * work), however, the CIPSO draft says we should do
  1050. * all the CIPSO validations here but it doesn't
  1051. * really specify _exactly_ what we need to validate
  1052. * ... so, just make it a sysctl tunable. */
  1053. if (cipso_v4_rbm_strictvalid) {
  1054. if (cipso_v4_map_lvl_valid(doi_def,
  1055. tag[3]) < 0) {
  1056. err_offset = opt_iter + 3;
  1057. goto validate_return_locked;
  1058. }
  1059. if (tag_len > 4 &&
  1060. cipso_v4_map_cat_rbm_valid(doi_def,
  1061. &tag[4],
  1062. tag_len - 4) < 0) {
  1063. err_offset = opt_iter + 4;
  1064. goto validate_return_locked;
  1065. }
  1066. }
  1067. break;
  1068. default:
  1069. err_offset = opt_iter;
  1070. goto validate_return_locked;
  1071. }
  1072. tag += tag_len;
  1073. opt_iter += tag_len;
  1074. }
  1075. validate_return_locked:
  1076. rcu_read_unlock();
  1077. validate_return:
  1078. *option = opt + err_offset;
  1079. return err_offset;
  1080. }
  1081. /**
  1082. * cipso_v4_error - Send the correct reponse for a bad packet
  1083. * @skb: the packet
  1084. * @error: the error code
  1085. * @gateway: CIPSO gateway flag
  1086. *
  1087. * Description:
  1088. * Based on the error code given in @error, send an ICMP error message back to
  1089. * the originating host. From the IETF draft ...
  1090. *
  1091. * "If the contents of the CIPSO [option] are valid but the security label is
  1092. * outside of the configured host or port label range, the datagram is
  1093. * discarded and an ICMP 'destination unreachable' (type 3) is generated and
  1094. * returned. The code field of the ICMP is set to 'communication with
  1095. * destination network administratively prohibited' (code 9) or to
  1096. * 'communication with destination host administratively prohibited'
  1097. * (code 10). The value of the code is dependent on whether the originator
  1098. * of the ICMP message is acting as a CIPSO host or a CIPSO gateway. The
  1099. * recipient of the ICMP message MUST be able to handle either value. The
  1100. * same procedure is performed if a CIPSO [option] can not be added to an
  1101. * IP packet because it is too large to fit in the IP options area."
  1102. *
  1103. * "If the error is triggered by receipt of an ICMP message, the message is
  1104. * discarded and no response is permitted (consistent with general ICMP
  1105. * processing rules)."
  1106. *
  1107. */
  1108. void cipso_v4_error(struct sk_buff *skb, int error, u32 gateway)
  1109. {
  1110. if (skb->nh.iph->protocol == IPPROTO_ICMP || error != -EACCES)
  1111. return;
  1112. if (gateway)
  1113. icmp_send(skb, ICMP_DEST_UNREACH, ICMP_NET_ANO, 0);
  1114. else
  1115. icmp_send(skb, ICMP_DEST_UNREACH, ICMP_HOST_ANO, 0);
  1116. }
  1117. /**
  1118. * cipso_v4_socket_setattr - Add a CIPSO option to a socket
  1119. * @sock: the socket
  1120. * @doi_def: the CIPSO DOI to use
  1121. * @secattr: the specific security attributes of the socket
  1122. *
  1123. * Description:
  1124. * Set the CIPSO option on the given socket using the DOI definition and
  1125. * security attributes passed to the function. This function requires
  1126. * exclusive access to @sock->sk, which means it either needs to be in the
  1127. * process of being created or locked via lock_sock(sock->sk). Returns zero on
  1128. * success and negative values on failure.
  1129. *
  1130. */
  1131. int cipso_v4_socket_setattr(const struct socket *sock,
  1132. const struct cipso_v4_doi *doi_def,
  1133. const struct netlbl_lsm_secattr *secattr)
  1134. {
  1135. int ret_val = -EPERM;
  1136. u32 iter;
  1137. unsigned char *buf = NULL;
  1138. u32 buf_len = 0;
  1139. u32 opt_len;
  1140. struct ip_options *opt = NULL;
  1141. struct sock *sk;
  1142. struct inet_sock *sk_inet;
  1143. struct inet_connection_sock *sk_conn;
  1144. /* In the case of sock_create_lite(), the sock->sk field is not
  1145. * defined yet but it is not a problem as the only users of these
  1146. * "lite" PF_INET sockets are functions which do an accept() call
  1147. * afterwards so we will label the socket as part of the accept(). */
  1148. sk = sock->sk;
  1149. if (sk == NULL)
  1150. return 0;
  1151. /* XXX - This code assumes only one tag per CIPSO option which isn't
  1152. * really a good assumption to make but since we only support the MAC
  1153. * tags right now it is a safe assumption. */
  1154. iter = 0;
  1155. do {
  1156. switch (doi_def->tags[iter]) {
  1157. case CIPSO_V4_TAG_RBITMAP:
  1158. ret_val = cipso_v4_gentag_rbm(doi_def,
  1159. secattr,
  1160. &buf,
  1161. &buf_len);
  1162. break;
  1163. default:
  1164. ret_val = -EPERM;
  1165. goto socket_setattr_failure;
  1166. }
  1167. iter++;
  1168. } while (ret_val != 0 &&
  1169. iter < CIPSO_V4_TAG_MAXCNT &&
  1170. doi_def->tags[iter] != CIPSO_V4_TAG_INVALID);
  1171. if (ret_val != 0)
  1172. goto socket_setattr_failure;
  1173. /* We can't use ip_options_get() directly because it makes a call to
  1174. * ip_options_get_alloc() which allocates memory with GFP_KERNEL and
  1175. * we can't block here. */
  1176. opt_len = (buf_len + 3) & ~3;
  1177. opt = kzalloc(sizeof(*opt) + opt_len, GFP_ATOMIC);
  1178. if (opt == NULL) {
  1179. ret_val = -ENOMEM;
  1180. goto socket_setattr_failure;
  1181. }
  1182. memcpy(opt->__data, buf, buf_len);
  1183. opt->optlen = opt_len;
  1184. opt->is_data = 1;
  1185. kfree(buf);
  1186. buf = NULL;
  1187. ret_val = ip_options_compile(opt, NULL);
  1188. if (ret_val != 0)
  1189. goto socket_setattr_failure;
  1190. sk_inet = inet_sk(sk);
  1191. if (sk_inet->is_icsk) {
  1192. sk_conn = inet_csk(sk);
  1193. if (sk_inet->opt)
  1194. sk_conn->icsk_ext_hdr_len -= sk_inet->opt->optlen;
  1195. sk_conn->icsk_ext_hdr_len += opt->optlen;
  1196. sk_conn->icsk_sync_mss(sk, sk_conn->icsk_pmtu_cookie);
  1197. }
  1198. opt = xchg(&sk_inet->opt, opt);
  1199. kfree(opt);
  1200. return 0;
  1201. socket_setattr_failure:
  1202. kfree(buf);
  1203. kfree(opt);
  1204. return ret_val;
  1205. }
  1206. /**
  1207. * cipso_v4_sock_getattr - Get the security attributes from a sock
  1208. * @sk: the sock
  1209. * @secattr: the security attributes
  1210. *
  1211. * Description:
  1212. * Query @sk to see if there is a CIPSO option attached to the sock and if
  1213. * there is return the CIPSO security attributes in @secattr. This function
  1214. * requires that @sk be locked, or privately held, but it does not do any
  1215. * locking itself. Returns zero on success and negative values on failure.
  1216. *
  1217. */
  1218. int cipso_v4_sock_getattr(struct sock *sk, struct netlbl_lsm_secattr *secattr)
  1219. {
  1220. int ret_val = -ENOMSG;
  1221. struct inet_sock *sk_inet;
  1222. unsigned char *cipso_ptr;
  1223. u32 doi;
  1224. struct cipso_v4_doi *doi_def;
  1225. sk_inet = inet_sk(sk);
  1226. if (sk_inet->opt == NULL || sk_inet->opt->cipso == 0)
  1227. return -ENOMSG;
  1228. cipso_ptr = sk_inet->opt->__data + sk_inet->opt->cipso -
  1229. sizeof(struct iphdr);
  1230. ret_val = cipso_v4_cache_check(cipso_ptr, cipso_ptr[1], secattr);
  1231. if (ret_val == 0)
  1232. return ret_val;
  1233. doi = ntohl(*(u32 *)&cipso_ptr[2]);
  1234. rcu_read_lock();
  1235. doi_def = cipso_v4_doi_getdef(doi);
  1236. if (doi_def == NULL) {
  1237. rcu_read_unlock();
  1238. return -ENOMSG;
  1239. }
  1240. switch (cipso_ptr[6]) {
  1241. case CIPSO_V4_TAG_RBITMAP:
  1242. ret_val = cipso_v4_parsetag_rbm(doi_def,
  1243. &cipso_ptr[6],
  1244. secattr);
  1245. break;
  1246. }
  1247. rcu_read_unlock();
  1248. return ret_val;
  1249. }
  1250. /**
  1251. * cipso_v4_socket_getattr - Get the security attributes from a socket
  1252. * @sock: the socket
  1253. * @secattr: the security attributes
  1254. *
  1255. * Description:
  1256. * Query @sock to see if there is a CIPSO option attached to the socket and if
  1257. * there is return the CIPSO security attributes in @secattr. Returns zero on
  1258. * success and negative values on failure.
  1259. *
  1260. */
  1261. int cipso_v4_socket_getattr(const struct socket *sock,
  1262. struct netlbl_lsm_secattr *secattr)
  1263. {
  1264. int ret_val;
  1265. lock_sock(sock->sk);
  1266. ret_val = cipso_v4_sock_getattr(sock->sk, secattr);
  1267. release_sock(sock->sk);
  1268. return ret_val;
  1269. }
  1270. /**
  1271. * cipso_v4_skbuff_getattr - Get the security attributes from the CIPSO option
  1272. * @skb: the packet
  1273. * @secattr: the security attributes
  1274. *
  1275. * Description:
  1276. * Parse the given packet's CIPSO option and return the security attributes.
  1277. * Returns zero on success and negative values on failure.
  1278. *
  1279. */
  1280. int cipso_v4_skbuff_getattr(const struct sk_buff *skb,
  1281. struct netlbl_lsm_secattr *secattr)
  1282. {
  1283. int ret_val = -ENOMSG;
  1284. unsigned char *cipso_ptr;
  1285. u32 doi;
  1286. struct cipso_v4_doi *doi_def;
  1287. if (!CIPSO_V4_OPTEXIST(skb))
  1288. return -ENOMSG;
  1289. cipso_ptr = CIPSO_V4_OPTPTR(skb);
  1290. if (cipso_v4_cache_check(cipso_ptr, cipso_ptr[1], secattr) == 0)
  1291. return 0;
  1292. doi = ntohl(*(u32 *)&cipso_ptr[2]);
  1293. rcu_read_lock();
  1294. doi_def = cipso_v4_doi_getdef(doi);
  1295. if (doi_def == NULL)
  1296. goto skbuff_getattr_return;
  1297. switch (cipso_ptr[6]) {
  1298. case CIPSO_V4_TAG_RBITMAP:
  1299. ret_val = cipso_v4_parsetag_rbm(doi_def,
  1300. &cipso_ptr[6],
  1301. secattr);
  1302. break;
  1303. }
  1304. skbuff_getattr_return:
  1305. rcu_read_unlock();
  1306. return ret_val;
  1307. }
  1308. /*
  1309. * Setup Functions
  1310. */
  1311. /**
  1312. * cipso_v4_init - Initialize the CIPSO module
  1313. *
  1314. * Description:
  1315. * Initialize the CIPSO module and prepare it for use. Returns zero on success
  1316. * and negative values on failure.
  1317. *
  1318. */
  1319. static int __init cipso_v4_init(void)
  1320. {
  1321. int ret_val;
  1322. ret_val = cipso_v4_cache_init();
  1323. if (ret_val != 0)
  1324. panic("Failed to initialize the CIPSO/IPv4 cache (%d)\n",
  1325. ret_val);
  1326. return 0;
  1327. }
  1328. subsys_initcall(cipso_v4_init);