cipso_ipv4.c 40 KB

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