cipso_ipv4.c 63 KB

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