cipso_ipv4.c 54 KB

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