cipso_ipv4.c 53 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617161816191620162116221623162416251626162716281629163016311632163316341635163616371638163916401641164216431644164516461647164816491650165116521653165416551656165716581659166016611662166316641665166616671668166916701671167216731674167516761677167816791680168116821683168416851686168716881689169016911692169316941695169616971698169917001701170217031704170517061707170817091710171117121713171417151716171717181719172017211722172317241725172617271728172917301731173217331734173517361737173817391740174117421743174417451746174717481749175017511752175317541755175617571758175917601761176217631764176517661767176817691770177117721773177417751776177717781779178017811782178317841785178617871788178917901791179217931794179517961797179817991800180118021803180418051806180718081809181018111812181318141815181618171818181918201821182218231824182518261827182818291830183118321833183418351836183718381839184018411842184318441845184618471848184918501851185218531854185518561857185818591860186118621863186418651866186718681869187018711872187318741875187618771878187918801881188218831884188518861887188818891890189118921893189418951896189718981899190019011902190319041905190619071908190919101911191219131914191519161917191819191920192119221923192419251926192719281929193019311932193319341935193619371938193919401941194219431944194519461947194819491950195119521953195419551956195719581959196019611962196319641965196619671968196919701971197219731974197519761977197819791980198119821983198419851986198719881989199019911992199319941995
  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. * Protocol Constants
  87. */
  88. /* Maximum size of the CIPSO IP option, derived from the fact that the maximum
  89. * IPv4 header size is 60 bytes and the base IPv4 header is 20 bytes long. */
  90. #define CIPSO_V4_OPT_LEN_MAX 40
  91. /* Length of the base CIPSO option, this includes the option type (1 byte), the
  92. * option length (1 byte), and the DOI (4 bytes). */
  93. #define CIPSO_V4_HDR_LEN 6
  94. /* Base length of the restrictive category bitmap tag (tag #1). */
  95. #define CIPSO_V4_TAG_RBM_BLEN 4
  96. /* Base length of the enumerated category tag (tag #2). */
  97. #define CIPSO_V4_TAG_ENUM_BLEN 4
  98. /* Base length of the ranged categories bitmap tag (tag #5). */
  99. #define CIPSO_V4_TAG_RNG_BLEN 4
  100. /* The maximum number of category ranges permitted in the ranged category tag
  101. * (tag #5). You may note that the IETF draft states that the maximum number
  102. * of category ranges is 7, but if the low end of the last category range is
  103. * zero then it is possibile to fit 8 category ranges because the zero should
  104. * be omitted. */
  105. #define CIPSO_V4_TAG_RNG_CAT_MAX 8
  106. /*
  107. * Helper Functions
  108. */
  109. /**
  110. * cipso_v4_bitmap_walk - Walk a bitmap looking for a bit
  111. * @bitmap: the bitmap
  112. * @bitmap_len: length in bits
  113. * @offset: starting offset
  114. * @state: if non-zero, look for a set (1) bit else look for a cleared (0) bit
  115. *
  116. * Description:
  117. * Starting at @offset, walk the bitmap from left to right until either the
  118. * desired bit is found or we reach the end. Return the bit offset, -1 if
  119. * not found, or -2 if error.
  120. */
  121. static int cipso_v4_bitmap_walk(const unsigned char *bitmap,
  122. u32 bitmap_len,
  123. u32 offset,
  124. u8 state)
  125. {
  126. u32 bit_spot;
  127. u32 byte_offset;
  128. unsigned char bitmask;
  129. unsigned char byte;
  130. /* gcc always rounds to zero when doing integer division */
  131. byte_offset = offset / 8;
  132. byte = bitmap[byte_offset];
  133. bit_spot = offset;
  134. bitmask = 0x80 >> (offset % 8);
  135. while (bit_spot < bitmap_len) {
  136. if ((state && (byte & bitmask) == bitmask) ||
  137. (state == 0 && (byte & bitmask) == 0))
  138. return bit_spot;
  139. bit_spot++;
  140. bitmask >>= 1;
  141. if (bitmask == 0) {
  142. byte = bitmap[++byte_offset];
  143. bitmask = 0x80;
  144. }
  145. }
  146. return -1;
  147. }
  148. /**
  149. * cipso_v4_bitmap_setbit - Sets a single bit in a bitmap
  150. * @bitmap: the bitmap
  151. * @bit: the bit
  152. * @state: if non-zero, set the bit (1) else clear the bit (0)
  153. *
  154. * Description:
  155. * Set a single bit in the bitmask. Returns zero on success, negative values
  156. * on error.
  157. */
  158. static void cipso_v4_bitmap_setbit(unsigned char *bitmap,
  159. u32 bit,
  160. u8 state)
  161. {
  162. u32 byte_spot;
  163. u8 bitmask;
  164. /* gcc always rounds to zero when doing integer division */
  165. byte_spot = bit / 8;
  166. bitmask = 0x80 >> (bit % 8);
  167. if (state)
  168. bitmap[byte_spot] |= bitmask;
  169. else
  170. bitmap[byte_spot] &= ~bitmask;
  171. }
  172. /**
  173. * cipso_v4_doi_domhsh_free - Frees a domain list entry
  174. * @entry: the entry's RCU field
  175. *
  176. * Description:
  177. * This function is designed to be used as a callback to the call_rcu()
  178. * function so that the memory allocated to a domain list entry can be released
  179. * safely.
  180. *
  181. */
  182. static void cipso_v4_doi_domhsh_free(struct rcu_head *entry)
  183. {
  184. struct cipso_v4_domhsh_entry *ptr;
  185. ptr = container_of(entry, struct cipso_v4_domhsh_entry, rcu);
  186. kfree(ptr->domain);
  187. kfree(ptr);
  188. }
  189. /**
  190. * cipso_v4_cache_entry_free - Frees a cache entry
  191. * @entry: the entry to free
  192. *
  193. * Description:
  194. * This function frees the memory associated with a cache entry including the
  195. * LSM cache data if there are no longer any users, i.e. reference count == 0.
  196. *
  197. */
  198. static void cipso_v4_cache_entry_free(struct cipso_v4_map_cache_entry *entry)
  199. {
  200. if (entry->lsm_data)
  201. netlbl_secattr_cache_free(entry->lsm_data);
  202. kfree(entry->key);
  203. kfree(entry);
  204. }
  205. /**
  206. * cipso_v4_map_cache_hash - Hashing function for the CIPSO cache
  207. * @key: the hash key
  208. * @key_len: the length of the key in bytes
  209. *
  210. * Description:
  211. * The CIPSO tag hashing function. Returns a 32-bit hash value.
  212. *
  213. */
  214. static u32 cipso_v4_map_cache_hash(const unsigned char *key, u32 key_len)
  215. {
  216. return jhash(key, key_len, 0);
  217. }
  218. /*
  219. * Label Mapping Cache Functions
  220. */
  221. /**
  222. * cipso_v4_cache_init - Initialize the CIPSO cache
  223. *
  224. * Description:
  225. * Initializes the CIPSO label mapping cache, this function should be called
  226. * before any of the other functions defined in this file. Returns zero on
  227. * success, negative values on error.
  228. *
  229. */
  230. static int cipso_v4_cache_init(void)
  231. {
  232. u32 iter;
  233. cipso_v4_cache = kcalloc(CIPSO_V4_CACHE_BUCKETS,
  234. sizeof(struct cipso_v4_map_cache_bkt),
  235. GFP_KERNEL);
  236. if (cipso_v4_cache == NULL)
  237. return -ENOMEM;
  238. for (iter = 0; iter < CIPSO_V4_CACHE_BUCKETS; iter++) {
  239. spin_lock_init(&cipso_v4_cache[iter].lock);
  240. cipso_v4_cache[iter].size = 0;
  241. INIT_LIST_HEAD(&cipso_v4_cache[iter].list);
  242. }
  243. return 0;
  244. }
  245. /**
  246. * cipso_v4_cache_invalidate - Invalidates the current CIPSO cache
  247. *
  248. * Description:
  249. * Invalidates and frees any entries in the CIPSO cache. Returns zero on
  250. * success and negative values on failure.
  251. *
  252. */
  253. void cipso_v4_cache_invalidate(void)
  254. {
  255. struct cipso_v4_map_cache_entry *entry, *tmp_entry;
  256. u32 iter;
  257. for (iter = 0; iter < CIPSO_V4_CACHE_BUCKETS; iter++) {
  258. spin_lock_bh(&cipso_v4_cache[iter].lock);
  259. list_for_each_entry_safe(entry,
  260. tmp_entry,
  261. &cipso_v4_cache[iter].list, list) {
  262. list_del(&entry->list);
  263. cipso_v4_cache_entry_free(entry);
  264. }
  265. cipso_v4_cache[iter].size = 0;
  266. spin_unlock_bh(&cipso_v4_cache[iter].lock);
  267. }
  268. return;
  269. }
  270. /**
  271. * cipso_v4_cache_check - Check the CIPSO cache for a label mapping
  272. * @key: the buffer to check
  273. * @key_len: buffer length in bytes
  274. * @secattr: the security attribute struct to use
  275. *
  276. * Description:
  277. * This function checks the cache to see if a label mapping already exists for
  278. * the given key. If there is a match then the cache is adjusted and the
  279. * @secattr struct is populated with the correct LSM security attributes. The
  280. * cache is adjusted in the following manner if the entry is not already the
  281. * first in the cache bucket:
  282. *
  283. * 1. The cache entry's activity counter is incremented
  284. * 2. The previous (higher ranking) entry's activity counter is decremented
  285. * 3. If the difference between the two activity counters is geater than
  286. * CIPSO_V4_CACHE_REORDERLIMIT the two entries are swapped
  287. *
  288. * Returns zero on success, -ENOENT for a cache miss, and other negative values
  289. * on error.
  290. *
  291. */
  292. static int cipso_v4_cache_check(const unsigned char *key,
  293. u32 key_len,
  294. struct netlbl_lsm_secattr *secattr)
  295. {
  296. u32 bkt;
  297. struct cipso_v4_map_cache_entry *entry;
  298. struct cipso_v4_map_cache_entry *prev_entry = NULL;
  299. u32 hash;
  300. if (!cipso_v4_cache_enabled)
  301. return -ENOENT;
  302. hash = cipso_v4_map_cache_hash(key, key_len);
  303. bkt = hash & (CIPSO_V4_CACHE_BUCKETBITS - 1);
  304. spin_lock_bh(&cipso_v4_cache[bkt].lock);
  305. list_for_each_entry(entry, &cipso_v4_cache[bkt].list, list) {
  306. if (entry->hash == hash &&
  307. entry->key_len == key_len &&
  308. memcmp(entry->key, key, key_len) == 0) {
  309. entry->activity += 1;
  310. atomic_inc(&entry->lsm_data->refcount);
  311. secattr->cache = entry->lsm_data;
  312. secattr->flags |= NETLBL_SECATTR_CACHE;
  313. if (prev_entry == NULL) {
  314. spin_unlock_bh(&cipso_v4_cache[bkt].lock);
  315. return 0;
  316. }
  317. if (prev_entry->activity > 0)
  318. prev_entry->activity -= 1;
  319. if (entry->activity > prev_entry->activity &&
  320. entry->activity - prev_entry->activity >
  321. CIPSO_V4_CACHE_REORDERLIMIT) {
  322. __list_del(entry->list.prev, entry->list.next);
  323. __list_add(&entry->list,
  324. prev_entry->list.prev,
  325. &prev_entry->list);
  326. }
  327. spin_unlock_bh(&cipso_v4_cache[bkt].lock);
  328. return 0;
  329. }
  330. prev_entry = entry;
  331. }
  332. spin_unlock_bh(&cipso_v4_cache[bkt].lock);
  333. return -ENOENT;
  334. }
  335. /**
  336. * cipso_v4_cache_add - Add an entry to the CIPSO cache
  337. * @skb: the packet
  338. * @secattr: the packet's security attributes
  339. *
  340. * Description:
  341. * Add a new entry into the CIPSO label mapping cache. Add the new entry to
  342. * head of the cache bucket's list, if the cache bucket is out of room remove
  343. * the last entry in the list first. It is important to note that there is
  344. * currently no checking for duplicate keys. Returns zero on success,
  345. * negative values on failure.
  346. *
  347. */
  348. int cipso_v4_cache_add(const struct sk_buff *skb,
  349. const struct netlbl_lsm_secattr *secattr)
  350. {
  351. int ret_val = -EPERM;
  352. u32 bkt;
  353. struct cipso_v4_map_cache_entry *entry = NULL;
  354. struct cipso_v4_map_cache_entry *old_entry = NULL;
  355. unsigned char *cipso_ptr;
  356. u32 cipso_ptr_len;
  357. if (!cipso_v4_cache_enabled || cipso_v4_cache_bucketsize <= 0)
  358. return 0;
  359. cipso_ptr = CIPSO_V4_OPTPTR(skb);
  360. cipso_ptr_len = cipso_ptr[1];
  361. entry = kzalloc(sizeof(*entry), GFP_ATOMIC);
  362. if (entry == NULL)
  363. return -ENOMEM;
  364. entry->key = kmemdup(cipso_ptr, cipso_ptr_len, GFP_ATOMIC);
  365. if (entry->key == NULL) {
  366. ret_val = -ENOMEM;
  367. goto cache_add_failure;
  368. }
  369. entry->key_len = cipso_ptr_len;
  370. entry->hash = cipso_v4_map_cache_hash(cipso_ptr, cipso_ptr_len);
  371. atomic_inc(&secattr->cache->refcount);
  372. entry->lsm_data = secattr->cache;
  373. bkt = entry->hash & (CIPSO_V4_CACHE_BUCKETBITS - 1);
  374. spin_lock_bh(&cipso_v4_cache[bkt].lock);
  375. if (cipso_v4_cache[bkt].size < cipso_v4_cache_bucketsize) {
  376. list_add(&entry->list, &cipso_v4_cache[bkt].list);
  377. cipso_v4_cache[bkt].size += 1;
  378. } else {
  379. old_entry = list_entry(cipso_v4_cache[bkt].list.prev,
  380. struct cipso_v4_map_cache_entry, list);
  381. list_del(&old_entry->list);
  382. list_add(&entry->list, &cipso_v4_cache[bkt].list);
  383. cipso_v4_cache_entry_free(old_entry);
  384. }
  385. spin_unlock_bh(&cipso_v4_cache[bkt].lock);
  386. return 0;
  387. cache_add_failure:
  388. if (entry)
  389. cipso_v4_cache_entry_free(entry);
  390. return ret_val;
  391. }
  392. /*
  393. * DOI List Functions
  394. */
  395. /**
  396. * cipso_v4_doi_search - Searches for a DOI definition
  397. * @doi: the DOI to search for
  398. *
  399. * Description:
  400. * Search the DOI definition list for a DOI definition with a DOI value that
  401. * matches @doi. The caller is responsibile for calling rcu_read_[un]lock().
  402. * Returns a pointer to the DOI definition on success and NULL on failure.
  403. */
  404. static struct cipso_v4_doi *cipso_v4_doi_search(u32 doi)
  405. {
  406. struct cipso_v4_doi *iter;
  407. list_for_each_entry_rcu(iter, &cipso_v4_doi_list, list)
  408. if (iter->doi == doi && iter->valid)
  409. return iter;
  410. return NULL;
  411. }
  412. /**
  413. * cipso_v4_doi_add - Add a new DOI to the CIPSO protocol engine
  414. * @doi_def: the DOI structure
  415. *
  416. * Description:
  417. * The caller defines a new DOI for use by the CIPSO engine and calls this
  418. * function to add it to the list of acceptable domains. The caller must
  419. * ensure that the mapping table specified in @doi_def->map meets all of the
  420. * requirements of the mapping type (see cipso_ipv4.h for details). Returns
  421. * zero on success and non-zero on failure.
  422. *
  423. */
  424. int cipso_v4_doi_add(struct cipso_v4_doi *doi_def)
  425. {
  426. u32 iter;
  427. if (doi_def == NULL || doi_def->doi == CIPSO_V4_DOI_UNKNOWN)
  428. return -EINVAL;
  429. for (iter = 0; iter < CIPSO_V4_TAG_MAXCNT; iter++) {
  430. switch (doi_def->tags[iter]) {
  431. case CIPSO_V4_TAG_RBITMAP:
  432. break;
  433. case CIPSO_V4_TAG_RANGE:
  434. if (doi_def->type != CIPSO_V4_MAP_PASS)
  435. return -EINVAL;
  436. break;
  437. case CIPSO_V4_TAG_INVALID:
  438. if (iter == 0)
  439. return -EINVAL;
  440. break;
  441. case CIPSO_V4_TAG_ENUM:
  442. if (doi_def->type != CIPSO_V4_MAP_PASS)
  443. return -EINVAL;
  444. break;
  445. default:
  446. return -EINVAL;
  447. }
  448. }
  449. doi_def->valid = 1;
  450. INIT_RCU_HEAD(&doi_def->rcu);
  451. INIT_LIST_HEAD(&doi_def->dom_list);
  452. rcu_read_lock();
  453. if (cipso_v4_doi_search(doi_def->doi) != NULL)
  454. goto doi_add_failure_rlock;
  455. spin_lock(&cipso_v4_doi_list_lock);
  456. if (cipso_v4_doi_search(doi_def->doi) != NULL)
  457. goto doi_add_failure_slock;
  458. list_add_tail_rcu(&doi_def->list, &cipso_v4_doi_list);
  459. spin_unlock(&cipso_v4_doi_list_lock);
  460. rcu_read_unlock();
  461. return 0;
  462. doi_add_failure_slock:
  463. spin_unlock(&cipso_v4_doi_list_lock);
  464. doi_add_failure_rlock:
  465. rcu_read_unlock();
  466. return -EEXIST;
  467. }
  468. /**
  469. * cipso_v4_doi_remove - Remove an existing DOI from the CIPSO protocol engine
  470. * @doi: the DOI value
  471. * @audit_secid: the LSM secid to use in the audit message
  472. * @callback: the DOI cleanup/free callback
  473. *
  474. * Description:
  475. * Removes a DOI definition from the CIPSO engine, @callback is called to
  476. * free any memory. The NetLabel routines will be called to release their own
  477. * LSM domain mappings as well as our own domain list. Returns zero on
  478. * success and negative values on failure.
  479. *
  480. */
  481. int cipso_v4_doi_remove(u32 doi,
  482. struct netlbl_audit *audit_info,
  483. void (*callback) (struct rcu_head * head))
  484. {
  485. struct cipso_v4_doi *doi_def;
  486. struct cipso_v4_domhsh_entry *dom_iter;
  487. rcu_read_lock();
  488. if (cipso_v4_doi_search(doi) != NULL) {
  489. spin_lock(&cipso_v4_doi_list_lock);
  490. doi_def = cipso_v4_doi_search(doi);
  491. if (doi_def == NULL) {
  492. spin_unlock(&cipso_v4_doi_list_lock);
  493. rcu_read_unlock();
  494. return -ENOENT;
  495. }
  496. doi_def->valid = 0;
  497. list_del_rcu(&doi_def->list);
  498. spin_unlock(&cipso_v4_doi_list_lock);
  499. list_for_each_entry_rcu(dom_iter, &doi_def->dom_list, list)
  500. if (dom_iter->valid)
  501. netlbl_domhsh_remove(dom_iter->domain,
  502. audit_info);
  503. cipso_v4_cache_invalidate();
  504. rcu_read_unlock();
  505. call_rcu(&doi_def->rcu, callback);
  506. return 0;
  507. }
  508. rcu_read_unlock();
  509. return -ENOENT;
  510. }
  511. /**
  512. * cipso_v4_doi_getdef - Returns a pointer to a valid DOI definition
  513. * @doi: the DOI value
  514. *
  515. * Description:
  516. * Searches for a valid DOI definition and if one is found it is returned to
  517. * the caller. Otherwise NULL is returned. The caller must ensure that
  518. * rcu_read_lock() is held while accessing the returned definition.
  519. *
  520. */
  521. struct cipso_v4_doi *cipso_v4_doi_getdef(u32 doi)
  522. {
  523. return cipso_v4_doi_search(doi);
  524. }
  525. /**
  526. * cipso_v4_doi_walk - Iterate through the DOI definitions
  527. * @skip_cnt: skip past this number of DOI definitions, updated
  528. * @callback: callback for each DOI definition
  529. * @cb_arg: argument for the callback function
  530. *
  531. * Description:
  532. * Iterate over the DOI definition list, skipping the first @skip_cnt entries.
  533. * For each entry call @callback, if @callback returns a negative value stop
  534. * 'walking' through the list and return. Updates the value in @skip_cnt upon
  535. * return. Returns zero on success, negative values on failure.
  536. *
  537. */
  538. int cipso_v4_doi_walk(u32 *skip_cnt,
  539. int (*callback) (struct cipso_v4_doi *doi_def, void *arg),
  540. void *cb_arg)
  541. {
  542. int ret_val = -ENOENT;
  543. u32 doi_cnt = 0;
  544. struct cipso_v4_doi *iter_doi;
  545. rcu_read_lock();
  546. list_for_each_entry_rcu(iter_doi, &cipso_v4_doi_list, list)
  547. if (iter_doi->valid) {
  548. if (doi_cnt++ < *skip_cnt)
  549. continue;
  550. ret_val = callback(iter_doi, cb_arg);
  551. if (ret_val < 0) {
  552. doi_cnt--;
  553. goto doi_walk_return;
  554. }
  555. }
  556. doi_walk_return:
  557. rcu_read_unlock();
  558. *skip_cnt = doi_cnt;
  559. return ret_val;
  560. }
  561. /**
  562. * cipso_v4_doi_domhsh_add - Adds a domain entry to a DOI definition
  563. * @doi_def: the DOI definition
  564. * @domain: the domain to add
  565. *
  566. * Description:
  567. * Adds the @domain to the DOI specified by @doi_def, this function
  568. * should only be called by external functions (i.e. NetLabel). This function
  569. * does allocate memory. Returns zero on success, negative values on failure.
  570. *
  571. */
  572. int cipso_v4_doi_domhsh_add(struct cipso_v4_doi *doi_def, const char *domain)
  573. {
  574. struct cipso_v4_domhsh_entry *iter;
  575. struct cipso_v4_domhsh_entry *new_dom;
  576. new_dom = kzalloc(sizeof(*new_dom), GFP_KERNEL);
  577. if (new_dom == NULL)
  578. return -ENOMEM;
  579. if (domain) {
  580. new_dom->domain = kstrdup(domain, GFP_KERNEL);
  581. if (new_dom->domain == NULL) {
  582. kfree(new_dom);
  583. return -ENOMEM;
  584. }
  585. }
  586. new_dom->valid = 1;
  587. INIT_RCU_HEAD(&new_dom->rcu);
  588. rcu_read_lock();
  589. spin_lock(&cipso_v4_doi_list_lock);
  590. list_for_each_entry_rcu(iter, &doi_def->dom_list, list)
  591. if (iter->valid &&
  592. ((domain != NULL && iter->domain != NULL &&
  593. strcmp(iter->domain, domain) == 0) ||
  594. (domain == NULL && iter->domain == NULL))) {
  595. spin_unlock(&cipso_v4_doi_list_lock);
  596. rcu_read_unlock();
  597. kfree(new_dom->domain);
  598. kfree(new_dom);
  599. return -EEXIST;
  600. }
  601. list_add_tail_rcu(&new_dom->list, &doi_def->dom_list);
  602. spin_unlock(&cipso_v4_doi_list_lock);
  603. rcu_read_unlock();
  604. return 0;
  605. }
  606. /**
  607. * cipso_v4_doi_domhsh_remove - Removes a domain entry from a DOI definition
  608. * @doi_def: the DOI definition
  609. * @domain: the domain to remove
  610. *
  611. * Description:
  612. * Removes the @domain from the DOI specified by @doi_def, this function
  613. * should only be called by external functions (i.e. NetLabel). Returns zero
  614. * on success and negative values on error.
  615. *
  616. */
  617. int cipso_v4_doi_domhsh_remove(struct cipso_v4_doi *doi_def,
  618. const char *domain)
  619. {
  620. struct cipso_v4_domhsh_entry *iter;
  621. rcu_read_lock();
  622. spin_lock(&cipso_v4_doi_list_lock);
  623. list_for_each_entry_rcu(iter, &doi_def->dom_list, list)
  624. if (iter->valid &&
  625. ((domain != NULL && iter->domain != NULL &&
  626. strcmp(iter->domain, domain) == 0) ||
  627. (domain == NULL && iter->domain == NULL))) {
  628. iter->valid = 0;
  629. list_del_rcu(&iter->list);
  630. spin_unlock(&cipso_v4_doi_list_lock);
  631. rcu_read_unlock();
  632. call_rcu(&iter->rcu, cipso_v4_doi_domhsh_free);
  633. return 0;
  634. }
  635. spin_unlock(&cipso_v4_doi_list_lock);
  636. rcu_read_unlock();
  637. return -ENOENT;
  638. }
  639. /*
  640. * Label Mapping Functions
  641. */
  642. /**
  643. * cipso_v4_map_lvl_valid - Checks to see if the given level is understood
  644. * @doi_def: the DOI definition
  645. * @level: the level to check
  646. *
  647. * Description:
  648. * Checks the given level against the given DOI definition and returns a
  649. * negative value if the level does not have a valid mapping and a zero value
  650. * if the level is defined by the DOI.
  651. *
  652. */
  653. static int cipso_v4_map_lvl_valid(const struct cipso_v4_doi *doi_def, u8 level)
  654. {
  655. switch (doi_def->type) {
  656. case CIPSO_V4_MAP_PASS:
  657. return 0;
  658. case CIPSO_V4_MAP_STD:
  659. if (doi_def->map.std->lvl.cipso[level] < CIPSO_V4_INV_LVL)
  660. return 0;
  661. break;
  662. }
  663. return -EFAULT;
  664. }
  665. /**
  666. * cipso_v4_map_lvl_hton - Perform a level mapping from the host to the network
  667. * @doi_def: the DOI definition
  668. * @host_lvl: the host MLS level
  669. * @net_lvl: the network/CIPSO MLS level
  670. *
  671. * Description:
  672. * Perform a label mapping to translate a local MLS level to the correct
  673. * CIPSO level using the given DOI definition. Returns zero on success,
  674. * negative values otherwise.
  675. *
  676. */
  677. static int cipso_v4_map_lvl_hton(const struct cipso_v4_doi *doi_def,
  678. u32 host_lvl,
  679. u32 *net_lvl)
  680. {
  681. switch (doi_def->type) {
  682. case CIPSO_V4_MAP_PASS:
  683. *net_lvl = host_lvl;
  684. return 0;
  685. case CIPSO_V4_MAP_STD:
  686. if (host_lvl < doi_def->map.std->lvl.local_size &&
  687. doi_def->map.std->lvl.local[host_lvl] < CIPSO_V4_INV_LVL) {
  688. *net_lvl = doi_def->map.std->lvl.local[host_lvl];
  689. return 0;
  690. }
  691. return -EPERM;
  692. }
  693. return -EINVAL;
  694. }
  695. /**
  696. * cipso_v4_map_lvl_ntoh - Perform a level mapping from the network to the host
  697. * @doi_def: the DOI definition
  698. * @net_lvl: the network/CIPSO MLS level
  699. * @host_lvl: the host MLS level
  700. *
  701. * Description:
  702. * Perform a label mapping to translate a CIPSO level to the correct local MLS
  703. * level using the given DOI definition. Returns zero on success, negative
  704. * values otherwise.
  705. *
  706. */
  707. static int cipso_v4_map_lvl_ntoh(const struct cipso_v4_doi *doi_def,
  708. u32 net_lvl,
  709. u32 *host_lvl)
  710. {
  711. struct cipso_v4_std_map_tbl *map_tbl;
  712. switch (doi_def->type) {
  713. case CIPSO_V4_MAP_PASS:
  714. *host_lvl = net_lvl;
  715. return 0;
  716. case CIPSO_V4_MAP_STD:
  717. map_tbl = doi_def->map.std;
  718. if (net_lvl < map_tbl->lvl.cipso_size &&
  719. map_tbl->lvl.cipso[net_lvl] < CIPSO_V4_INV_LVL) {
  720. *host_lvl = doi_def->map.std->lvl.cipso[net_lvl];
  721. return 0;
  722. }
  723. return -EPERM;
  724. }
  725. return -EINVAL;
  726. }
  727. /**
  728. * cipso_v4_map_cat_rbm_valid - Checks to see if the category bitmap is valid
  729. * @doi_def: the DOI definition
  730. * @bitmap: category bitmap
  731. * @bitmap_len: bitmap length in bytes
  732. *
  733. * Description:
  734. * Checks the given category bitmap against the given DOI definition and
  735. * returns a negative value if any of the categories in the bitmap do not have
  736. * a valid mapping and a zero value if all of the categories are valid.
  737. *
  738. */
  739. static int cipso_v4_map_cat_rbm_valid(const struct cipso_v4_doi *doi_def,
  740. const unsigned char *bitmap,
  741. u32 bitmap_len)
  742. {
  743. int cat = -1;
  744. u32 bitmap_len_bits = bitmap_len * 8;
  745. u32 cipso_cat_size;
  746. u32 *cipso_array;
  747. switch (doi_def->type) {
  748. case CIPSO_V4_MAP_PASS:
  749. return 0;
  750. case CIPSO_V4_MAP_STD:
  751. cipso_cat_size = doi_def->map.std->cat.cipso_size;
  752. cipso_array = doi_def->map.std->cat.cipso;
  753. for (;;) {
  754. cat = cipso_v4_bitmap_walk(bitmap,
  755. bitmap_len_bits,
  756. cat + 1,
  757. 1);
  758. if (cat < 0)
  759. break;
  760. if (cat >= cipso_cat_size ||
  761. cipso_array[cat] >= CIPSO_V4_INV_CAT)
  762. return -EFAULT;
  763. }
  764. if (cat == -1)
  765. return 0;
  766. break;
  767. }
  768. return -EFAULT;
  769. }
  770. /**
  771. * cipso_v4_map_cat_rbm_hton - Perform a category mapping from host to network
  772. * @doi_def: the DOI definition
  773. * @secattr: the security attributes
  774. * @net_cat: the zero'd out category bitmap in network/CIPSO format
  775. * @net_cat_len: the length of the CIPSO bitmap in bytes
  776. *
  777. * Description:
  778. * Perform a label mapping to translate a local MLS category bitmap to the
  779. * correct CIPSO bitmap using the given DOI definition. Returns the minimum
  780. * size in bytes of the network bitmap on success, negative values otherwise.
  781. *
  782. */
  783. static int cipso_v4_map_cat_rbm_hton(const struct cipso_v4_doi *doi_def,
  784. const struct netlbl_lsm_secattr *secattr,
  785. unsigned char *net_cat,
  786. u32 net_cat_len)
  787. {
  788. int host_spot = -1;
  789. u32 net_spot = CIPSO_V4_INV_CAT;
  790. u32 net_spot_max = 0;
  791. u32 net_clen_bits = net_cat_len * 8;
  792. u32 host_cat_size = 0;
  793. u32 *host_cat_array = NULL;
  794. if (doi_def->type == CIPSO_V4_MAP_STD) {
  795. host_cat_size = doi_def->map.std->cat.local_size;
  796. host_cat_array = doi_def->map.std->cat.local;
  797. }
  798. for (;;) {
  799. host_spot = netlbl_secattr_catmap_walk(secattr->mls_cat,
  800. host_spot + 1);
  801. if (host_spot < 0)
  802. break;
  803. switch (doi_def->type) {
  804. case CIPSO_V4_MAP_PASS:
  805. net_spot = host_spot;
  806. break;
  807. case CIPSO_V4_MAP_STD:
  808. if (host_spot >= host_cat_size)
  809. return -EPERM;
  810. net_spot = host_cat_array[host_spot];
  811. if (net_spot >= CIPSO_V4_INV_CAT)
  812. return -EPERM;
  813. break;
  814. }
  815. if (net_spot >= net_clen_bits)
  816. return -ENOSPC;
  817. cipso_v4_bitmap_setbit(net_cat, net_spot, 1);
  818. if (net_spot > net_spot_max)
  819. net_spot_max = net_spot;
  820. }
  821. if (++net_spot_max % 8)
  822. return net_spot_max / 8 + 1;
  823. return net_spot_max / 8;
  824. }
  825. /**
  826. * cipso_v4_map_cat_rbm_ntoh - Perform a category mapping from network to host
  827. * @doi_def: the DOI definition
  828. * @net_cat: the category bitmap in network/CIPSO format
  829. * @net_cat_len: the length of the CIPSO bitmap in bytes
  830. * @secattr: the security attributes
  831. *
  832. * Description:
  833. * Perform a label mapping to translate a CIPSO bitmap to the correct local
  834. * MLS category bitmap using the given DOI definition. Returns zero on
  835. * success, negative values on failure.
  836. *
  837. */
  838. static int cipso_v4_map_cat_rbm_ntoh(const struct cipso_v4_doi *doi_def,
  839. const unsigned char *net_cat,
  840. u32 net_cat_len,
  841. struct netlbl_lsm_secattr *secattr)
  842. {
  843. int ret_val;
  844. int net_spot = -1;
  845. u32 host_spot = CIPSO_V4_INV_CAT;
  846. u32 net_clen_bits = net_cat_len * 8;
  847. u32 net_cat_size = 0;
  848. u32 *net_cat_array = NULL;
  849. if (doi_def->type == CIPSO_V4_MAP_STD) {
  850. net_cat_size = doi_def->map.std->cat.cipso_size;
  851. net_cat_array = doi_def->map.std->cat.cipso;
  852. }
  853. for (;;) {
  854. net_spot = cipso_v4_bitmap_walk(net_cat,
  855. net_clen_bits,
  856. net_spot + 1,
  857. 1);
  858. if (net_spot < 0) {
  859. if (net_spot == -2)
  860. return -EFAULT;
  861. return 0;
  862. }
  863. switch (doi_def->type) {
  864. case CIPSO_V4_MAP_PASS:
  865. host_spot = net_spot;
  866. break;
  867. case CIPSO_V4_MAP_STD:
  868. if (net_spot >= net_cat_size)
  869. return -EPERM;
  870. host_spot = net_cat_array[net_spot];
  871. if (host_spot >= CIPSO_V4_INV_CAT)
  872. return -EPERM;
  873. break;
  874. }
  875. ret_val = netlbl_secattr_catmap_setbit(secattr->mls_cat,
  876. host_spot,
  877. GFP_ATOMIC);
  878. if (ret_val != 0)
  879. return ret_val;
  880. }
  881. return -EINVAL;
  882. }
  883. /**
  884. * cipso_v4_map_cat_enum_valid - Checks to see if the categories are valid
  885. * @doi_def: the DOI definition
  886. * @enumcat: category list
  887. * @enumcat_len: length of the category list in bytes
  888. *
  889. * Description:
  890. * Checks the given categories against the given DOI definition and returns a
  891. * negative value if any of the categories do not have a valid mapping and a
  892. * zero value if all of the categories are valid.
  893. *
  894. */
  895. static int cipso_v4_map_cat_enum_valid(const struct cipso_v4_doi *doi_def,
  896. const unsigned char *enumcat,
  897. u32 enumcat_len)
  898. {
  899. u16 cat;
  900. int cat_prev = -1;
  901. u32 iter;
  902. if (doi_def->type != CIPSO_V4_MAP_PASS || enumcat_len & 0x01)
  903. return -EFAULT;
  904. for (iter = 0; iter < enumcat_len; iter += 2) {
  905. cat = ntohs(*((__be16 *)&enumcat[iter]));
  906. if (cat <= cat_prev)
  907. return -EFAULT;
  908. cat_prev = cat;
  909. }
  910. return 0;
  911. }
  912. /**
  913. * cipso_v4_map_cat_enum_hton - Perform a category mapping from host to network
  914. * @doi_def: the DOI definition
  915. * @secattr: the security attributes
  916. * @net_cat: the zero'd out category list in network/CIPSO format
  917. * @net_cat_len: the length of the CIPSO category list in bytes
  918. *
  919. * Description:
  920. * Perform a label mapping to translate a local MLS category bitmap to the
  921. * correct CIPSO category list using the given DOI definition. Returns the
  922. * size in bytes of the network category bitmap on success, negative values
  923. * otherwise.
  924. *
  925. */
  926. static int cipso_v4_map_cat_enum_hton(const struct cipso_v4_doi *doi_def,
  927. const struct netlbl_lsm_secattr *secattr,
  928. unsigned char *net_cat,
  929. u32 net_cat_len)
  930. {
  931. int cat = -1;
  932. u32 cat_iter = 0;
  933. for (;;) {
  934. cat = netlbl_secattr_catmap_walk(secattr->mls_cat, cat + 1);
  935. if (cat < 0)
  936. break;
  937. if ((cat_iter + 2) > net_cat_len)
  938. return -ENOSPC;
  939. *((__be16 *)&net_cat[cat_iter]) = htons(cat);
  940. cat_iter += 2;
  941. }
  942. return cat_iter;
  943. }
  944. /**
  945. * cipso_v4_map_cat_enum_ntoh - Perform a category mapping from network to host
  946. * @doi_def: the DOI definition
  947. * @net_cat: the category list in network/CIPSO format
  948. * @net_cat_len: the length of the CIPSO bitmap in bytes
  949. * @secattr: the security attributes
  950. *
  951. * Description:
  952. * Perform a label mapping to translate a CIPSO category list to the correct
  953. * local MLS category bitmap using the given DOI definition. Returns zero on
  954. * success, negative values on failure.
  955. *
  956. */
  957. static int cipso_v4_map_cat_enum_ntoh(const struct cipso_v4_doi *doi_def,
  958. const unsigned char *net_cat,
  959. u32 net_cat_len,
  960. struct netlbl_lsm_secattr *secattr)
  961. {
  962. int ret_val;
  963. u32 iter;
  964. for (iter = 0; iter < net_cat_len; iter += 2) {
  965. ret_val = netlbl_secattr_catmap_setbit(secattr->mls_cat,
  966. ntohs(*((__be16 *)&net_cat[iter])),
  967. GFP_ATOMIC);
  968. if (ret_val != 0)
  969. return ret_val;
  970. }
  971. return 0;
  972. }
  973. /**
  974. * cipso_v4_map_cat_rng_valid - Checks to see if the categories are valid
  975. * @doi_def: the DOI definition
  976. * @rngcat: category list
  977. * @rngcat_len: length of the category list in bytes
  978. *
  979. * Description:
  980. * Checks the given categories against the given DOI definition and returns a
  981. * negative value if any of the categories do not have a valid mapping and a
  982. * zero value if all of the categories are valid.
  983. *
  984. */
  985. static int cipso_v4_map_cat_rng_valid(const struct cipso_v4_doi *doi_def,
  986. const unsigned char *rngcat,
  987. u32 rngcat_len)
  988. {
  989. u16 cat_high;
  990. u16 cat_low;
  991. u32 cat_prev = CIPSO_V4_MAX_REM_CATS + 1;
  992. u32 iter;
  993. if (doi_def->type != CIPSO_V4_MAP_PASS || rngcat_len & 0x01)
  994. return -EFAULT;
  995. for (iter = 0; iter < rngcat_len; iter += 4) {
  996. cat_high = ntohs(*((__be16 *)&rngcat[iter]));
  997. if ((iter + 4) <= rngcat_len)
  998. cat_low = ntohs(*((__be16 *)&rngcat[iter + 2]));
  999. else
  1000. cat_low = 0;
  1001. if (cat_high > cat_prev)
  1002. return -EFAULT;
  1003. cat_prev = cat_low;
  1004. }
  1005. return 0;
  1006. }
  1007. /**
  1008. * cipso_v4_map_cat_rng_hton - Perform a category mapping from host to network
  1009. * @doi_def: the DOI definition
  1010. * @secattr: the security attributes
  1011. * @net_cat: the zero'd out category list in network/CIPSO format
  1012. * @net_cat_len: the length of the CIPSO category list in bytes
  1013. *
  1014. * Description:
  1015. * Perform a label mapping to translate a local MLS category bitmap to the
  1016. * correct CIPSO category list using the given DOI definition. Returns the
  1017. * size in bytes of the network category bitmap on success, negative values
  1018. * otherwise.
  1019. *
  1020. */
  1021. static int cipso_v4_map_cat_rng_hton(const struct cipso_v4_doi *doi_def,
  1022. const struct netlbl_lsm_secattr *secattr,
  1023. unsigned char *net_cat,
  1024. u32 net_cat_len)
  1025. {
  1026. int iter = -1;
  1027. u16 array[CIPSO_V4_TAG_RNG_CAT_MAX * 2];
  1028. u32 array_cnt = 0;
  1029. u32 cat_size = 0;
  1030. /* make sure we don't overflow the 'array[]' variable */
  1031. if (net_cat_len >
  1032. (CIPSO_V4_OPT_LEN_MAX - CIPSO_V4_HDR_LEN - CIPSO_V4_TAG_RNG_BLEN))
  1033. return -ENOSPC;
  1034. for (;;) {
  1035. iter = netlbl_secattr_catmap_walk(secattr->mls_cat, iter + 1);
  1036. if (iter < 0)
  1037. break;
  1038. cat_size += (iter == 0 ? 0 : sizeof(u16));
  1039. if (cat_size > net_cat_len)
  1040. return -ENOSPC;
  1041. array[array_cnt++] = iter;
  1042. iter = netlbl_secattr_catmap_walk_rng(secattr->mls_cat, iter);
  1043. if (iter < 0)
  1044. return -EFAULT;
  1045. cat_size += sizeof(u16);
  1046. if (cat_size > net_cat_len)
  1047. return -ENOSPC;
  1048. array[array_cnt++] = iter;
  1049. }
  1050. for (iter = 0; array_cnt > 0;) {
  1051. *((__be16 *)&net_cat[iter]) = htons(array[--array_cnt]);
  1052. iter += 2;
  1053. array_cnt--;
  1054. if (array[array_cnt] != 0) {
  1055. *((__be16 *)&net_cat[iter]) = htons(array[array_cnt]);
  1056. iter += 2;
  1057. }
  1058. }
  1059. return cat_size;
  1060. }
  1061. /**
  1062. * cipso_v4_map_cat_rng_ntoh - Perform a category mapping from network to host
  1063. * @doi_def: the DOI definition
  1064. * @net_cat: the category list in network/CIPSO format
  1065. * @net_cat_len: the length of the CIPSO bitmap in bytes
  1066. * @secattr: the security attributes
  1067. *
  1068. * Description:
  1069. * Perform a label mapping to translate a CIPSO category list to the correct
  1070. * local MLS category bitmap using the given DOI definition. Returns zero on
  1071. * success, negative values on failure.
  1072. *
  1073. */
  1074. static int cipso_v4_map_cat_rng_ntoh(const struct cipso_v4_doi *doi_def,
  1075. const unsigned char *net_cat,
  1076. u32 net_cat_len,
  1077. struct netlbl_lsm_secattr *secattr)
  1078. {
  1079. int ret_val;
  1080. u32 net_iter;
  1081. u16 cat_low;
  1082. u16 cat_high;
  1083. for (net_iter = 0; net_iter < net_cat_len; net_iter += 4) {
  1084. cat_high = ntohs(*((__be16 *)&net_cat[net_iter]));
  1085. if ((net_iter + 4) <= net_cat_len)
  1086. cat_low = ntohs(*((__be16 *)&net_cat[net_iter + 2]));
  1087. else
  1088. cat_low = 0;
  1089. ret_val = netlbl_secattr_catmap_setrng(secattr->mls_cat,
  1090. cat_low,
  1091. cat_high,
  1092. GFP_ATOMIC);
  1093. if (ret_val != 0)
  1094. return ret_val;
  1095. }
  1096. return 0;
  1097. }
  1098. /*
  1099. * Protocol Handling Functions
  1100. */
  1101. /**
  1102. * cipso_v4_gentag_hdr - Generate a CIPSO option header
  1103. * @doi_def: the DOI definition
  1104. * @len: the total tag length in bytes, not including this header
  1105. * @buf: the CIPSO option buffer
  1106. *
  1107. * Description:
  1108. * Write a CIPSO header into the beginning of @buffer.
  1109. *
  1110. */
  1111. static void cipso_v4_gentag_hdr(const struct cipso_v4_doi *doi_def,
  1112. unsigned char *buf,
  1113. u32 len)
  1114. {
  1115. buf[0] = IPOPT_CIPSO;
  1116. buf[1] = CIPSO_V4_HDR_LEN + len;
  1117. *(__be32 *)&buf[2] = htonl(doi_def->doi);
  1118. }
  1119. /**
  1120. * cipso_v4_gentag_rbm - Generate a CIPSO restricted bitmap tag (type #1)
  1121. * @doi_def: the DOI definition
  1122. * @secattr: the security attributes
  1123. * @buffer: the option buffer
  1124. * @buffer_len: length of buffer in bytes
  1125. *
  1126. * Description:
  1127. * Generate a CIPSO option using the restricted bitmap tag, tag type #1. The
  1128. * actual buffer length may be larger than the indicated size due to
  1129. * translation between host and network category bitmaps. Returns the size of
  1130. * the tag on success, negative values on failure.
  1131. *
  1132. */
  1133. static int cipso_v4_gentag_rbm(const struct cipso_v4_doi *doi_def,
  1134. const struct netlbl_lsm_secattr *secattr,
  1135. unsigned char *buffer,
  1136. u32 buffer_len)
  1137. {
  1138. int ret_val;
  1139. u32 tag_len;
  1140. u32 level;
  1141. if ((secattr->flags & NETLBL_SECATTR_MLS_LVL) == 0)
  1142. return -EPERM;
  1143. ret_val = cipso_v4_map_lvl_hton(doi_def, secattr->mls_lvl, &level);
  1144. if (ret_val != 0)
  1145. return ret_val;
  1146. if (secattr->flags & NETLBL_SECATTR_MLS_CAT) {
  1147. ret_val = cipso_v4_map_cat_rbm_hton(doi_def,
  1148. secattr,
  1149. &buffer[4],
  1150. buffer_len - 4);
  1151. if (ret_val < 0)
  1152. return ret_val;
  1153. /* This will send packets using the "optimized" format when
  1154. * possibile as specified in section 3.4.2.6 of the
  1155. * CIPSO draft. */
  1156. if (cipso_v4_rbm_optfmt && ret_val > 0 && ret_val <= 10)
  1157. tag_len = 14;
  1158. else
  1159. tag_len = 4 + ret_val;
  1160. } else
  1161. tag_len = 4;
  1162. buffer[0] = 0x01;
  1163. buffer[1] = tag_len;
  1164. buffer[3] = level;
  1165. return tag_len;
  1166. }
  1167. /**
  1168. * cipso_v4_parsetag_rbm - Parse a CIPSO restricted bitmap tag
  1169. * @doi_def: the DOI definition
  1170. * @tag: the CIPSO tag
  1171. * @secattr: the security attributes
  1172. *
  1173. * Description:
  1174. * Parse a CIPSO restricted bitmap tag (tag type #1) and return the security
  1175. * attributes in @secattr. Return zero on success, negatives values on
  1176. * failure.
  1177. *
  1178. */
  1179. static int cipso_v4_parsetag_rbm(const struct cipso_v4_doi *doi_def,
  1180. const unsigned char *tag,
  1181. struct netlbl_lsm_secattr *secattr)
  1182. {
  1183. int ret_val;
  1184. u8 tag_len = tag[1];
  1185. u32 level;
  1186. ret_val = cipso_v4_map_lvl_ntoh(doi_def, tag[3], &level);
  1187. if (ret_val != 0)
  1188. return ret_val;
  1189. secattr->mls_lvl = level;
  1190. secattr->flags |= NETLBL_SECATTR_MLS_LVL;
  1191. if (tag_len > 4) {
  1192. secattr->mls_cat = netlbl_secattr_catmap_alloc(GFP_ATOMIC);
  1193. if (secattr->mls_cat == NULL)
  1194. return -ENOMEM;
  1195. ret_val = cipso_v4_map_cat_rbm_ntoh(doi_def,
  1196. &tag[4],
  1197. tag_len - 4,
  1198. secattr);
  1199. if (ret_val != 0) {
  1200. netlbl_secattr_catmap_free(secattr->mls_cat);
  1201. return ret_val;
  1202. }
  1203. secattr->flags |= NETLBL_SECATTR_MLS_CAT;
  1204. }
  1205. return 0;
  1206. }
  1207. /**
  1208. * cipso_v4_gentag_enum - Generate a CIPSO enumerated tag (type #2)
  1209. * @doi_def: the DOI definition
  1210. * @secattr: the security attributes
  1211. * @buffer: the option buffer
  1212. * @buffer_len: length of buffer in bytes
  1213. *
  1214. * Description:
  1215. * Generate a CIPSO option using the enumerated tag, tag type #2. Returns the
  1216. * size of the tag on success, negative values on failure.
  1217. *
  1218. */
  1219. static int cipso_v4_gentag_enum(const struct cipso_v4_doi *doi_def,
  1220. const struct netlbl_lsm_secattr *secattr,
  1221. unsigned char *buffer,
  1222. u32 buffer_len)
  1223. {
  1224. int ret_val;
  1225. u32 tag_len;
  1226. u32 level;
  1227. if (!(secattr->flags & NETLBL_SECATTR_MLS_LVL))
  1228. return -EPERM;
  1229. ret_val = cipso_v4_map_lvl_hton(doi_def, secattr->mls_lvl, &level);
  1230. if (ret_val != 0)
  1231. return ret_val;
  1232. if (secattr->flags & NETLBL_SECATTR_MLS_CAT) {
  1233. ret_val = cipso_v4_map_cat_enum_hton(doi_def,
  1234. secattr,
  1235. &buffer[4],
  1236. buffer_len - 4);
  1237. if (ret_val < 0)
  1238. return ret_val;
  1239. tag_len = 4 + ret_val;
  1240. } else
  1241. tag_len = 4;
  1242. buffer[0] = 0x02;
  1243. buffer[1] = tag_len;
  1244. buffer[3] = level;
  1245. return tag_len;
  1246. }
  1247. /**
  1248. * cipso_v4_parsetag_enum - Parse a CIPSO enumerated tag
  1249. * @doi_def: the DOI definition
  1250. * @tag: the CIPSO tag
  1251. * @secattr: the security attributes
  1252. *
  1253. * Description:
  1254. * Parse a CIPSO enumerated tag (tag type #2) and return the security
  1255. * attributes in @secattr. Return zero on success, negatives values on
  1256. * failure.
  1257. *
  1258. */
  1259. static int cipso_v4_parsetag_enum(const struct cipso_v4_doi *doi_def,
  1260. const unsigned char *tag,
  1261. struct netlbl_lsm_secattr *secattr)
  1262. {
  1263. int ret_val;
  1264. u8 tag_len = tag[1];
  1265. u32 level;
  1266. ret_val = cipso_v4_map_lvl_ntoh(doi_def, tag[3], &level);
  1267. if (ret_val != 0)
  1268. return ret_val;
  1269. secattr->mls_lvl = level;
  1270. secattr->flags |= NETLBL_SECATTR_MLS_LVL;
  1271. if (tag_len > 4) {
  1272. secattr->mls_cat = netlbl_secattr_catmap_alloc(GFP_ATOMIC);
  1273. if (secattr->mls_cat == NULL)
  1274. return -ENOMEM;
  1275. ret_val = cipso_v4_map_cat_enum_ntoh(doi_def,
  1276. &tag[4],
  1277. tag_len - 4,
  1278. secattr);
  1279. if (ret_val != 0) {
  1280. netlbl_secattr_catmap_free(secattr->mls_cat);
  1281. return ret_val;
  1282. }
  1283. secattr->flags |= NETLBL_SECATTR_MLS_CAT;
  1284. }
  1285. return 0;
  1286. }
  1287. /**
  1288. * cipso_v4_gentag_rng - Generate a CIPSO ranged tag (type #5)
  1289. * @doi_def: the DOI definition
  1290. * @secattr: the security attributes
  1291. * @buffer: the option buffer
  1292. * @buffer_len: length of buffer in bytes
  1293. *
  1294. * Description:
  1295. * Generate a CIPSO option using the ranged tag, tag type #5. Returns the
  1296. * size of the tag on success, negative values on failure.
  1297. *
  1298. */
  1299. static int cipso_v4_gentag_rng(const struct cipso_v4_doi *doi_def,
  1300. const struct netlbl_lsm_secattr *secattr,
  1301. unsigned char *buffer,
  1302. u32 buffer_len)
  1303. {
  1304. int ret_val;
  1305. u32 tag_len;
  1306. u32 level;
  1307. if (!(secattr->flags & NETLBL_SECATTR_MLS_LVL))
  1308. return -EPERM;
  1309. ret_val = cipso_v4_map_lvl_hton(doi_def, secattr->mls_lvl, &level);
  1310. if (ret_val != 0)
  1311. return ret_val;
  1312. if (secattr->flags & NETLBL_SECATTR_MLS_CAT) {
  1313. ret_val = cipso_v4_map_cat_rng_hton(doi_def,
  1314. secattr,
  1315. &buffer[4],
  1316. buffer_len - 4);
  1317. if (ret_val < 0)
  1318. return ret_val;
  1319. tag_len = 4 + ret_val;
  1320. } else
  1321. tag_len = 4;
  1322. buffer[0] = 0x05;
  1323. buffer[1] = tag_len;
  1324. buffer[3] = level;
  1325. return tag_len;
  1326. }
  1327. /**
  1328. * cipso_v4_parsetag_rng - Parse a CIPSO ranged tag
  1329. * @doi_def: the DOI definition
  1330. * @tag: the CIPSO tag
  1331. * @secattr: the security attributes
  1332. *
  1333. * Description:
  1334. * Parse a CIPSO ranged tag (tag type #5) and return the security attributes
  1335. * in @secattr. Return zero on success, negatives values on failure.
  1336. *
  1337. */
  1338. static int cipso_v4_parsetag_rng(const struct cipso_v4_doi *doi_def,
  1339. const unsigned char *tag,
  1340. struct netlbl_lsm_secattr *secattr)
  1341. {
  1342. int ret_val;
  1343. u8 tag_len = tag[1];
  1344. u32 level;
  1345. ret_val = cipso_v4_map_lvl_ntoh(doi_def, tag[3], &level);
  1346. if (ret_val != 0)
  1347. return ret_val;
  1348. secattr->mls_lvl = level;
  1349. secattr->flags |= NETLBL_SECATTR_MLS_LVL;
  1350. if (tag_len > 4) {
  1351. secattr->mls_cat = netlbl_secattr_catmap_alloc(GFP_ATOMIC);
  1352. if (secattr->mls_cat == NULL)
  1353. return -ENOMEM;
  1354. ret_val = cipso_v4_map_cat_rng_ntoh(doi_def,
  1355. &tag[4],
  1356. tag_len - 4,
  1357. secattr);
  1358. if (ret_val != 0) {
  1359. netlbl_secattr_catmap_free(secattr->mls_cat);
  1360. return ret_val;
  1361. }
  1362. secattr->flags |= NETLBL_SECATTR_MLS_CAT;
  1363. }
  1364. return 0;
  1365. }
  1366. /**
  1367. * cipso_v4_validate - Validate a CIPSO option
  1368. * @option: the start of the option, on error it is set to point to the error
  1369. *
  1370. * Description:
  1371. * This routine is called to validate a CIPSO option, it checks all of the
  1372. * fields to ensure that they are at least valid, see the draft snippet below
  1373. * for details. If the option is valid then a zero value is returned and
  1374. * the value of @option is unchanged. If the option is invalid then a
  1375. * non-zero value is returned and @option is adjusted to point to the
  1376. * offending portion of the option. From the IETF draft ...
  1377. *
  1378. * "If any field within the CIPSO options, such as the DOI identifier, is not
  1379. * recognized the IP datagram is discarded and an ICMP 'parameter problem'
  1380. * (type 12) is generated and returned. The ICMP code field is set to 'bad
  1381. * parameter' (code 0) and the pointer is set to the start of the CIPSO field
  1382. * that is unrecognized."
  1383. *
  1384. */
  1385. int cipso_v4_validate(unsigned char **option)
  1386. {
  1387. unsigned char *opt = *option;
  1388. unsigned char *tag;
  1389. unsigned char opt_iter;
  1390. unsigned char err_offset = 0;
  1391. u8 opt_len;
  1392. u8 tag_len;
  1393. struct cipso_v4_doi *doi_def = NULL;
  1394. u32 tag_iter;
  1395. /* caller already checks for length values that are too large */
  1396. opt_len = opt[1];
  1397. if (opt_len < 8) {
  1398. err_offset = 1;
  1399. goto validate_return;
  1400. }
  1401. rcu_read_lock();
  1402. doi_def = cipso_v4_doi_search(ntohl(*((__be32 *)&opt[2])));
  1403. if (doi_def == NULL) {
  1404. err_offset = 2;
  1405. goto validate_return_locked;
  1406. }
  1407. opt_iter = 6;
  1408. tag = opt + opt_iter;
  1409. while (opt_iter < opt_len) {
  1410. for (tag_iter = 0; doi_def->tags[tag_iter] != tag[0];)
  1411. if (doi_def->tags[tag_iter] == CIPSO_V4_TAG_INVALID ||
  1412. ++tag_iter == CIPSO_V4_TAG_MAXCNT) {
  1413. err_offset = opt_iter;
  1414. goto validate_return_locked;
  1415. }
  1416. tag_len = tag[1];
  1417. if (tag_len > (opt_len - opt_iter)) {
  1418. err_offset = opt_iter + 1;
  1419. goto validate_return_locked;
  1420. }
  1421. switch (tag[0]) {
  1422. case CIPSO_V4_TAG_RBITMAP:
  1423. if (tag_len < 4) {
  1424. err_offset = opt_iter + 1;
  1425. goto validate_return_locked;
  1426. }
  1427. /* We are already going to do all the verification
  1428. * necessary at the socket layer so from our point of
  1429. * view it is safe to turn these checks off (and less
  1430. * work), however, the CIPSO draft says we should do
  1431. * all the CIPSO validations here but it doesn't
  1432. * really specify _exactly_ what we need to validate
  1433. * ... so, just make it a sysctl tunable. */
  1434. if (cipso_v4_rbm_strictvalid) {
  1435. if (cipso_v4_map_lvl_valid(doi_def,
  1436. tag[3]) < 0) {
  1437. err_offset = opt_iter + 3;
  1438. goto validate_return_locked;
  1439. }
  1440. if (tag_len > 4 &&
  1441. cipso_v4_map_cat_rbm_valid(doi_def,
  1442. &tag[4],
  1443. tag_len - 4) < 0) {
  1444. err_offset = opt_iter + 4;
  1445. goto validate_return_locked;
  1446. }
  1447. }
  1448. break;
  1449. case CIPSO_V4_TAG_ENUM:
  1450. if (tag_len < 4) {
  1451. err_offset = opt_iter + 1;
  1452. goto validate_return_locked;
  1453. }
  1454. if (cipso_v4_map_lvl_valid(doi_def,
  1455. tag[3]) < 0) {
  1456. err_offset = opt_iter + 3;
  1457. goto validate_return_locked;
  1458. }
  1459. if (tag_len > 4 &&
  1460. cipso_v4_map_cat_enum_valid(doi_def,
  1461. &tag[4],
  1462. tag_len - 4) < 0) {
  1463. err_offset = opt_iter + 4;
  1464. goto validate_return_locked;
  1465. }
  1466. break;
  1467. case CIPSO_V4_TAG_RANGE:
  1468. if (tag_len < 4) {
  1469. err_offset = opt_iter + 1;
  1470. goto validate_return_locked;
  1471. }
  1472. if (cipso_v4_map_lvl_valid(doi_def,
  1473. tag[3]) < 0) {
  1474. err_offset = opt_iter + 3;
  1475. goto validate_return_locked;
  1476. }
  1477. if (tag_len > 4 &&
  1478. cipso_v4_map_cat_rng_valid(doi_def,
  1479. &tag[4],
  1480. tag_len - 4) < 0) {
  1481. err_offset = opt_iter + 4;
  1482. goto validate_return_locked;
  1483. }
  1484. break;
  1485. default:
  1486. err_offset = opt_iter;
  1487. goto validate_return_locked;
  1488. }
  1489. tag += tag_len;
  1490. opt_iter += tag_len;
  1491. }
  1492. validate_return_locked:
  1493. rcu_read_unlock();
  1494. validate_return:
  1495. *option = opt + err_offset;
  1496. return err_offset;
  1497. }
  1498. /**
  1499. * cipso_v4_error - Send the correct reponse for a bad packet
  1500. * @skb: the packet
  1501. * @error: the error code
  1502. * @gateway: CIPSO gateway flag
  1503. *
  1504. * Description:
  1505. * Based on the error code given in @error, send an ICMP error message back to
  1506. * the originating host. From the IETF draft ...
  1507. *
  1508. * "If the contents of the CIPSO [option] are valid but the security label is
  1509. * outside of the configured host or port label range, the datagram is
  1510. * discarded and an ICMP 'destination unreachable' (type 3) is generated and
  1511. * returned. The code field of the ICMP is set to 'communication with
  1512. * destination network administratively prohibited' (code 9) or to
  1513. * 'communication with destination host administratively prohibited'
  1514. * (code 10). The value of the code is dependent on whether the originator
  1515. * of the ICMP message is acting as a CIPSO host or a CIPSO gateway. The
  1516. * recipient of the ICMP message MUST be able to handle either value. The
  1517. * same procedure is performed if a CIPSO [option] can not be added to an
  1518. * IP packet because it is too large to fit in the IP options area."
  1519. *
  1520. * "If the error is triggered by receipt of an ICMP message, the message is
  1521. * discarded and no response is permitted (consistent with general ICMP
  1522. * processing rules)."
  1523. *
  1524. */
  1525. void cipso_v4_error(struct sk_buff *skb, int error, u32 gateway)
  1526. {
  1527. if (ip_hdr(skb)->protocol == IPPROTO_ICMP || error != -EACCES)
  1528. return;
  1529. if (gateway)
  1530. icmp_send(skb, ICMP_DEST_UNREACH, ICMP_NET_ANO, 0);
  1531. else
  1532. icmp_send(skb, ICMP_DEST_UNREACH, ICMP_HOST_ANO, 0);
  1533. }
  1534. /**
  1535. * cipso_v4_socket_setattr - Add a CIPSO option to a socket
  1536. * @sock: the socket
  1537. * @doi_def: the CIPSO DOI to use
  1538. * @secattr: the specific security attributes of the socket
  1539. *
  1540. * Description:
  1541. * Set the CIPSO option on the given socket using the DOI definition and
  1542. * security attributes passed to the function. This function requires
  1543. * exclusive access to @sock->sk, which means it either needs to be in the
  1544. * process of being created or locked via lock_sock(sock->sk). Returns zero on
  1545. * success and negative values on failure.
  1546. *
  1547. */
  1548. int cipso_v4_socket_setattr(const struct socket *sock,
  1549. const struct cipso_v4_doi *doi_def,
  1550. const struct netlbl_lsm_secattr *secattr)
  1551. {
  1552. int ret_val = -EPERM;
  1553. u32 iter;
  1554. unsigned char *buf;
  1555. u32 buf_len = 0;
  1556. u32 opt_len;
  1557. struct ip_options *opt = NULL;
  1558. struct sock *sk;
  1559. struct inet_sock *sk_inet;
  1560. struct inet_connection_sock *sk_conn;
  1561. /* In the case of sock_create_lite(), the sock->sk field is not
  1562. * defined yet but it is not a problem as the only users of these
  1563. * "lite" PF_INET sockets are functions which do an accept() call
  1564. * afterwards so we will label the socket as part of the accept(). */
  1565. sk = sock->sk;
  1566. if (sk == NULL)
  1567. return 0;
  1568. /* We allocate the maximum CIPSO option size here so we are probably
  1569. * being a little wasteful, but it makes our life _much_ easier later
  1570. * on and after all we are only talking about 40 bytes. */
  1571. buf_len = CIPSO_V4_OPT_LEN_MAX;
  1572. buf = kmalloc(buf_len, GFP_ATOMIC);
  1573. if (buf == NULL) {
  1574. ret_val = -ENOMEM;
  1575. goto socket_setattr_failure;
  1576. }
  1577. /* XXX - This code assumes only one tag per CIPSO option which isn't
  1578. * really a good assumption to make but since we only support the MAC
  1579. * tags right now it is a safe assumption. */
  1580. iter = 0;
  1581. do {
  1582. memset(buf, 0, buf_len);
  1583. switch (doi_def->tags[iter]) {
  1584. case CIPSO_V4_TAG_RBITMAP:
  1585. ret_val = cipso_v4_gentag_rbm(doi_def,
  1586. secattr,
  1587. &buf[CIPSO_V4_HDR_LEN],
  1588. buf_len - CIPSO_V4_HDR_LEN);
  1589. break;
  1590. case CIPSO_V4_TAG_ENUM:
  1591. ret_val = cipso_v4_gentag_enum(doi_def,
  1592. secattr,
  1593. &buf[CIPSO_V4_HDR_LEN],
  1594. buf_len - CIPSO_V4_HDR_LEN);
  1595. break;
  1596. case CIPSO_V4_TAG_RANGE:
  1597. ret_val = cipso_v4_gentag_rng(doi_def,
  1598. secattr,
  1599. &buf[CIPSO_V4_HDR_LEN],
  1600. buf_len - CIPSO_V4_HDR_LEN);
  1601. break;
  1602. default:
  1603. ret_val = -EPERM;
  1604. goto socket_setattr_failure;
  1605. }
  1606. iter++;
  1607. } while (ret_val < 0 &&
  1608. iter < CIPSO_V4_TAG_MAXCNT &&
  1609. doi_def->tags[iter] != CIPSO_V4_TAG_INVALID);
  1610. if (ret_val < 0)
  1611. goto socket_setattr_failure;
  1612. cipso_v4_gentag_hdr(doi_def, buf, ret_val);
  1613. buf_len = CIPSO_V4_HDR_LEN + ret_val;
  1614. /* We can't use ip_options_get() directly because it makes a call to
  1615. * ip_options_get_alloc() which allocates memory with GFP_KERNEL and
  1616. * we won't always have CAP_NET_RAW even though we _always_ want to
  1617. * set the IPOPT_CIPSO option. */
  1618. opt_len = (buf_len + 3) & ~3;
  1619. opt = kzalloc(sizeof(*opt) + opt_len, GFP_ATOMIC);
  1620. if (opt == NULL) {
  1621. ret_val = -ENOMEM;
  1622. goto socket_setattr_failure;
  1623. }
  1624. memcpy(opt->__data, buf, buf_len);
  1625. opt->optlen = opt_len;
  1626. opt->is_data = 1;
  1627. opt->cipso = sizeof(struct iphdr);
  1628. kfree(buf);
  1629. buf = NULL;
  1630. sk_inet = inet_sk(sk);
  1631. if (sk_inet->is_icsk) {
  1632. sk_conn = inet_csk(sk);
  1633. if (sk_inet->opt)
  1634. sk_conn->icsk_ext_hdr_len -= sk_inet->opt->optlen;
  1635. sk_conn->icsk_ext_hdr_len += opt->optlen;
  1636. sk_conn->icsk_sync_mss(sk, sk_conn->icsk_pmtu_cookie);
  1637. }
  1638. opt = xchg(&sk_inet->opt, opt);
  1639. kfree(opt);
  1640. return 0;
  1641. socket_setattr_failure:
  1642. kfree(buf);
  1643. kfree(opt);
  1644. return ret_val;
  1645. }
  1646. /**
  1647. * cipso_v4_sock_getattr - Get the security attributes from a sock
  1648. * @sk: the sock
  1649. * @secattr: the security attributes
  1650. *
  1651. * Description:
  1652. * Query @sk to see if there is a CIPSO option attached to the sock and if
  1653. * there is return the CIPSO security attributes in @secattr. This function
  1654. * requires that @sk be locked, or privately held, but it does not do any
  1655. * locking itself. Returns zero on success and negative values on failure.
  1656. *
  1657. */
  1658. int cipso_v4_sock_getattr(struct sock *sk, struct netlbl_lsm_secattr *secattr)
  1659. {
  1660. int ret_val = -ENOMSG;
  1661. struct inet_sock *sk_inet;
  1662. unsigned char *cipso_ptr;
  1663. u32 doi;
  1664. struct cipso_v4_doi *doi_def;
  1665. sk_inet = inet_sk(sk);
  1666. if (sk_inet->opt == NULL || sk_inet->opt->cipso == 0)
  1667. return -ENOMSG;
  1668. cipso_ptr = sk_inet->opt->__data + sk_inet->opt->cipso -
  1669. sizeof(struct iphdr);
  1670. ret_val = cipso_v4_cache_check(cipso_ptr, cipso_ptr[1], secattr);
  1671. if (ret_val == 0)
  1672. return ret_val;
  1673. doi = ntohl(*(__be32 *)&cipso_ptr[2]);
  1674. rcu_read_lock();
  1675. doi_def = cipso_v4_doi_search(doi);
  1676. if (doi_def == NULL) {
  1677. rcu_read_unlock();
  1678. return -ENOMSG;
  1679. }
  1680. /* XXX - This code assumes only one tag per CIPSO option which isn't
  1681. * really a good assumption to make but since we only support the MAC
  1682. * tags right now it is a safe assumption. */
  1683. switch (cipso_ptr[6]) {
  1684. case CIPSO_V4_TAG_RBITMAP:
  1685. ret_val = cipso_v4_parsetag_rbm(doi_def,
  1686. &cipso_ptr[6],
  1687. secattr);
  1688. break;
  1689. case CIPSO_V4_TAG_ENUM:
  1690. ret_val = cipso_v4_parsetag_enum(doi_def,
  1691. &cipso_ptr[6],
  1692. secattr);
  1693. break;
  1694. case CIPSO_V4_TAG_RANGE:
  1695. ret_val = cipso_v4_parsetag_rng(doi_def,
  1696. &cipso_ptr[6],
  1697. secattr);
  1698. break;
  1699. }
  1700. rcu_read_unlock();
  1701. return ret_val;
  1702. }
  1703. /**
  1704. * cipso_v4_socket_getattr - Get the security attributes from a socket
  1705. * @sock: the socket
  1706. * @secattr: the security attributes
  1707. *
  1708. * Description:
  1709. * Query @sock to see if there is a CIPSO option attached to the socket and if
  1710. * there is return the CIPSO security attributes in @secattr. Returns zero on
  1711. * success and negative values on failure.
  1712. *
  1713. */
  1714. int cipso_v4_socket_getattr(const struct socket *sock,
  1715. struct netlbl_lsm_secattr *secattr)
  1716. {
  1717. int ret_val;
  1718. lock_sock(sock->sk);
  1719. ret_val = cipso_v4_sock_getattr(sock->sk, secattr);
  1720. release_sock(sock->sk);
  1721. return ret_val;
  1722. }
  1723. /**
  1724. * cipso_v4_skbuff_getattr - Get the security attributes from the CIPSO option
  1725. * @skb: the packet
  1726. * @secattr: the security attributes
  1727. *
  1728. * Description:
  1729. * Parse the given packet's CIPSO option and return the security attributes.
  1730. * Returns zero on success and negative values on failure.
  1731. *
  1732. */
  1733. int cipso_v4_skbuff_getattr(const struct sk_buff *skb,
  1734. struct netlbl_lsm_secattr *secattr)
  1735. {
  1736. int ret_val = -ENOMSG;
  1737. unsigned char *cipso_ptr;
  1738. u32 doi;
  1739. struct cipso_v4_doi *doi_def;
  1740. cipso_ptr = CIPSO_V4_OPTPTR(skb);
  1741. if (cipso_v4_cache_check(cipso_ptr, cipso_ptr[1], secattr) == 0)
  1742. return 0;
  1743. doi = ntohl(*(__be32 *)&cipso_ptr[2]);
  1744. rcu_read_lock();
  1745. doi_def = cipso_v4_doi_search(doi);
  1746. if (doi_def == NULL)
  1747. goto skbuff_getattr_return;
  1748. /* XXX - This code assumes only one tag per CIPSO option which isn't
  1749. * really a good assumption to make but since we only support the MAC
  1750. * tags right now it is a safe assumption. */
  1751. switch (cipso_ptr[6]) {
  1752. case CIPSO_V4_TAG_RBITMAP:
  1753. ret_val = cipso_v4_parsetag_rbm(doi_def,
  1754. &cipso_ptr[6],
  1755. secattr);
  1756. break;
  1757. case CIPSO_V4_TAG_ENUM:
  1758. ret_val = cipso_v4_parsetag_enum(doi_def,
  1759. &cipso_ptr[6],
  1760. secattr);
  1761. break;
  1762. case CIPSO_V4_TAG_RANGE:
  1763. ret_val = cipso_v4_parsetag_rng(doi_def,
  1764. &cipso_ptr[6],
  1765. secattr);
  1766. break;
  1767. }
  1768. skbuff_getattr_return:
  1769. rcu_read_unlock();
  1770. return ret_val;
  1771. }
  1772. /*
  1773. * Setup Functions
  1774. */
  1775. /**
  1776. * cipso_v4_init - Initialize the CIPSO module
  1777. *
  1778. * Description:
  1779. * Initialize the CIPSO module and prepare it for use. Returns zero on success
  1780. * and negative values on failure.
  1781. *
  1782. */
  1783. static int __init cipso_v4_init(void)
  1784. {
  1785. int ret_val;
  1786. ret_val = cipso_v4_cache_init();
  1787. if (ret_val != 0)
  1788. panic("Failed to initialize the CIPSO/IPv4 cache (%d)\n",
  1789. ret_val);
  1790. return 0;
  1791. }
  1792. subsys_initcall(cipso_v4_init);