cipso_ipv4.c 45 KB

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