cipso_ipv4.c 63 KB

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