cipso_ipv4.c 59 KB

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