cipso_ipv4.c 52 KB

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