cipso_ipv4.c 40 KB

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