cipso_ipv4.c 51 KB

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